skip to content

In a .NET 10 CI pipeline, how do dotnet format --verify-no-changes and dotnet build -p switches enforce code style, warnings and version stamps?

level: seniorimportance: nice to knowfreq 28%

answer

  1. non-zero exit code, nothing rewritten
  2. .editorconfig is the source of truth
  3. whitespace, style, analyzers subcommands
  4. -p sets a global MSBuild property
  5. TreatWarningsAsErrors, Version, --getProperty

basics

~10 s

dotnet format --verify-no-changes checks the code against .editorconfig and exits non-zero if it would change anything, and -p:Name=Value sets MSBuild properties such as TreatWarningsAsErrors=true or Version=1.4.0 for that build only.

solid answer

~30 s

`dotnet format` applies the `.editorconfig` preferences and analyzer fixes to a project or solution; with `--verify-no-changes` it rewrites nothing and fails the job with a non-zero exit code when any file would change. Its subcommands `whitespace`, `style` and `analyzers` narrow the check, and `--severity` sets the minimum level to fix. `-p:Name=Value` (`--property`) passes a **global** MSBuild property that overrides the same property in the project file: `-p:TreatWarningsAsErrors=true` turns new compiler warnings into a failed build, `-p:Version=1.4.0` stamps the assembly and file versions, `-p:ContinuousIntegrationBuild=true` marks a CI build. `--getProperty:Version` prints an evaluated property, handy for checking what a pipeline actually set.

code

bash · 13 lines
bash
# 1. Formatting gate: rewrites nothing, exits non-zero on drift
dotnet format PressCtl.slnx --verify-no-changes --severity warn

# 2. One Release build with warnings as errors and the release number
dotnet build PressCtl.slnx -c Release \
  -p:TreatWarningsAsErrors=true -p:Version=1.4.0 \
  -p:ContinuousIntegrationBuild=true

# 3. Reuse that build
dotnet test PressCtl.slnx -c Release --no-build

# Check what a property evaluates to
dotnet build src/PressCtl --getProperty:Version

go deeper

for a junior

Recall that dotnet format fixes style from .editorconfig and that -p:Name=Value sets an MSBuild property on the command line.

for a middle

Explain --verify-no-changes and its exit code, the three subcommands, and why a -p value overrides the project file.

for a senior

Design the gate order: fast formatting check first, one build with warnings as errors and a version stamp, then test and publish with --no-build, verified with --getProperty.

for a principal

Decide which rules live in shared .editorconfig and props files versus pipeline switches, so local builds and CI agree and policy changes happen in one reviewed place.

## Two gates and one stamp The print shop's pipeline for its `PressCtl` tool must fail when code is not formatted to the team's rules, fail when a change introduces compiler warnings, and stamp the build with the release number. All three are `dotnet` CLI features. ## dotnet format as a gate `dotnet format` ships inside the SDK and reads its rules from **`.editorconfig`**: indentation and spacing, `var` preferences, naming, and the severities of analyzer diagnostics. On a developer machine it fixes files in place. In CI you add **`--verify-no-changes`**: nothing is written, and the command terminates with a **non-zero exit code** if any file would have been reformatted, which fails the job. The work splits into three subcommands you can run on their own: | Subcommand | Checks | |---|---| | `dotnet format whitespace` | spacing, indentation and line breaks only; the fastest pass | | `dotnet format style` | code-style rules (IDE diagnostics) from `.editorconfig` | | `dotnet format analyzers` | third-party and code-quality analyzer diagnostics | Useful options: `--severity` (`info`, `warn`, `error`) sets the minimum severity to act on; `--include` and `--exclude` limit the files; `--diagnostics` limits the rule IDs; `--report` writes a JSON report the pipeline can archive. Because `style` and `analyzers` load the project, they may restore and compile it and run analyzer code, so point them only at trusted code. ## -p: global MSBuild properties Every build-like verb (`build`, `publish`, `test`, `pack`, `run`) accepts `-p:Name=Value`, also written `--property:Name=Value` or `/p:`. It sets an MSBuild **global property** for that invocation: 1. It overrides the same property assigned in the `.csproj` or in `Directory.Build.props`, so a pipeline can enforce a value without editing files. 2. It applies to every project in the build, including project references. 3. Several values for one property, such as a list, need `%3B` for the semicolons, because a raw `;` separates properties. Properties worth knowing for CI: - `TreatWarningsAsErrors=true`: the SDK defaults it to `false`; true makes any compiler warning fail the build. - `Version=1.4.0`: feeds the generated assembly attributes, so `InformationalVersion` and the file version follow it; `--version-suffix` adds a prerelease suffix to `VersionPrefix`. - `ContinuousIntegrationBuild=true`: tells the build it runs on a CI server, enabling settings meant only for official builds, such as deterministic source paths. ## Seeing what the build sees `dotnet build --getProperty:Version` evaluates the project and prints the final value instead of building, and `--getItem` does the same for items. It is the quickest way to prove that a `-p` switch or a props file took effect. ## A pipeline in order - Run `dotnet format --verify-no-changes` first; it fails fast and gives a precise list of files. - Build once with `-c Release -p:TreatWarningsAsErrors=true -p:Version=$RELEASE`. - Test and publish with `--no-build`, so later steps reuse that exact build. Fixing failures locally is the same command without `--verify-no-changes`. ## Common mistakes - Running `dotnet format` without `--verify-no-changes` in CI: it fixes the agent's copy, exits zero, and the unformatted code stays in the repository. - Leaving `.editorconfig` severities at their defaults and expecting the formatter to enforce them; rules below the chosen `--severity` are not acted on. - Setting `-p:Version` on `dotnet test` and `dotnet publish` separately with different values; build once, then reuse that output with `--no-build` so every artifact carries the same version.

  • Why does a -p value win over the project file?
    MSBuild treats command-line properties as global properties, and a global property cannot be overwritten by an ordinary property assignment in an imported file or the project itself. That makes `-p` the reliable way for a pipeline to enforce a setting.
  • Why run dotnet format whitespace separately?
    Whitespace formatting works from the source text, so it is the quick first gate. The style and analyzer passes load the project, which may restore and compile it and runs analyzer code, so they cost more and should only run on trusted code.

  • dotnet format --verify-no-changes fixes the files and then reports success.
  • dotnet format needs a separate global tool installed on the CI agent.
  • A -p switch is overridden by the same property set in the .csproj.
  • TreatWarningsAsErrors is true by default in new SDK projects.
  • dotnet format reads its rules from the project file, not .editorconfig.