Meta

Formatting Generated Artifacts

How to keep Xtraq-generated code consistent by running dotnet format locally and in CI.

Deterministic formatting keeps the generated sources diff-friendly and prevents avoidable churn in pull requests. The generator already normalises indentation and braces, but you should still run dotnet format so that analyzers, spacing rules, and newline preferences stay aligned with the solution-wide .editorconfig.

Local Workflow

  1. Run the generator (dotnet run --project src/Xtraq.csproj -- build …).
  2. Format the main project:
    dotnet format src/Xtraq.csproj
    
  3. Format generated samples (limit the scope to the artifact folders to avoid reformatting hand-written sample code):
    dotnet format samples/restapi/RestApiSample.csproj --include samples/restapi/Xtraq
    
  4. Optionally pass --verify-no-changes during development to ensure the workspace is clean before pushing:
    dotnet format Xtraq.sln --verify-no-changes --include src samples/restapi/Xtraq
    

The VS Code format code task (Ctrl/Cmd+Shift+B) already wires dotnet format src/Xtraq.csproj; you can duplicate that task and point it at other projects if needed.

Continuous Verification

  • Add a CI job that runs dotnet format Xtraq.sln --verify-no-changes so diffs fail fast when formatting drifts.
  • Pair the check with your generator invocation in the pipeline: regenerate ➜ dotnet format ➜ tests.
  • When contributors add new artifact folders, update the --include list so the formatter covers them as well.

Troubleshooting

  • Ensure the installed .NET SDK matches the version declared in global.json (if present); older SDKs may not understand the latest format options.
  • If dotnet format reports analyzers that the generator cannot fix automatically, address them in the templates or suppress them in .editorconfig with a comment explaining the rationale.
  • For large repos, combine --include with explicit file globs to keep formatting runs fast.