Skip to content

chore(pre-commit): format .NET code via a local dotnet-format hook#182

Merged
devantler merged 1 commit into
mainfrom
claude/precommit-dotnet-format
Jun 1, 2026
Merged

chore(pre-commit): format .NET code via a local dotnet-format hook#182
devantler merged 1 commit into
mainfrom
claude/precommit-dotnet-format

Conversation

@devantler
Copy link
Copy Markdown
Contributor

🤖 Generated by the Daily AI Assistant

Problem

.pre-commit-config.yaml shipped empty (repos: []) while both its header comment and AGENTS.md promised "automatic .NET code formatting" — a no-op that delivers nothing. It also diverges from its sibling go-template, which ships a working config (golangci-lint-fmt + a local hook). This is the pre-commit watch-item flagged in the roadmap epic #167 ("either populate with a useful hook (e.g. dotnet format) or drop it").

Change

Wire up the .NET equivalent of go-template's setup — a local dotnet-format hook (mirroring go-template's local-hook pattern) that runs dotnet format on staged C# changes:

repos:
  - repo: local
    hooks:
      - id: dotnet-format
        name: Format .NET code with dotnet format
        entry: dotnet format
        language: system
        files: '\\.cs$'
        pass_filenames: false

Adopters opt in with pre-commit install and get format-on-commit that matches the template's EnforceCodeStyleInBuild / TreatWarningsAsErrors stance — catching formatting locally before it becomes a CI build error. AGENTS.md updated to describe the configured hook (was "empty/prepared … no hooks configured yet").

Real finding the hook surfaced

Running the new hook revealed pre-existing drift the in-build analyzers miss: both example .cs files carried a UTF-8 BOM (EF BB BF), violating the repo's own .editorconfig charset = utf-8 rule (charset/BOM normalization is a dotnet format concern, not a build-enforced analyzer rule, so it slipped past CI). Stripped the BOM so the scaffold is dotnet format-clean and the hook passes on first run for adopters.

Validation (local, .NET 10.0.300 SDK)

  • pre-commit validate-config ✅ · yamllint ✅ (only the cosmetic document-start warning, matching the original file and go-template's config)
  • pre-commit run dotnet-format --all-filesFailed once (fixed the BOM drift), then Passed clean (idempotent) ✅
  • dotnet build -c Release0 warnings, 0 errors ✅ · dotnet test -c Release2 passed

Notes

  • Dev-convenience tooling only — no CI/build/release behaviour change; chore: so it does not cut a NuGet release.
  • One coherent concern: the BOM fix is a prerequisite for the hook to pass out-of-box, not a mixed-in refactor.

The .pre-commit-config.yaml shipped empty (repos: []) while its header and
AGENTS.md both promised "automatic .NET code formatting" — a no-op that
delivers nothing and diverges from go-template, whose config runs a real
golangci-lint-fmt + local hook. Wire up the .NET equivalent: a local
dotnet-format hook (mirroring go-template's local-hook pattern) that runs
"dotnet format" on staged C# changes, so adopters get format-on-commit that
matches the template's EnforceCodeStyleInBuild / TreatWarningsAsErrors stance.

Running the new hook surfaced pre-existing drift the in-build analyzers miss:
both example .cs files carried a UTF-8 BOM, violating the repo's own
.editorconfig charset = utf-8 rule (charset/BOM is not a build-enforced
analyzer rule). Stripped the BOM so the scaffold is dotnet-format-clean and
the hook passes on first run for adopters.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 1, 2026 03:18
@github-code-quality
Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall coverage in the branch is 100%. Coverage data for the branch is not yet available.

Show a code coverage summary of the most covered files.
File 401a7de +/-
/home/runner/wo...ExampleClass.cs 100%

Code Coverage is in Public Preview. Learn more and provide us with your feedback.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Wires up the previously-empty .pre-commit-config.yaml with a local dotnet-format hook that runs dotnet format on staged C# changes, removes pre-existing UTF-8 BOMs from the two example .cs files (which violated .editorconfig's charset = utf-8), and updates AGENTS.md to describe the now-configured hook.

Changes:

  • Add a local dotnet-format pre-commit hook triggered by *.cs changes.
  • Strip UTF-8 BOM from ExampleClass.cs and ExampleClassTests.cs.
  • Update AGENTS.md to reflect the configured hook (replacing the prior "no hooks configured yet" wording).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
.pre-commit-config.yaml Replaces empty repos: [] with a local dotnet format hook scoped to *.cs.
src/Example/ExampleClass.cs Removes leading UTF-8 BOM.
tests/Example.Tests/ExampleClassTests.cs Removes leading UTF-8 BOM.
AGENTS.md Updates description of .pre-commit-config.yaml to mention the new hook and opt-in via pre-commit install.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@devantler devantler marked this pull request as ready for review June 1, 2026 05:58
@devantler devantler merged commit 10dc6aa into main Jun 1, 2026
12 checks passed
@devantler devantler deleted the claude/precommit-dotnet-format branch June 1, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants