Consistent formatting locally and in CI — when IDE0055 fails only on the build agent
Part 11 of 13 · Code quality & toolingIn one codebase I worked on, the CI pipeline (Linux, .NET 9) started failing with IDE0055: Fix formatting
in hundreds of places. Locally, every developer had a clean build. Visual Studio showed no warnings, Rider was
happy, and git diff showed nothing. The code was “correctly formatted” everywhere except on the one machine that
actually decides whether a PR can be merged.
Formatting seems like the most trivial quality topic there is. That’s exactly why it’s so annoying when it breaks: nobody wants to spend an afternoon on whitespace. The fix is a one-time setup, and afterwards formatting is something nobody has to think about again.
Why local and CI disagree
Section titled “Why local and CI disagree”The mismatch came from four differences between the developer machines and the build agent:
| Cause | What happens |
|---|---|
| CRLF vs. LF | Windows checkouts had CRLF line endings, the Linux agent LF. Roslyn’s formatting check compares against the configured end_of_line, so the same file passes on one machine and fails on the other. |
| UTF-8 with BOM vs. without | Some files had a byte order mark, others didn’t. With a charset rule in place, the inconsistent ones are flagged. |
| Different formatters | Rider uses its own formatting engine, Visual Studio uses Roslyn. Code that Rider considers formatted isn’t necessarily formatted for Roslyn’s IDE0055. |
| Strictness only in CI | Locally, formatting findings were suggestions. CI ran dotnet format --verify-no-changes, which fails on any difference. |
None of these is a bug. They’re all the result of each tool doing what it’s configured to do, with no shared configuration.
The solution: one source of truth in the repository
Section titled “The solution: one source of truth in the repository”Three files and one command:
.editorconfigdefines how code should look, for every IDE and fordotnet format..gitattributesmakes git store and check out files with consistent line endings.dotnet formatapplies and verifies the rules, locally and in CI.
.editorconfig
Section titled “.editorconfig”The parts that matter for cross-platform consistency come first:
root = true
[*]charset = utf-8end_of_line = lfinsert_final_newline = truetrim_trailing_whitespace = trueindent_style = spaceindent_size = 4
[*.{json,yml,yaml,xml,csproj,props,targets}]indent_size = 2
[*.cs]# Formatting violations fail the builddotnet_diagnostic.IDE0055.severity = error
# C# formatting preferencescsharp_new_line_before_open_brace = allcsharp_indent_case_contents = truecsharp_space_after_keywords_in_control_flow_statements = truecsharp_prefer_braces = truecsharp_style_namespace_declarations = file_scopedcsharp_using_directive_placement = outside_namespacedotnet_sort_system_directives_first = true
# Severities via dotnet_diagnostic: the "option = value:warning" shorthand is ignored by the builddotnet_diagnostic.IDE0011.severity = warning # bracesdotnet_diagnostic.IDE0161.severity = warning # file-scoped namespacesdotnet_diagnostic.IDE0065.severity = warning # using placement
# Generated code is not ours to format[**/Migrations/*.cs]generated_code = truecharset = utf-8 means UTF-8 without BOM. That’s the choice I’d make for any repository built on Linux agents.
Visual Studio is fine with it, and nothing else trips over invisible bytes at the start of a file.
Together with EnforceCodeStyleInBuild in your
Directory.Build.props, the IDE0055 severity makes formatting part of the
normal build, so you see it in the IDE as you type instead of only in CI.
.gitattributes: the part most teams miss
Section titled “.gitattributes: the part most teams miss”.editorconfig tells editors to write LF. It doesn’t control what git does. On Windows, git is often installed
with core.autocrlf=true, which converts LF to CRLF on checkout. The result: .editorconfig says LF, the files on
disk say CRLF, and the formatting check fails locally or in CI depending on the machine.
.gitattributes overrides the per-machine git setting for everyone:
# Normalize all text files to LF in the repository and on checkout* text=auto eol=lf
# Windows-only scripts keep CRLF*.cmd text eol=crlf*.bat text eol=crlf
# Binary files are never touched*.png binary*.dll binaryAfter adding it, re-normalize the files that are already in the repository once:
git add --renormalize .git commit -m "Normalize line endings to LF"The big reformatting commit
Section titled “The big reformatting commit”Once the configuration is in place, run the formatter over the whole solution:
dotnet format MyShop.slnThe first run produces a large, whitespace-only diff. That’s expected. Do it in a dedicated PR with nothing else in it, merge it quickly (it conflicts with everything), and ask the team to rebase afterwards.
To keep git blame useful, add the commit to a .git-blame-ignore-revs file:
# Normalize line endings and apply dotnet format3f9c2e1a7d...GitHub picks this file up automatically in its blame view. Locally, git config blame.ignoreRevsFile .git-blame-ignore-revs does the same.
Make every IDE follow the same rules
Section titled “Make every IDE follow the same rules”- Visual Studio reads
.editorconfigby default. Make sure “Use .editorconfig settings” isn’t disabled under Tools → Options → Text Editor → Code Style. - Rider has EditorConfig support under Settings → Editor → Code Style. Enable it, and make sure it overrides the
IDE’s own settings. Rider’s formatter still isn’t Roslyn, so for edge cases
dotnet formatis the referee. - VS Code with C# Dev Kit uses Roslyn and reads
.editorconfig.
Format on save is a nice convenience, but don’t rely on it. Not everyone enables it, and not every tool that touches code (including AI agents) runs it.
The CI step
Section titled “The CI step”- name: Verify formatting run: dotnet format MyShop.sln --verify-no-changes --verbosity diagnostic--verify-no-changes fails with a non-zero exit code if any file would change, and lists the files and rules
involved. Run it before the build and tests, because it’s fast and the most common reason for a red PR.
For large solutions, dotnet format can take a while. Two options: run only the whitespace check
(dotnet format whitespace --verify-no-changes), which doesn’t need a full compilation, or restrict the check to
files changed in the PR with --include.
Catching it before the push
Section titled “Catching it before the push”A pre-commit hook gives the fastest feedback. A plain git hook works, or a tool like Husky.Net if you want it to be
installed automatically on dotnet tool restore:
#!/bin/sh# .githooks/pre-commit: check only staged C# filesfiles=$(git diff --cached --name-only --diff-filter=ACM -- '*.cs')[ -z "$files" ] && exit 0dotnet format MyShop.sln --verify-no-changes --include $files || { echo "Formatting issues found. Run 'dotnet format' and stage the changes." exit 1}Hooks are optional and can be skipped, so the CI step stays the source of truth.
Formatting approaches compared
Section titled “Formatting approaches compared”| IDE settings only | .editorconfig + IDE |
.editorconfig + dotnet format in CI |
CSharpier | |
|---|---|---|---|---|
| Same result for every developer | ❌ | mostly | ✅ | ✅ |
| Enforced in CI | ❌ | ❌ | ✅ | ✅ (csharpier check) |
| Configurable style | ✅ | ✅ | ✅ | ❌ deliberately opinionated |
| Covers code style beyond whitespace | partly | ✅ | ✅ (style, analyzers subcommands) |
❌ whitespace and layout only |
| Line endings and encoding | ❌ | ✅ | ✅ (with .gitattributes) |
partly |
CSharpier is worth a look if your team is tired of discussing style: like Prettier, it
formats everything one way, with almost no options. It doesn’t replace .editorconfig for analyzer severities and
code-style rules, though, and mixing it with Roslyn’s IDE0055 means you need to decide which formatter is in charge.
Pitfalls
Section titled “Pitfalls”.gitattributes without re-normalizing. Adding the file only affects files git touches from now on. Without
git add --renormalize ., existing files keep their old line endings in the repository.
Generated code in the check. EF Core migrations, gRPC stubs and other generated files shouldn’t fail a formatting
check. Mark them with generated_code = true or exclude their folders.
Different SDK versions. dotnet format ships with the SDK, and its rules can change between versions. Pin the SDK
with global.json so local runs and CI use the same formatter.
Formatting rules as suggestion locally, error in CI. That’s exactly the situation from the beginning. Use the
same severity everywhere. That’s what having it in .editorconfig is for.
Using formatting as a review topic. Once the tooling enforces it, whitespace comments in code reviews should stop.
If a reviewer wants a different rule, it’s a PR to .editorconfig, not a comment on someone’s code. More on what
reviews should and shouldn’t cover in the code review checklist.
Summary
Section titled “Summary”- “Clean locally, failing in CI” formatting errors come from line endings, BOMs, different formatters and different strictness.
- Put the rules in
.editorconfig: UTF-8 without BOM, LF, andIDE0055as an error. - Add
.gitattributeswith* text=auto eol=lfand re-normalize once..editorconfigalone doesn’t control git. - Reformat in one dedicated PR and add it to
.git-blame-ignore-revs. - Run
dotnet format --verify-no-changesin CI before the build. Pin the SDK withglobal.json. - Once tooling enforces formatting, stop discussing whitespace in reviews.