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

Some rules can no longer be silenced with an .editorconfig file in .NET 7 #6281

Closed
billybraga opened this issue Nov 18, 2022 · 7 comments · Fixed by #6427
Closed

Some rules can no longer be silenced with an .editorconfig file in .NET 7 #6281

billybraga opened this issue Nov 18, 2022 · 7 comments · Fixed by #6427

Comments

@billybraga
Copy link

billybraga commented Nov 18, 2022

Analyzer

Diagnostic ID: CA1848: Use the LoggerMessage delegates (among others)

Analyzer source

SDK: Built-in CA analyzers in .NET 7 SDK

Version: SDK 7.0.100

Describe the bug

Some rules' severity can no longer be set to none with a global .editorconfig file in .NET 7, but it could in .NET 6.

Steps To Reproduce

Build this solution with .NET 7.

Expected behavior

The Net7ProjectWithEditorConfigIgnore project builds successfully, like its equivalent in .NET 6.

Actual behavior

The project Net7ProjectWithEditorConfigIgnore fails to build because of CA1848 (CA1303 was successfully silenced, as we can see by the fact that Net7ProjectWithoutEditorConfigIgnore fails with CA1303).

Additional context

See the .editorconfig. It sets to none the severity of CA1303 and CA1848 in the Net7ProjectWithEditorConfigIgnore folder.

@christianduerselen
Copy link

christianduerselen commented Nov 29, 2022

We're also running into this issue after udpating to Visual Studio 17.4 and .NET 7. While the projects configuration remained for "net6.0", the update in the tooling broke all our builds because we're using

<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>

and override errors within the .editorconfig instead of using other means of configuration. The official documentation at
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#editorconfig
still shows the usage of overriding dotnet_diagnostic.CA____ severities.

Is this a regression problem or has the behavior of this feature changed with .NET 7?

@mavasani
Copy link
Contributor

mavasani commented Nov 30, 2022

This seems to have regressed with #5776 when CodeAnalysisTreatWarningsAsErrors is true and TreatWarningsAsErrors is not true. With that change, we now pass in /warnaserror+:CAxxxx for all CA rules and as per the severity precedence rules specified in https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#severity-options, /warnaserror+:ruleId overrides the editorconfig severity configurations dotnet_diagnostic.ruleId.severity = xxxx in the compiler.

I am not sure if it is possible to alter the compiler's precedence rules between /warnaserror+:ruleId and the editorconfig severity configurations, so we likely need a different mechanism to implement CodeAnalysisTreatWarningsAsErrors in the .NET SDK.

Meanwhile, as a workaround you can do one of the following:

  1. Set TreatWarningsAsErrors to true instead of CodeAnalysisTreatWarningsAsErrors
  2. Set <WarningsNotAsErrors>$(WarningsNotAsErrors);CAxxxx</WarningsNotAsErrors> for specific rules you don't want to bump to errors.

@billybraga
Copy link
Author

Solution 1 works, thanks!

@christianduerselen
Copy link

christianduerselen commented Nov 30, 2022

@mavasani thank you for locating/isolating the cause.
Unfortunately we're in a migration phase for other code parts and have TreatWarningsAsErrors specifically set to false at the moment. Maybe we'll find a way to use it in combination with temporary editorconfig or WarningNotAsError overrides.

As mentioned the two options are only workarounds and not a "solution" as @billybraga described it. Since there seems to be an open issue with the handling between .editorconfig and warnaserror arguments, should this topic then preferrably be left open since the regression is still an issue that may be fixed at some point? Or should this be adressed in a separate topic in an SDK repo?

@mavasani
Copy link
Contributor

mavasani commented Dec 1, 2022

Since there seems to be an open issue with the handling between .editorconfig and warnaserror arguments

Can you please point out the issue?

@christianduerselen
Copy link

As you pointed out:

I am not sure if it is possible to alter the compiler's precedence rules between /warnaserror+:ruleId and the editorconfig severity configurations, so we likely need a different mechanism to implement CodeAnalysisTreatWarningsAsErrors in the .NET SDK.

