← Back to Pull requests

feat(builtins): add csharpier C# formatter #1958

Closed
unknown closed this pull request without merging on Nov 29, 2026 16:34 UTC
View on GitHub
unknown unknown commented on Nov 29, 2026 15:14 UTC
Author

Adds Builtins.csharpier, a check/fix step that runs CSharpier, the opinionated C# formatter, on staged C# and MSBuild files.

hk already has Builtins.dotnet_format, but dotnet format works on a whole project or solution: it needs a single .csproj/.sln to discover, restores packages, and can compile analyzers, so the step is exclusive and slow in a pre-commit hook. CSharpier formats only the files it is given, with no restore or build, so it fits a per-file hook and runs in parallel with other steps.

amends "hk:/Config.pkl"
import "hk:/Builtins.pkl"

hooks {
  ["pre-commit"] {
    fix = true
    stash = "git"
    steps {
      ["csharpier"] = Builtins.csharpier
    }
  }
}

With class A{void M(){var x=1;}} staged in Program.cs, hk check fails with CSharpier's report:

Error ./Program.cs - Was not formatted.
  ----------------------------- Expected: Around Line 1 -----------------------------
  class A
  {
  ----------------------------- Actual: Around Line 1 -----------------------------
  class A{void M(){var x=1;}}

and hk fix rewrites the file. CSharpier's own settings apply: .csharpierrc/.csharpierrc.json/.csharpierrc.yaml and .editorconfig, and files listed in .csharpierignore are skipped.

Files. The step's glob is */.cs, */.csx, */.csproj, */.props and */.targets. CSharpier 1.x formats MSBuild project files as well as C#, and csharpier format . formats them too, so they are included. It also formats other XML (.xml, .config, .slnx, .xaml); those are left out because a project's other XML often belongs to another tool. Override glob to add them, or to drop the MSBuild files.

Commands. check runs csharpier check <files> and fix runs csharpier format <files>. check_diff prints a patch built with hk util format-diff from csharpier format --write-stdout <file>, so hk fix with check_first can show and apply the change. It does not use CSharpier's stdin mode because CSharpier 1.4.2 crashes formatting stdin when --stdin-path names a file with no directory part (for example Program.cs at the repository root). CSharpier prints nothing for files it skips (generated files such as .g.cs, and files in .csharpierignore or .gitignore), so a small sh wrapper prints those unchanged and the patch for the other files still applies. On Windows, where there is no POSIX shell, check_diff is empty and hk falls back to check.

Versions and installing. The step targets the CSharpier 1.x CLI (tested with 1.4.2). The 0.x syntax, dotnet csharpier --check, is not supported. CSharpier is a .NET tool and needs the .NET SDK both to install and to run. With mise:

mise use dotnet dotnet:csharpier

hk init suggests the step when it finds a .csharpierrc, .csharpierignore, or a .config/dotnet-tools.json that mentions csharpier.

Tests. hk test --step csharpier passes on Linux with CSharpier 1.4.2 and .NET SDK 10.0.404: unformatted, formatted and syntax-error files for check; fix; diff tests for a root file, a file in a nested directory with a space in its name, a .csproj, and a set that includes a generated and an ignored file; and a .csharpierrc.json that changes the indent size. To run these in CI, mise run test:bats:setup now also installs .NET SDK 10.0.404 (added to mise.lock), and test/builtins_tests.bats exports DOTNET_ROOT as the real SDK directory behind mise where dotnet@10 and puts it on PATH, because mise drops its own installs directory from PATH when it runs a tool stub. The test fails with a clear message when no .NET SDK is installed. The csharpier cases also pass in the full builtins tests run bats case. macOS and Windows were not tested.

Requested in https://github.com/jdx/hk/discussions/1951.

AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: 2.1.332.

🤖 Generated with Claude Code

<!-- CURSOR_SUMMARY -->
---

[!NOTE]

Low Risk

Additive formatter builtin and test-only mise/bats wiring; no changes to auth, data handling, or core hook execution paths beyond running another optional step in CI.

> Overview

Adds a new Builtins.csharpier hook step for opinionated C# and MSBuild formatting, aimed at per-file pre-commit use without project restore/build (unlike dotnet_format).

> The step runs csharpier check / csharpier format on globs for .cs, .csx, and common MSBuild files, with check_diff built via format-diff and a POSIX shell wrapper so skipped/ignored files still produce a valid patch (Windows leaves check_diff empty). Mise deps are dotnet and dotnet:csharpier.

> Test/CI wiring: Bats setup preinstalls .NET SDK 10.0.404 (mise.lock), adds a csharpier tool stub, and builtins tests run exports DOTNET_ROOT and puts the SDK on PATH so .NET tool stubs work under parallel mise jobs.

> <sup>Reviewed by Cursor Bugbot for commit 5b218d59763b8a294a4c9c6d634aa55d71c48e6e. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Diff: 650 lines (+487 / -163) across 8 files Reviewer: jdx Review duration: 80m
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.388
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1958"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52% (MEDIUM) · Higher = larger blast radius
52%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 20.5% · Safe to merge
20.5%
Repository