mhartkorn/dotnet-formatpipe

`jj fix`-compatible alternative to `dotnet format`

★ 0Forks 0GitHub ↗Compare

README

FormatPipe

A .NET global tool that formats C# source code using Roslyn, designed for use with the jj fix stdin/stdout API. Produces formatting output consistent with dotnet format by reading .editorconfig settings.

Installation

Install the tool globally from NuGet:

dotnet tool install --global FormatPipe

Usage

The tool reads C# source from stdin, can run analyzer fixes/checks, formats with .editorconfig settings resolved from the given file path, and writes the resulting source to stdout.

cat MyFile.cs | formatpipe --stdin-path MyFile.cs --fix-analyzers --format --check-analyzers

Options

Option Description
--stdin-path The file path hint used to resolve .editorconfig settings (required)
--fix-analyzers Runs dotnet format analyzers and applies available fixes before formatting
--check-analyzers Runs dotnet format analyzers --verify-no-changes; exits 1 if warning/error diagnostics remain
--format Runs Roslyn formatting after analyzer operations
--max-fix-iterations Maximum analyzer fix passes; values less than 1 are treated as 5

Execution Order

When multiple stages are requested, formatpipe always runs:

  1. Analyzer fixes (--fix-analyzers)
  2. Formatting (--format)
  3. Analyzer verification (--check-analyzers)

This keeps material code changes first and non-material formatting last.

stdout is reserved for the final source text. Analyzer diagnostics are written to stderr. Exit codes: 0 success, 1 analyzer check failure, 2 tool/runtime failure.

jj fix Configuration

Add the following to your jj configuration (~/.jjconfig.toml or .jj/repo/config.toml):

[fix.tools.formatpipe]
command = ["formatpipe", "--stdin-path", "$path"]
patterns = ["glob:'**/*.cs'"]

To run analyzer fixes, formatting, and final verification in a single jj fix pass:

[fix.tools.formatpipe]
command = ["formatpipe", "--stdin-path", "$path", "--fix-analyzers", "--format", "--check-analyzers"]
patterns = ["glob:'**/*.cs'"]

Then run:

jj fix

This will format all changed C# files in the current revision using Roslyn, respecting .editorconfig settings from the repository.

How It Works

  1. Reads C# source code from stdin
  2. Walks the directory tree from the given --stdin-path upward to collect all .editorconfig files (stopping at root = true)
  3. Optionally runs dotnet format analyzers to apply analyzer fixes
  4. Optionally formats the source using Microsoft.CodeAnalysis.Formatting.Formatter
  5. Optionally runs analyzer verification (--verify-no-changes)
  6. Writes the resulting source to stdout

The formatting rules applied are the same subset that dotnet format uses for whitespace formatting: indentation, spacing, brace placement, and other layout rules defined in .editorconfig.

Uninstall

dotnet tool uninstall --global FormatPipe

Contributors

LanceBresslerPar

Issues