If I understand correctly, using compiler parameters is the necessary way to fix #5776 which implicitly changes the precedence of severity options and therefore .editorconfig is ignored. This only applies to the specific case where TreatWarningsAsErrors=false and CodeAnalysisTreatWarningsAsErrors=true.

I evaluated the behavior of TreatWarningsAsErrors and having TreatWarningsAsErrors=true it's still possible to override e.g. CS0067 via .editorconfig. Is the following an inconsistent behavior that would need an open issue?

  • TreatWarningsAsErrors=true - severity can be overwritten in .editorconfig
  • TreatWarningsAsErrors=false & CodeAnalysisTreatWarningsAsErrors=true - severity cannot be overwritten in .editorconfig

@mavasani mavasani reopened this Dec 8, 2022
mavasani added a commit to mavasani/roslyn-analyzers that referenced this issue Jan 12, 2023
Fixes dotnet#6281

Prior implementation for `CodeAnalysisTreatWarningsAsErrors` passed `/warnaserror+:CAxxxx` to the command line for each CA rule ID. This led to couple of issues:
1. All disabled by default CA warnings seem to get enabled as errors
2. Editorconfig/globalconfig based configuration of the enabled CA rules stops working.

The new implementation updates our globalconfig tooling to generate parallel globalconfigs with `_warnaserror` suffix, such that rules which would have had effective warning severity are set to error severity in the new globalconfig files. The targets/props that picks the appropriate globalconfig based on the `AnalysisLevel` property now chooses the file with `_warnaserror` suffix if user has enabled code analysis warnings to be treated as errors.

Another note: Given that the buggy implementation of `CodeAnalysisTreatWarningsAsErrors` has already shipped in .NET7 and prior SDKs, we use a separate property `EffectiveCodeAnalysisTreatWarningsAsErrors`, so users using the newer NuGet package, but older .NET SDK can still get this functionality. Users that move to the newer .NET8 preview SDK with this fix can use `CodeAnalysisTreatWarningsAsErrors` itself.
mavasani added a commit to mavasani/roslyn-analyzers that referenced this issue Jan 12, 2023
Fixes dotnet#6281

Prior implementation for `CodeAnalysisTreatWarningsAsErrors` passed `/warnaserror+:CAxxxx` to the command line for each CA rule ID. This led to couple of issues:
1. All disabled by default CA warnings seem to get enabled as errors
2. Editorconfig/globalconfig based configuration of the enabled CA rules stops working.

The new implementation updates our globalconfig tooling to generate parallel globalconfigs with `_warnaserror` suffix, such that rules which would have had effective warning severity are set to error severity in the new globalconfig files. The targets/props that picks the appropriate globalconfig based on the `AnalysisLevel` property now chooses the file with `_warnaserror` suffix if user has enabled code analysis warnings to be treated as errors.

Another couple of notes:
1. Given that the buggy implementation of `CodeAnalysisTreatWarningsAsErrors` has already shipped in .NET7 and prior SDKs, we use a separate property `EffectiveCodeAnalysisTreatWarningsAsErrors`, so users using the newer NuGet package, but older .NET SDK can still get this functionality. Users that move to the newer .NET8 preview SDK with this fix can use `CodeAnalysisTreatWarningsAsErrors` itself.
2. I have adjusted the tooling to generate the config files into buildtransitive folder instead of build folder. This is needed as a follow-up to dotnet#6325 for globalconfig files to be correctly mapped.
3. I have also renamed all the tooling generated globalconfig files to have .globalconfig extension instead of .editorconfig extension, as that was misleading.

I have verified that `CodeAnalysisTreatWarningsAsErrors` works fine with the locally built package when `EffectiveCodeAnalysisTreatWarningsAsErrors` is set to true. If I manually edit the targets/props shipped inside .NET7 SDK to delete all the prior shipped `CodeAnalysisTreatWarningsAsErrors` logic, then setting `CodeAnalysisTreatWarningsAsErrors` also works fine.
mavasani added a commit to mavasani/roslyn-analyzers that referenced this issue Jan 12, 2023
Fixes dotnet#6281

Prior implementation for `CodeAnalysisTreatWarningsAsErrors` passed `/warnaserror+:CAxxxx` to the command line for each CA rule ID. This led to couple of issues:
1. All disabled by default CA warnings seem to get enabled as errors
2. Editorconfig/globalconfig based configuration of the enabled CA rules stops working.

The new implementation updates our globalconfig tooling to generate parallel globalconfigs with `_warnaserror` suffix, such that rules which would have had effective warning severity are set to error severity in the new globalconfig files. The targets/props that picks the appropriate globalconfig based on the `AnalysisLevel` property now chooses the file with `_warnaserror` suffix if user has enabled code analysis warnings to be treated as errors.

Another couple of notes:
1. Given that the buggy implementation of `CodeAnalysisTreatWarningsAsErrors` has already shipped in .NET7 and prior SDKs, we use a separate property `EffectiveCodeAnalysisTreatWarningsAsErrors`, so users using the newer NuGet package, but older .NET SDK can still get this functionality. Users that move to the newer .NET8 preview SDK with this fix can use `CodeAnalysisTreatWarningsAsErrors` itself.
2. I have adjusted the tooling to generate the config files into buildtransitive folder instead of build folder. This is needed as a follow-up to dotnet#6325 for globalconfig files to be correctly mapped.
3. I have also renamed all the tooling generated globalconfig files to have .globalconfig extension instead of .editorconfig extension, as that was misleading.

I have verified that `CodeAnalysisTreatWarningsAsErrors` works fine with the locally built package when `EffectiveCodeAnalysisTreatWarningsAsErrors` is set to true. If I manually edit the targets/props shipped inside .NET7 SDK to delete all the prior shipped `CodeAnalysisTreatWarningsAsErrors` logic, then setting `CodeAnalysisTreatWarningsAsErrors` also works fine.
@mavasani
Copy link
Contributor

mavasani commented Jan 13, 2023

Notes about this fix:

  1. With Re-implement CodeAnalysisTreatWarningsAsErrors with globalconfig files #6427, we have re-implemented how CodeAnalysisTreatsWarningsAsErrors works, which should address the issues identified here. The fix is part of Microsoft.CodeAnalysis.NetAnalyzers NuGet package version 8.0.0-preview1.23063.1 which should be available on this public feed very soon: https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet8/NuGet/Microsoft.CodeAnalysis.NetAnalyzers/versions

  2. To avail this fix one would need to do either of the following:

    1. Move to .NET 8 SDK preview1 or later (once released). You should be able to use CodeAnalysisTreatsWarningsAsErrors property once you are on an SDK version with this fix, regardless of whether you consume the analyzers directly from the SDK or add a NuGet package reference to Microsoft.CodeAnalysis.NetAnalyzers NuGet package with version greater than or equals 8.0.0-preview1.23063.1
    2. If you are unable to move to .NET 8 SDK preview1 or later, and still continue to be on .NET 7 SDK or earlier, you can avail this fix by just adding a PackageReference to Microsoft.CodeAnalysis.NetAnalyzers with version greater than or equals 8.0.0-preview1.23063.1 but setting the property EffectiveCodeAnalysisTreatsWarningsAsErrors instead of CodeAnalysisTreatsWarningsAsErrors. EffectiveCodeAnalysisTreatsWarningsAsErrors overrides the buggy implementation of CodeAnalysisTreatsWarningsAsErrors in already shipped .NET 7 SDK. In future, whenever you are able to move to .NET 8 SDK, you can revert back to using CodeAnalysisTreatsWarningsAsErrors property instead of EffectiveCodeAnalysisTreatsWarningsAsErrors

craigktreasure added a commit to craigktreasure/SlnUp that referenced this issue Sep 6, 2023
I'm trying a few changes to see if I can get the analyzers to behave properly locally. For some reason, they get into a state where they ignore the rules defined in the `.editorconfig` files entirely. Something similar to [this](dotnet/roslyn-analyzers#6281), but it certainly isn't "fixed". Also, I believe there is a case where the node reuse makes this even worse requiring me to kill all `dotnet` processes in order for some settings changes to even take effect, so I'm attempting the use of the `Directory.Build.rsp` file with the `/NodeReuse:false` setting to see if it helps at all. I have a feeling that there is a quirk between the nodes that VS starts up and the ones that the CLI starts up.
craigktreasure added a commit to craigktreasure/SlnUp that referenced this issue Sep 6, 2023
I'm trying a few changes to see if I can get the analyzers to behave
properly locally. For some reason, they get into a state where they
ignore the rules defined in the `.editorconfig` files entirely.
Something similar to
[this](dotnet/roslyn-analyzers#6281), but it
certainly isn't "fixed". Also, I believe there is a case where the node
reuse makes this even worse requiring me to kill all `dotnet` processes
in order for some settings changes to even take effect, so I'm
attempting the use of the `Directory.Build.rsp` file with the
`/NodeReuse:false` setting to see if it helps at all. I have a feeling
that there is a quirk between the nodes that VS starts up and the ones
that the CLI starts up.
craigktreasure added a commit to craigktreasure/SynologyDdnsUpdater that referenced this issue Sep 16, 2023
I'm trying a few changes to see if I can get the analyzers to behave properly locally. For some reason, they get into a state where they ignore the rules defined in the `.editorconfig` files entirely. Something similar to [this](dotnet/roslyn-analyzers#6281), but it certainly isn't "fixed". Also, I believe there is a case where the node reuse makes this even worse requiring me to kill all `dotnet` processes in order for some settings changes to even take effect, so I'm attempting the use of the `Directory.Build.rsp` file with the `/NodeReuse:false` setting to see if it helps at all. I have a feeling that there is a quirk between the nodes that VS starts up and the ones that the CLI starts up.
craigktreasure added a commit to craigktreasure/SynologyDdnsUpdater that referenced this issue Sep 16, 2023
I'm trying a few changes to see if I can get the analyzers to behave
properly locally. For some reason, they get into a state where they
ignore the rules defined in the `.editorconfig` files entirely.
Something similar to
[this](dotnet/roslyn-analyzers#6281), but it
certainly isn't "fixed". Also, I believe there is a case where the node
reuse makes this even worse requiring me to kill all `dotnet` processes
in order for some settings changes to even take effect, so I'm
attempting the use of the `Directory.Build.rsp` file with the
`/NodeReuse:false` setting to see if it helps at all. I have a feeling
that there is a quirk between the nodes that VS starts up and the ones
that the CLI starts up.
craigktreasure added a commit to craigktreasure/Treasure.Utils that referenced this issue Sep 23, 2023
I'm trying a few changes to see if I can get the analyzers to behave properly locally. For some reason, they get into a state where they ignore the rules defined in the `.editorconfig` files entirely. Something similar to [this](dotnet/roslyn-analyzers#6281), but it certainly isn't "fixed". Also, I believe there is a case where the node reuse makes this even worse requiring me to kill all `dotnet` processes in order for some settings changes to even take effect, so I'm attempting the use of the `Directory.Build.rsp` file with the `/NodeReuse:false` setting to see if it helps at all. I have a feeling that there is a quirk between the nodes that VS starts up and the ones that the CLI starts up.
craigktreasure added a commit to craigktreasure/Treasure.Utils that referenced this issue Sep 23, 2023
I'm trying a few changes to see if I can get the analyzers to behave
properly locally. For some reason, they get into a state where they
ignore the rules defined in the `.editorconfig` files entirely.
Something similar to
[this](dotnet/roslyn-analyzers#6281), but it
certainly isn't "fixed". Also, I believe there is a case where the node
reuse makes this even worse requiring me to kill all `dotnet` processes
in order for some settings changes to even take effect, so I'm
attempting the use of the `Directory.Build.rsp` file with the
`/NodeReuse:false` setting to see if it helps at all. I have a feeling
that there is a quirk between the nodes that VS starts up and the ones
that the CLI starts up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants