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

Add an analyzer rule against unused using directives #19747

Merged
merged 2 commits into from
Oct 15, 2021

Conversation

penev92
Copy link
Member

@penev92 penev92 commented Oct 12, 2021

What started as simple "spring" cleaning turned out bigger than expected, so splitting it into its own PR.
Unfortunately none of the options I tried with the suggested analyzer rules IDE0005 and CS8019 could make make check fail in any way, but I still think having the rule is an improvement. Arguably it could be even made to show as an error inside an IDE (both VS and VSCode do it), but it still won't stop the build.

@pchote
Copy link
Member

pchote commented Oct 12, 2021

Did you try defining the full

<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
   <Rule Id="IDE0005" Action="Warning" />
</Rules>

?

IDE0005 isn't part of the StyleCop.Analyzers namespace, so it seems to me that make check is behaving correctly and your IDE is wrong.

@penev92
Copy link
Member Author

penev92 commented Oct 12, 2021

Tested both namespaces (StyleCop.Analyzers and Microsoft.CodeAnalysis.CSharp.Features) with both rules (IDE0005 and CS8019) and no combination of any two or more behaves any different to the other combinations - both IDEs still report a warning/error, but still compile successfully and dotnet build still reports 0 warnings/errors and compiles successfully.

@RoosterDragon
Copy link
Member

So it looks like IDE0005 specifically is borked. IDE0005 docs have a note which states To enable this rule on build, you need to enable XML documentation comments for the project. See this issue for more details. Which links to dotnet/roslyn#41640

If you follow the advice, you can indeed get it to show up at build time. Add this to the csproj

<PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

You'll get some additional spam but it does actually work!

Therefore your changes are fine, it's just a bug in the infrastructure. I wouldn't bother with the workaround since it adds other spam - I think adding the rules will do for now and at some point in future I guess it will magically work in the build again. Other IDEXXX rules are not affected and appear to work without any workarounds.

That said I agree with pchote it would be best to put it into the correct analyser group. Apparently you can choose between Microsoft.CodeAnalysis.CSharp.Features and Microsoft.CodeAnalysis.CSharp.CodeStyle - they seem to have the same rules and no I don't know what the difference is.

<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.CodeStyle" RuleNamespace="Microsoft.CodeAnalysis.CSharp.CodeStyle">
    <Rule Id="IDE0005" Action="Warning" />
</Rules>

Changes looks fine otherwise.

@penev92
Copy link
Member Author

penev92 commented Oct 13, 2021

Thanks to @RoosterDragon we now know we need a combination of EnforceCodeStyleInBuild and GenerateDocumentationFile in the project file/Directory.Build.props as well as the rule itself, and then a bunch more rules to silence the warnings that come from generating the XML documentation.
I moved rule IDE0005 to the namespace mentioned above.
It all works as expected now with one known issue - EnforceCodeStyleInBuild produces a bunch of warnings in older versions of Visual Studio. I tested an old and a new version of VS, tested in VSCode and tested the Windows make script and everything seems to work as it should.

@pchote
Copy link
Member

pchote commented Oct 14, 2021

We could avoid the VS problems by specifying -p:EnforceCodeStyleInBuild=true -p:GenerateDocumentationFile=true in the msbuild args in the check rule only, like we did for #19671.

I'm pretty sure the CS rules also don't live in StyleCop's namespace?

@RoosterDragon
Copy link
Member

Can confirm the above would work. We still get the warning in the IDE, but make check will also be able to surface it. We would need to maintain the CSXXXX overrides though, or else make check will spit those out too.

@pchote
Copy link
Member

pchote commented Oct 14, 2021

Yeah, that seems like the best compromise to me - they just need to go in the right namespace 😁

@RoosterDragon
Copy link
Member

This is the namespace you'll want for compiler rules.

<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp" RuleNamespace="Microsoft.CodeAnalysis.CSharp">
    <Rule Id="CS1570" Action="None" />
    <Rule Id="CS1573" Action="None" />
    <Rule Id="CS1591" Action="None" />
</Rules>

@penev92
Copy link
Member Author

penev92 commented Oct 14, 2021

Added a separate "group" for the CSxxxx (XML documentation generation) rules with the proper namespace as provided above.
Also added explanations to the new rules and a bunch of empty lines for improved readability.
Also made the XML generation (and code style enforcement) only run when compiling in Debug.

@penev92 penev92 force-pushed the usingDirectives branch 3 times, most recently from 3adfbe9 to 42eb603 Compare October 14, 2021 18:47
@penev92
Copy link
Member Author

penev92 commented Oct 14, 2021

*Also made the XML generation (and code style enforcement) only run for the make check command and added a comment there explaining the weird new parameters.

Copy link
Member

@pchote pchote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise, confirmed to work as expected on macOS.

Makefile Outdated Show resolved Hide resolved
Unfortunately due to bugs in the analyzers or something else, the IDE0005 doesn't work as expected. The "officially suggested" workaround is to enable XML documentation generation to trigger IDE0005 during compiling. Then we need to add three more rules to silence the warnings that come from the XML documentation generation. We also need to enable code style enforcing on build for all of this to work.
Known issue is that all of this produces a bunch (tens to hundreds) of obscure analyzer warnings on older versions of Visual Studio, but those seem to not be causing issues.
Copy link
Member

@pchote pchote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now (but i'm trusting that you've tested it works correctly on Windows).

@abcdefg30 abcdefg30 merged commit e1e7641 into OpenRA:bleed Oct 15, 2021
@penev92 penev92 deleted the usingDirectives branch October 16, 2021 01:00
mavasani added a commit to mavasani/roslyn that referenced this pull request Jan 13, 2022
…ngs) on build

Due to dotnet#41640, enabling IDE0005 on build requires users to enable generation of XML documentation comments. Even though this is documented with a note on IDE0005's [doc page](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005), we have had numerous reports of users not figuring this out and spending lot of cycles fighting with this, especially given other IDE diagnostics work just fine on build. We have had many user reports of the same: dotnet#58103, dotnet#53720, dotnet#57539, OpenRA/OpenRA#19747 and numerous other offline queries.

This change enhances the IDE0005 analyzer to now detect the case when IDE0005 is being reported as a warning or an error in the IDE, but `GenerateDocumentationFile` is `false` for the project, which would mean IDE0005 wouldn't be reported on build. The analyzer reports a special helper diagnostic for this case, which recommends  the user to set this property to `true` in their project file to enable IDE0005 on build. This should reduce the pain for customers who try to enforce IDE0005 on build and get hit by this issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants