Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.48k stars 60 forks source link

CliFx.Analyzers produces hundreds of CS8032 warnings in build #135

Closed fossbrandon closed 1 year ago

fossbrandon commented 1 year ago

Version

2.3.2

Details

Hi there, about a year ago I ran into a similar issue to this that you were able to resolve in #127 .

In version 2.3.2 of CliFx, I get hundreds of CS8032 warnings when I build my project within Visual Studio. The only non CS8032 warning I get is CS8784.

Examples of the some of the errors:

warning CS8784: Generator 'JsonSourceGenerator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'Microsoft.CodeAnalysis.IncrementalValueProvider`1<System.Collections.Immutable.ImmutableArray`1<!!0>> Microsoft.CodeAnalysis.IncrementalValueProviderExtensions.Collect(Microsoft.CodeAnalysis.IncrementalValuesProvider`1<!!0>)'.'
warning CS8032: An instance of analyzer CliFx.Analyzers.ParameterMustHaveValidValidatorsAnalyzer cannot be created from C:\Users\branfoss\.nuget\packages\clifx\2.3.2\analyzers\dotnet\cs\CliFx.Analyzers.dll : Method 'get_SupportedDiagnostics' in type 'CliFx.Analyzers.ParameterMustHaveValidValidatorsAnalyzer' from assembly 'CliFx.Analyzers, Version=2.3.2.0, Culture=neutral, PublicKeyToken=null' does not have an implementation..
warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.CSharp.UsePatternMatching.CSharpAsAndMemberAccessDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\7.0.203\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Method 'get_SupportedDiagnostics' in type 'Microsoft.CodeAnalysis.CSharp.UsePatternMatching.CSharpAsAndMemberAccessDiagnosticAnalyzer' from assembly 'Microsoft.CodeAnalysis.CSharp.CodeStyle, Version=4.5.8.17107, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation..

Notes:

Steps to reproduce

Tyrrrz commented 1 year ago

Yeah, I ran into it too, I think it happened when I bumped the minor version on one of the analyzer dependencies. I don't really know a good way to fix it. I assume fixing the version would result in the same error later on when somebody decides to use the library with a newer compiler version 🤔 I'll ask around.

Tyrrrz commented 1 year ago

Also I think the first error you posted (CS8784) is unrelated to CliFx

Tyrrrz commented 1 year ago

Do you by chance have any CS9057 warnings there as well? That's the one I can reliably reproduce with dotnet CLI and Rider, so I wonder if your issue is connected to that one.

fossbrandon commented 1 year ago

Do you by chance have any CS9057 warnings there as well? That's the one I can reliably reproduce with dotnet CLI and Rider, so I wonder if your issue is connected to that one.

I do not get any CS9057 warnings.

Also I think the first error you posted (CS8784) is unrelated to CliFx

I agree, but I either have all CS8032 warnings and the one CS8784 warning, or none of them. It might just be a byproduct of the other warnings I'm seeing.

New findings:

I ran into the same warnings today even when using 2.3.1 which worked for me yesterday. So, I reverted it back to 2.3.0 and I can rebuild using Visual Studio as many times as I want with no warnings. When I move it to 2.3.1, the first rebuild succeeds with no warnings, but if I rebuild again, I get all the warnings back. The same pattern happens when I bump it back up to 2.3.2 where the first rebuild is fine and a follow up rebuild gives warnings. I'm not really sure what to make of this but figured I would mention it since my initial PR description is a bit out of date now.

I'm still not able to get the warnings using the CLI.

fossbrandon commented 1 year ago

I opened another project of mine not using CliFx and I'm encountering similar issues with other analyzers as well, so this seems like a me issue. I'll leave this issue open for a bit and update it if I find any fixes for myself, but I wouldn't worry too much about investigating it on your end for now.

Tyrrrz commented 1 year ago

I ran into the same warnings today even when using 2.3.1 which worked for me yesterday. So, I reverted it back to 2.3.0 and I can rebuild using Visual Studio as many times as I want with no warnings. When I move it to 2.3.1, the first rebuild succeeds with no warnings, but if I rebuild again, I get all the warnings back. The same pattern happens when I bump it back up to 2.3.2 where the first rebuild is fine and a follow up rebuild gives warnings. I'm not really sure what to make of this but figured I would mention it since my initial PR description is a bit out of date now.

Do the warnings also appear on 2.3.0 if you rebuild it twice? The warnings showing up every other time or so is a .NET issue, I think, and is not limited to analyzers. I'm not sure why exactly it happens (maybe some build cache preventing the warnings from triggering?).

Also, what version of the .NET SDK(s) do you have installed? Try to install the very latest version (.NET 8 preview) and see if it helps, out of curiosity. You don't have to change anything about your project, but if the issue is indeed related to Roslyn, then a newer SDK (which has newer Roslyn) might be able to resolve it. It may not be a permanent solution, but would help identify the issue more accurately.

Tyrrrz commented 1 year ago

I fixed an issue related to analyzers, which I think could be causing the problem you described. If it didn't help, please reopen the issue.

fossbrandon commented 1 year ago

Here are my currently installed .NET SDKS:

> dotnet --list-sdks
6.0.311 [C:\Program Files\dotnet\sdk]
7.0.201 [C:\Program Files\dotnet\sdk]
7.0.203 [C:\Program Files\dotnet\sdk]

I have not tried 2.3.1 or 2.3.2 with the .NET 8 preview since I'm not currently setup with the preview Visual Studio version. If I find the time soon, I'll try to get that setup and see if the newer SDK removes the warnings.

However, the new 2.3.3 release with my current SDKs and Visual Studio v17.5.4 no longer shows all the warnings I was seeing. I hate to see downgraded analyzers, but it does fix my current problems which may or may not be Visual Studio specific errors.

Thanks for updating the package so fast!

Tyrrrz commented 1 year ago

However, the new 2.3.3 release with my current SDKs and Visual Studio v17.5.4 no longer shows all the warnings I was seeing. I hate to see downgraded analyzers, but it does fix my current problems which may or may not be Visual Studio specific errors.

The main issue was that I recently added some checks for parameters/options bound to required properties. Because this language feature was added in C# 11 (.NET 7 SDK), I had to upgrade the analyzers to target the corresponding version of Roslyn. Then, in version 2.3.2 I bumped the version of Roslyn further by 1 minor (from 3.4.0 to 3.5.0) and, unbeknownst to me, it pushed the target from .NET 7 SDK to .NET 8 SDK.

In the latest version, I've downgraded the targeted version of Roslyn to 3.0.0 to cover even older SDK versions, and implemented the required keyword check myself in a slightly more elaborate way, without relying on newer Roslyn APIs. This is a bit hacky, but it's the easiest solution.

The correct way to handle this is to actually build an analyzer that multi-targets different Roslyn versions and provides checks based on available capabilities. But doing that is incredibly cumbersome right now and requires far more effort than the alternative. It's disappointing, but it seems that Microsoft doesn't see much point in investing into authoring experience of Roslyn extensions, because they consider it a scenario that's "too advanced".