Skip to content

treefmt . at the root of the project fails if the project root is a symlink #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jfly opened this issue May 25, 2025 · 0 comments · Fixed by #595
Closed

treefmt . at the root of the project fails if the project root is a symlink #594

jfly opened this issue May 25, 2025 · 0 comments · Fixed by #595
Assignees
Labels
bug Something isn't working

Comments

@jfly
Copy link
Collaborator

jfly commented May 25, 2025

Describe the bug

If the root of your project is a symlink, and you try running treefmt . at the root of the repo, you'll get a segfault.

To Reproduce

Steps to reproduce the behavior:

  1. Create a project ./prj with a working treefmt configuration.
  2. Create a symlink ./prj-link: ln -s prj prj-link.
  3. cd prj-link
  4. treefmt .
    $ treefmt .
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6c0548]
    
    goroutine 1 [running]:
    github.com/numtide/treefmt/v2/walk.(*CompositeReader).Read(0xc000214180, {0x9775a8?, 0xc0002042a0?}, {0xc00022c008?, 0x1?, 0x85bf20?})
        github.com/numtide/treefmt/v2/walk/walk.go:165 +0x88
    github.com/numtide/treefmt/v2/cmd/format.Run(0x4?, 0xc0000aeaa0, 0xc0001cbc48?, {0xc0000d5da0, 0x1, 0x3})
        github.com/numtide/treefmt/v2/cmd/format/format.go:157 +0x8e6
    github.com/numtide/treefmt/v2/cmd.runE(0xc0000ea480, 0xc0000aeaa0, 0xc0001bc008, {0xc0000d5da0, 0x1, 0x3})
        github.com/numtide/treefmt/v2/cmd/root.go:183 +0x6c6
    github.com/numtide/treefmt/v2/cmd.NewRoot.func1(0xc000180500?, {0xc0000d5da0?, 0x7?, 0x8c0546?})
        github.com/numtide/treefmt/v2/cmd/root.go:37 +0x32
    github.com/spf13/cobra.(*Command).execute(0xc0001bc008, {0xc000014110, 0x3, 0x3})
        github.com/spf13/[email protected]/command.go:1015 +0xaaa
    github.com/spf13/cobra.(*Command).ExecuteC(0xc0001bc008)
        github.com/spf13/[email protected]/command.go:1148 +0x46f
    github.com/spf13/cobra.(*Command).Execute(0xc000002380?)
        github.com/spf13/[email protected]/command.go:1071 +0x13
    main.main()
        github.com/numtide/treefmt/v2/main.go:12 +0x18

Expected behavior

We certainly shouldn't crash. I would expect this to format the whole repo despite the fact that the user have given us a path to a symlink, which is in opposition to the intent/thinking behind #528.

System information

N/A

Additional context

This was broken in #528. Root cause:

  1. This line of code skips over the given path if it's a symlink: https://github.com/numtide/treefmt/blob/v2.3.1/walk/walk.go#L284
  2. That leaves us with a nil entry in this readers array.
  3. We pull the nil value out of the readers array here: https://github.com/numtide/treefmt/blob/v2.3.1/walk/walk.go#L160
  4. We try to call c.current.Read where c.current == nil here: https://github.com/numtide/treefmt/blob/v2.3.1/walk/walk.go#L165. Boom.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant