| name | csharpier |
| description | CSharpier code formatting rules for the DataNormalizer project. Covers configuration (.csharpierrc.yaml), local tool manifest, format/check commands (v1.2.6 subcommand syntax), CI integration, and the "don't fight the formatter" philosophy. |
CSharpier Formatting
Overview
CSharpier is an opinionated C# code formatter. This project uses CSharpier v1.2.6 which requires subcommand syntax (dotnet csharpier format . instead of the older dotnet csharpier .).
Configuration
.csharpierrc.yaml
Located at the solution root:
printWidth: 120
This is the only configuration. CSharpier is intentionally opinionated — there are very few knobs.
.config/dotnet-tools.json
CSharpier is installed as a local .NET tool:
{
"version": 1,
"isRoot": true,
"tools": {
"csharpier": {
"version": "1.2.6",
"commands": ["dotnet-csharpier"]
}
}
}
Commands
IMPORTANT: v1.2.6 Subcommand Syntax
CSharpier v1.2.6 uses subcommand syntax. The older dotnet csharpier . syntax is deprecated.
dotnet csharpier format .
dotnet csharpier check .
dotnet csharpier format src/DataNormalizer/Runtime/NormalizationContext.cs
dotnet csharpier check src/DataNormalizer/Runtime/NormalizationContext.cs
Restore Tools First
Before running CSharpier (especially in CI or fresh clones):
dotnet tool restore
CI Integration
In .github/workflows/ci.yml:
- name: Check formatting
run: dotnet tool restore && dotnet csharpier check .
This step fails the CI build if any files are not formatted correctly.
Workflow Rules
Always Format Before Committing
dotnet csharpier format .
git add -A
git commit -m "your message"
Don't Fight the Formatter
If CSharpier reformats your code in a way you don't prefer, accept it. The value of CSharpier is consistency, not personal preference.
var result = someObject.Method1().Method2().Method3().Method4();
var result = someObject
.Method1()
.Method2()
.Method3()
.Method4();
What CSharpier Does NOT Format
- Comments content (it preserves comment text)
- String content
.csproj / .props / .targets XML files
- YAML / JSON files
- Markdown files
CSharpier only formats .cs files.
Common Scenarios
Long Lines
CSharpier wraps lines that exceed printWidth: 120:
public static NormalizedPersonResult Normalize(Person source, NormalizationContext context, CancellationToken cancellationToken)
public static NormalizedPersonResult Normalize(
Person source,
NormalizationContext context,
CancellationToken cancellationToken
)
Trailing Commas
CSharpier adds trailing commas in multi-line constructs:
var person = new Person
{
Name = "Alice",
Age = 30,
};
Method Chains
var names = items.Select(x => x.Name).ToList();
var results = typeGraph
.Where(static node => node.Kind == PropertyKind.Normalized)
.Select(static node => node.FullyQualifiedName)
.Distinct()
.OrderBy(static name => name)
.ToList();
Troubleshooting
"dotnet csharpier" Not Found
dotnet tool restore
dotnet tool install csharpier
Format Check Fails in CI
The most common cause is forgetting to run dotnet csharpier format . before committing. Fix:
dotnet csharpier format .
git add -A
git commit --amend --no-edit
Generated Code
CSharpier formats all .cs files, including generated ones in obj/. This is fine — generated files in obj/ are not committed to source control (they're in .gitignore).
For generated files that ARE committed (e.g., Verify snapshot .verified.cs files), CSharpier will format them too. This is expected and desired for consistency.