fix: normalize stdin path to be relative to tree root#682
fix: normalize stdin path to be relative to tree root#682adrian-gierakowski wants to merge 1 commit intonumtide:mainfrom
Conversation
When using --stdin with an absolute path, the RelPath was incorrectly set to the absolute path, causing pattern matching for includes and excludes to fail against project-relative globs. This change ensures that the path provided with --stdin is converted to a relative path from the tree root before processing.
jfly
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
| path := paths[0] | ||
| resolvedPath, err := resolvePath(path) | ||
| if err != nil { | ||
| // If the path doesn't exist, we still want to make it relative to the root if possible. |
There was a problem hiding this comment.
This smells weird to me. Could we just make do without trying to call resolvePath at all?
There was a problem hiding this comment.
I use resolvePath to match existing behaviour when used without --stdin. IMHO the behaviour should be the same between these 2 cases when main.go is a symlink to another file which matches a different formatter than the path of the symlink iself:
treefmt ./main.go
cat main.go | treefmt --stdin ./main.gothe first command (before and after this PR) follows the symlink and dispatches to formatter based on the path the symlink is pointing at
if we don't use resolvePath here, the second command would format based on the location of the symlink, not the file it is pointing at
The fallback is in case the path provided to --stdin is simply a hint, and does not point at an existing file.
There was a problem hiding this comment.
here's an implementation without the symlink resolution in case you want to try it for yourself:
NOTE: I based this PR on last commit for which CI was green (CI failure here must be due to main being broken and github merging main into PR branch before running CI)
|
|
When using --stdin with an absolute path, the RelPath was incorrectly
set to the absolute path, causing pattern matching for includes and
excludes to fail against project-relative globs.
This change ensures that the path provided with --stdin is converted
to a relative path from the tree root before processing.