Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS8019 doesn't work during compilation #41391

Closed
msallin opened this issue Feb 4, 2020 · 9 comments
Closed

CS8019 doesn't work during compilation #41391

msallin opened this issue Feb 4, 2020 · 9 comments
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@msallin
Copy link

msallin commented Feb 4, 2020

Version Used:

  • dotnet --version: 3.1.100
  • VS Enterprise 16.4.0

Steps to Reproduce:

  1. Create new Class Library (.NET Framework) project.
  2. Edit Active Rule Set and make CS8019 (Unnecessary using directive) an 'Error'.
  3. Compile project and/or run "dotnet build"

Expected Behavior:
Given that the project starts with a Class1.cs file containing unused using directives, compilation should fail.

Actual Behavior:
Compilation succeeds. The VS UI does show the IntelliSense error, however. dotnet build shows no warnings and no errors.

ConsoleApp4.zip

@sharwell
Copy link
Member

sharwell commented Feb 4, 2020

@dotnet/roslyn-compiler I saw that reporting these diagnostics is tied to the DocumentationMode:

if ((filterTree == null || filterTree == infoTree) && infoTree.Options.DocumentationMode != DocumentationMode.None)
{
TextSpan infoSpan = info.Span;
if (!this.IsImportDirectiveUsed(infoTree, infoSpan.Start))
{
ErrorCode code = info.Kind == SyntaxKind.ExternAliasDirective
? ErrorCode.HDN_UnusedExternAlias
: ErrorCode.HDN_UnusedUsingDirective;
diagnostics.Add(code, infoTree.GetLocation(infoSpan));
}
}

However, even with GenerateDocumentationFile=true I was not able to observe CS8019 diagnostics for command line builds.

@sharwell
Copy link
Member

sharwell commented Feb 4, 2020

I investigated this issue, and found that it is by design (discussion in #23695). Since CS8019 does not filter generated code, it's essentially impossible to increase the severity of this diagnostic and still have a build that works. For example, the {project}.AssemblyInfo.cs files that are generated by default for all SDK projects have unused using directives. Increasing the severity to Error would prevent the build from succeeding, and there would be no way to fix or suppress this case.

@sharwell sharwell closed this as completed Feb 4, 2020
@sharwell sharwell added the Resolution-By Design The behavior reported in the issue matches the current design label Feb 4, 2020
@msallin
Copy link
Author

msallin commented Feb 4, 2020

Hi Sam

Thank you for your quick response. Any idea of a workaround or a replacemenet to get unused usings reported like I expect it by CS8019?

Bascially, I want to let a build fail if there are unused usings.

Thanks, Marc

@sharwell
Copy link
Member

sharwell commented Feb 4, 2020

Currently no such analyzer is provided, but @mavasani is working to make IDE0005 available in command line scenarios.

@ericburcham
Copy link

I don't have time to wait for a public Analyzer. I need to fail the build during compilation for unneeded using statements now. Where would I go to look at the source code for CS8019 so we can make our own internal version?

@gafter
Copy link
Member

gafter commented Feb 6, 2020

@ericburcham Look in ErrorCode.cs for which enum value is assigned the value 8019. Then look to see where that enum value is used.

@mavasani
Copy link
Contributor

mavasani commented Mar 7, 2020

@ericburcham The IDE0005 analyzer for remove unnecessary usings is now part of this NuGet package: https://dotnet.myget.org/feed/roslyn/package/nuget/Microsoft.CodeAnalysis.CSharp.CodeStyle/3.6.0-2.20157.5

You can enforce this on command line for your project as follows:

  1. Install the NuGet package to your project
  2. Mark it as a build warning in your repo level editorconfig/ruleset file:
[*.{cs,vb}]

# IDE0005: Remove unnecessary usings/imports
dotnet_diagnostic.IDE0005.severity = warning
  1. Enable XML documentation comments for your project. This is needed to workaround Compiler does not report unused imports when XML doc comments are disabled #41640

@ericburcham
Copy link

ericburcham commented Mar 7, 2020 via email

@cezarypiatekGC
Copy link

@mavasani Are you going to release Microsoft.CodeAnalysis.CSharp.CodeStyle package via nuget.org ? I'm not able to use myget as a nuget feed inside my company.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

6 participants