Skip to content

fix(hooks): post-edit-format is a silent no-op (dotnet format --include path handling) - #27

Open
kig777 wants to merge 1 commit into
codewithmukesh:mainfrom
kig777:fix/dotnet-format-include-relative-path
Open

fix(hooks): post-edit-format is a silent no-op (dotnet format --include path handling)#27
kig777 wants to merge 1 commit into
codewithmukesh:mainfrom
kig777:fix/dotnet-format-include-relative-path

Conversation

@kig777

@kig777 kig777 commented Aug 8, 2026

Copy link
Copy Markdown

Problem

hooks/post-edit-format.sh never formats anything. It exits 0 either way, and 2>/dev/null || true hides the outcome, so the failure is invisible — edited files simply stay unformatted.

Two independent causes:

  1. --include is resolved against the current directory. The hook passes an absolute path, which matches no file.
  2. On Git Bash, the project argument must be a native Windows path. The script normalizes the edited path to the mixed C:/... form for its directory walk (correct, that walk needs it) and then passes that same form to dotnet as the project. The project loads, but --include matches nothing even when the include path itself is right.

Both have to be fixed; either one alone still yields a no-op. Measured on Windows 11 / Git Bash, .NET 10 SDK, formatting one file of a real solution:

project argument --include result
src/App.Core/App.Core.csproj (relative) cwd-relative formats
C:/repo/src/App.Core/App.Core.csproj cwd-relative no-op
C:/repo/src/App.Core/App.Core.csproj project-relative no-op
C:/repo/src/App.Core/App.Core.csproj absolute no-op ← current behaviour
C:\repo\src\App.Core\App.Core.csproj (native) cwd-relative formats

Fix

  • Convert the project with cygpath -w when cygpath is available.
  • Make --include relative to the current directory when the edited file lives under it.

A file outside the current directory keeps its previous absolute form, so nothing regresses there. On Linux and macOS cygpath is absent, so the project argument is untouched and the only change is that --include now uses the documented relative form.

Verification

Windows 11, Git Bash, .NET 10 SDK, against a real multi-project solution: introduce an indentation violation, run the hook, check git status.

  • Before the change: violation survives, hook exits 0.
  • After the change: file comes back formatted.

Repeated for all three input paths the hook accepts — PostToolUse stdin JSON, $1 with a backslash path, and $1 with a relative path. All three format correctly.

I could not test on Linux or macOS; the change there is limited to the --include relativization described above.

Note (out of scope)

The 2>/dev/null || true is what turned a path bug into an invisible one. Worth considering whether the hook should surface a failed dotnet format rather than swallow it — happy to open a separate PR if you want that changed.

`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.
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