Skip to content

fix(hooks): post-edit-format discards dotnet format failures - #28

Open
kig777 wants to merge 2 commits into
codewithmukesh:mainfrom
kig777:fix/post-edit-format-report-failures
Open

fix(hooks): post-edit-format discards dotnet format failures#28
kig777 wants to merge 2 commits into
codewithmukesh:mainfrom
kig777:fix/post-edit-format-report-failures

Conversation

@kig777

@kig777 kig777 commented Aug 8, 2026

Copy link
Copy Markdown

Follow-up to #27, as offered there.

Stacked on #27 — this branch contains that commit, so the PR shows two. Only the second one (fix(hooks): report a failed dotnet format instead of discarding it) belongs to this PR. Merge #27 first and this rebases to a single commit; if you would rather have it standalone, say so and I will rebase onto main.

Problem

dotnet format "$PROJECT" --include "$FILE" --no-restore 2>/dev/null || true

This throws away the error stream and the exit code together, so a broken invocation is indistinguishable from a working one. That is precisely what let the --include path bug in #27 survive unnoticed: the hook formatted nothing, on every edit, and no signal existed anywhere to say so.

Fix

Capture the output, print it when dotnet format exits non-zero.

Still exit 0 deliberately: files mid-edit routinely fail to compile, and a hook that failed there would interrupt an ordinary edit over something the next edit fixes. The goal is visibility, not a gate.

Printing only on failure also keeps a successful run quiet — dotnet format prints nothing on success at default verbosity, and it exits 0 for soft problems like workspace load warnings, which would otherwise add a line of noise to every edit.

Verification

Windows 11, Git Bash, .NET 10 SDK, against a real solution.

Success path — introduce an indentation violation, run the hook:

exit=0
stdout=[]
FIXED

Failure path — same call with dotnet off PATH:

exit=0
post-edit-format: dotnet format failed for src/App.Core/Services/Dn50Atlas.cs (file left unformatted)
.../hooks/post-edit-format.sh: line 92: dotnet: command not found

Worth noting for the record: a corrupt .csproj does not trigger this branch — dotnet format reports Warnings were encountered while loading the workspace and still exits 0. So this surfaces hard failures (missing SDK, bad arguments, IO errors), not every soft problem.

kig777 added 2 commits August 8, 2026 05:41
`dotnet format --include` resolves its paths against the current directory, so
the absolute path the hook passes matches no file. The command still exits 0,
and `2>/dev/null || true` swallows the outcome, so the hook has been a silent
no-op — edited files are never formatted and nothing reports it.

On Git Bash there is a second, independent cause: the script normalizes the
edited path to the mixed C:/... form for its directory walk and then hands that
same form to dotnet as the project argument. dotnet loads the project fine, but
--include then matches nothing even when the include path is correct. The
project argument has to be a native Windows path.

Fix both: convert the project with `cygpath -w` when cygpath is present, and
make --include relative to the current directory when the file lives under it.
Paths outside the current directory keep their previous form, and on Linux and
macOS the project argument is untouched.

Verified against a real solution on Windows 11 / Git Bash by introducing an
indentation violation and checking `git status` after the hook ran — before the
change the violation survived, after it the file comes back formatted. Covered
all three input paths the hook accepts: PostToolUse stdin JSON, `$1` with a
backslash path, and `$1` with a relative path.
`2>/dev/null || true` discarded both the error stream and the exit code, so a
broken invocation looked exactly like a successful one. That is what kept the
--include path bug invisible: the hook had been formatting nothing, and there
was no signal anywhere to say so.

Capture the output and print it when dotnet format exits non-zero. Still exit
0 — files mid-edit routinely fail to compile, and a hook that failed there
would interrupt an ordinary edit for something the next edit fixes.

Output is printed only on failure, so a successful format stays quiet: dotnet
format prints nothing on success at default verbosity, and it exits 0 for soft
problems such as workspace load warnings, which would otherwise add noise to
every single edit.
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.

1 participant