Writing custom codestyle checkers?
How would I go about doing this? Are there any libraries to help?
24 Replies
you'd want to look into roslyn analyzers and #roslyn
Tutorial: Write your first analyzer and code fix - C#
This tutorial provides step-by-step instructions to build an analyzer and code fix using the .NET Compiler SDK (Roslyn APIs).
Essentially, you write a library which acts as an extension to the compiler, analyzing the user's code for things like whitespace or style, and report diagnostics when you find something offending.
(also nice protogen pfp)
How do I run it without using visual studio? I'm on Linux and prefer to use cli anyways
analyzers are also run during builds
yeah
plus any editor you use (unless it's just a plain text editor with no C# awareness) can show them
eg they'll work just fine in Rider or VSCode
As long as you reference the project containing your analyzers properly, it'll work
How do I know if I'm doing that right
I have a rule set to always error so I can make sure it's working but it's not erroring when I build
show us your csproj
Analyzer projects have to target
netstandard2.0
Is there a reason for that?
jaredpar
The biggest blockers to shifting analyzers / generators from
netstandard2.0
to net6+
is
1. The VS OOP process. This can run as a .NET core process but believe you can still push it back to .NET Framework too. Honestly lost track of which is the default now but so long as it can run on .NET Framework it's a blocker
2. The VS process still hosts generators at least and it's a .NET Framework process.
3. The VS Build infrastructure, think typing MSBuild at the command line, is still a .NET Framework entity. There is no supported way to have tools within the build infra depend on the VS private .NET Core runtime.Quoted by
<@334287249816420352> from #roslyn (click here)
React with ❌ to remove this embed.
so they can run in VS
they might work in other contexts without ns2.0, but it's best to just stick to that
also, can we see the csproj for the project consuming the analyzer (assuming fixing the TargetFramework doesn't fix your issue)
the ProjectReference needs to be a little different
Meziantou's blog
Referencing an analyzer from a project - Gérald Barré
Once you have created a Roslyn Analyzer, you have multiple ways to consume it in your project: Using a Visual Studio extension Using a NuGet package Using a Project Reference when the Roslyn Analyzer is in the same solution The first 2 solutions are the most common ones, and are described in the first post of the series: Writing a Roslyn Analyze...
(I don't think you need SetTargetFramework tho)
I'm getting this error now:
CSC : warning AD0001: Analyzer 'Voxel.CodeChecker.Checker' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
you've got a bug in your analyzer then
This is all I have, code-wise right now
huh, weird
I'd check the docs for how to debug analyzers
#roslyn can also help
I'm gonna take a break and look at it again once my head is clear