Comment by rafaelmn
4 days ago
Since you mention great CLI - is there a CLI tool that is free/cross platform that works like prettier ? I found https://csharpier.com/ but have never heard of anyone using it.
Also not sure how it works with linters like roslynator or stylecop ?
This is something I miss very much when coming back from TS, other languages also have better formatters. C# is very tied to editors/IDEs which is a nightmare in teams where some people use vs/vs code/rider
Csharpier is the best option available. I've been using it for 3-4 years now and had success getting it adopted at my last two companies on greenfield projects. It's not exactly an unknown tool - nearly 2k stars on Github. But a lot old school .Net companies just don't seem to have much interest in auto formatters because they've been working without them for a long time.
If you're using csharpier you don't need to use Roslynator formatters. No issue with the analyzers package. The csharpier docs have info on what StyleCop rules need to be adjusted so they can work together. Personally I don't use StyleCop anymore as it seems to be fairly dead, and I don't really feel the need for things like enforcing file layout or naming.
Edit: Also I will add that unless the landscape has changed recently, I believe csharpier is the only C# tool that has deterministic formatting good enough to enforce code formatting checks in CI.
dotnet format [0] exists and is maintained by the .NET team. It is different from Prettier in that it is much more configurable, but it gets the job done for us. It does integrate language services and can automatically apply code fixes for custom analyzers. I have not explicitly tried roslynator or stylecop, but I suspect this would work as expected.
[0] https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-f...
The problem is that dotnet format is horribly slow. So much so that it's only useful as a precommit hook
> It is different from Prettier in that it is much more configurable
I only tried it briefly but found it was waaay to lenient with the amount of variance in style it left unchanged (eg line breaks for function parameters) - can it get configured to produce consistent formatting like prettier ?
I think there are also a lot of default rules which aren't enforced until they're bumped up in severity in the .editorconfig.
Also it can hook into roslyn analyzers, so if you want a more opinionated analyzer then stylecop analyzers ( https://github.com/DotNetAnalyzers/StyleCopAnalyzers) would be an option, although I haven't used it myself for a long time.
It can be configured to be much more aggressive with formatting, but I'm not certain if you can force it to produce the exact same output for any possible whitespace in the input.
I was not able to use (recent) dotnet-format as standalone .exe tool, also it only worked if inside an actual project with .sln/.slnx file and all that. I could not instruct it to work on single file.
Hi! I use csharpier, it does the job - I included it as a preliminary check in our CI pipeline for a step of merge requests quality assessment. It is the closest tool I found, that ressembles prettier. You should definitely give it a try.
I’ve been using csharpier for almost a year now and it’s amazing. Because it’s opinionated, you don’t have the space to overthink it, you just use it.
Besides, I really like its formatting style.
csharpier is great. It is what "dotnet format" should be.