dotnet format
Use the formatter already shipped with the selected .NET SDK. Preserve the repository's formatter ownership, existing changes, and configured style instead of introducing new preferences.
Workflow
- Read the nearest
AGENTS.md,global.json, solution/project files,.editorconfig, and current Git status. - Confirm the exact trusted workspace to load.
dotnet formatmay restore, compile, and run analyzers from that workspace. - Determine whether the request is read-only verification or permission to apply fixes. Do not run a mutating command for a review, explanation, or diagnosis request.
- Preserve the current diff before formatting. When scope is uncertain, begin with
--verify-no-changesor a narrow--includelist. - Choose the smallest formatter surface that matches the request:
whitespacefor indentation, spacing, and line-ending rules;stylefor built-in .NET code-style diagnostics;analyzersfor fixable non-style analyzer diagnostics;- the command without a subcommand only when all applicable surfaces are intended.
- Treat
--includeand--excludevalues as workspace-relative file or directory paths, not shell globs. Use--diagnosticsto narrow style or analyzer fixes by rule ID. - After a mutating run, inspect
git diff --stat, representative diffs, line endings, and every changed file. If scope is unexpectedly broad, stop and narrow the command; never discard pre-existing user changes. - Rerun the matching command with
--verify-no-changes. When analyzer fixes were applied, also build and run the tests relevant to the changed behavior.
Invariants
.editorconfigand existing MSBuild analyzer configuration are the source of truth. Do not add an arbitrary style template unless the user asks for one.- Use
--no-restoreonly after dependencies have already been restored successfully. - Generated files stay excluded unless the repository explicitly owns and formats them.
- A successful formatter process does not prove that every diagnostic has an automatic fix. Review its output or JSON report and use build/analyzer results as the final evidence.
- Keep formatter responsibilities explicit when CSharpier, ReSharper cleanup, generated-code tools, or other formatters coexist.
References
- Read references/commands.md for precise local, CI, filtering, and troubleshooting commands.
- Read references/config.md when changing
.editorconfig, analyzer severity, or formatter ownership.