csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Can this be implemented as Roslyn Analysers?

Open Meligy opened this issue 3 years ago • 12 comments

If this is possible, we'd get a lot of benefits:

  • C# integration via the C# extension itself
  • We might not need anything special for MSBuild
  • Possible support of dotnet format (maybe just need to turn off some built-in analyzers or something)

Forgive my ignorance if this is already implemented this way and still doesn't achieve these goals.

Meligy avatar Aug 18 '22 03:08 Meligy

I haven't had time to check this out yet, but my one worry is that analyzers run on a specific node type. This could be an analyzer on the CompilationUnitSyntax, but I don't know if a file would be re-analyzed when a line inside of a method changes. Even without that it would still work as part of the build, and may even be faster than the current MsBuild package.

belav avatar Sep 04 '22 21:09 belav

Thanks a lot. I mention this because it's how dotnet roslynator format works for example. It might also help with getting access to editorconfig style rules and using them.

Meligy avatar Sep 05 '22 00:09 Meligy

I tried creating an Analyzer in the CSharpier solution, but couldn't get past this error. There is some weirdness already with the source generators needing very specific versions of Microsoft.CodeAnalysis

  An instance of analyzer Insite.Analyzers.Net6Rules.CSharpierAnalyzer cannot be created from
C:\projects\csharpier\CSharpier.Analyzer\bin\Debug\net6.0\CSharpier.Analyzer.dll : 
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
The system cannot find the file specified..

I tried creating an empty solution for the analyzer, and referencing CSharpier from nuget. When I try to use CSharpier in the analyzer project, I get this error. But CSharpier can be referenced just fine from within a console app.

  Analyzer 'Insite.Analyzers.Net6Rules.CSharpierAnalyzer' threw an exception of type 'System.IO.FileNotFoundException'
with message 'Could not load file or assembly 'CSharpier, Version=0.18.0.0, Culture=neutral, PublicKeyToken=null'.
The system cannot find the file specified.'.

I'm not sure how to get past either of those errors.

belav avatar Oct 03 '22 16:10 belav

to use nuget packages inside of source generators it's a bit complicated.

https://www.youtube.com/watch?v=wp-dxZXRkJ4

OneCyrus avatar Oct 04 '22 09:10 OneCyrus

I brought this up before with them, but didn't get any response: https://github.com/dotnet/roslyn/discussions/54099

When I pinged the roslyn devs offline, they were against the idea since csharpier does't handle code with syntax errors.

shocklateboy92 avatar Oct 24 '22 21:10 shocklateboy92

Source generators can generate new source files to add to the compilation, but very importantly, they cannot modify existing files (at least, last time I checked). As a result, a source generator cannot be used to format the code.

From this FAQ on source generators:

The key difference is that Source Generators don’t allow you rewrite user code. We view this limitation as a significant benefit, since it keeps user code predictable with respect to what it actually does at runtime. We recognize that rewriting user code is a very powerful feature, but we’re unlikely to enable Source Generators to do that.

Code analyzers lets you analyze the code and produce diagnostics and the associated code fixes. If anything, this can be done as an analyzer, but not as a source generator.

gat-cs avatar Oct 26 '22 08:10 gat-cs

If anything, this can be done as an analyzer, but not as a source generator.

I totally agree. Code generators are for generated code. Analyzers are for human-written code.

Meligy avatar Oct 27 '22 01:10 Meligy

I ran across this which could help figuring out how to get the required nuget packages into CSharpier as an analyzer

https://www.meziantou.net/packaging-a-roslyn-analyzer-with-nuget-dependencies.htm

belav avatar Dec 19 '23 21:12 belav