fix(hooks): post-edit-format is a silent no-op (dotnet format --include path handling) - #27
Open
kig777 wants to merge 1 commit into
Open
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
hooks/post-edit-format.shnever formats anything. It exits 0 either way, and2>/dev/null || truehides the outcome, so the failure is invisible — edited files simply stay unformatted.Two independent causes:
--includeis resolved against the current directory. The hook passes an absolute path, which matches no file.C:/...form for its directory walk (correct, that walk needs it) and then passes that same form todotnetas the project. The project loads, but--includematches 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:
--includesrc/App.Core/App.Core.csproj(relative)C:/repo/src/App.Core/App.Core.csprojC:/repo/src/App.Core/App.Core.csprojC:/repo/src/App.Core/App.Core.csprojC:\repo\src\App.Core\App.Core.csproj(native)Fix
cygpath -wwhencygpathis available.--includerelative 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
cygpathis absent, so the project argument is untouched and the only change is that--includenow 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.Repeated for all three input paths the hook accepts — PostToolUse stdin JSON,
$1with a backslash path, and$1with a relative path. All three format correctly.I could not test on Linux or macOS; the change there is limited to the
--includerelativization described above.Note (out of scope)
The
2>/dev/null || trueis what turned a path bug into an invisible one. Worth considering whether the hook should surface a faileddotnet formatrather than swallow it — happy to open a separate PR if you want that changed.