Skip to content

fix: normalize stdin path to be relative to tree root#682

Open
adrian-gierakowski wants to merge 1 commit intonumtide:mainfrom
adrian-gierakowski:fix/includes-excludes-with-stdin
Open

fix: normalize stdin path to be relative to tree root#682
adrian-gierakowski wants to merge 1 commit intonumtide:mainfrom
adrian-gierakowski:fix/includes-excludes-with-stdin

Conversation

@adrian-gierakowski
Copy link
Copy Markdown

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.

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.
Copy link
Copy Markdown
Collaborator

@jfly jfly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This smells weird to me. Could we just make do without trying to call resolvePath at all?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's an implementation without the symlink resolution in case you want to try it for yourself:

adrian-gierakowski@9f74345

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)

@brianmcgee
Copy link
Copy Markdown
Member

brianmcgee commented Apr 7, 2026

main is now green. I'll have some brain space to weigh in on this later in the week.

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.

3 participants