numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
602 stars 37 forks source link

`nil pointer dereference` when running --no-cache on removed, git-staged file #364

Closed willbush closed 2 months ago

willbush commented 2 months ago

Describe the bug

nil pointer dereference when doing treefmt --no-cache after rm on a file that was staged in git.

To Reproduce

Steps to reproduce the behavior:

~
❯ mkdir test && cd test

~/test
❯ cat << EOF > treefmt.toml
[formatter.yaml]
command = "yamlfmt"
includes = ["*.yaml", "*.yml"]
EOF

~/test
❯ echo "key:  value" > test.yaml

~/test
❯ git init
Initialized empty Git repository in /home/will/test/.git/

test on  main [?]
❯ git add -A

test on  main [+]
❯ treefmt
WARN format: no formatter for path: treefmt.toml
traversed 2 files
emitted 2 files for processing
formatted 1 files (1 changed) in 2ms

test on  main [!+]
❯ treefmt --no-cache
WARN format: no formatter for path: treefmt.toml
traversed 2 files
emitted 2 files for processing
formatted 1 files (0 changed) in 2ms

test on  main [!+]
❯ rm test.yaml

test on  main [✘+]
❯ treefmt
treefmt: error: failed to generate change set: failed to walk path: lstat /home/will/test/test.yaml: no such file or directory

test on  main [✘+]
❯ treefmt --no-cache
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x9970b6]

goroutine 23 [running]:
git.numtide.com/numtide/treefmt/cli.(*Format).Run.(*Format).walkFilesystem.func6.2(0xc0001ea5d0, {0x19?, 0x0?})
    git.numtide.com/numtide/treefmt/cli/format.go:241 +0x56
git.numtide.com/numtide/treefmt/walk.(*gitWalker).Walk(0xc0001f2160, {0xba6f58, 0xc000168b40}, 0xc0001f2180)
    git.numtide.com/numtide/treefmt/walk/git.go:64 +0x66a
git.numtide.com/numtide/treefmt/cli.(*Format).Run.(*Format).walkFilesystem.func6()
    git.numtide.com/numtide/treefmt/cli/format.go:235 +0x425
golang.org/x/sync/errgroup.(*Group).Go.func1()
    golang.org/x/sync@v0.7.0/errgroup/errgroup.go:78 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
    golang.org/x/sync@v0.7.0/errgroup/errgroup.go:75 +0x96

Expected behavior

no crash

System information

Linux

$ treefmt --version
treefmt v2.0.3

$ yamlfmt --version
yamlfmt 0.13.0 (v0.13.0)

$ nixos-version
24.11.20240727.b73c222 (Vicuna)

Additional context

What I'm doing is weird, but I'm trying to narrow down another issue I'm having that might be related to https://github.com/numtide/treefmt/issues/343.

brianmcgee commented 2 months ago

With #377 we now log a warning and continue gracefully when a file has been removed from the filesystem but not yet staged:

❯ nix run github:numtide/treefmt/fix/git-rm-staged-file
WARN walker[git]: Path /tmp/tmp.SO08xXzmo1/test.yaml is in the index but appears to have been removed from the filesystem
traversed 1 files
emitted 0 files for processing
formatted 0 files (0 changed) in 0s

❯ nix run github:numtide/treefmt/fix/git-rm-staged-file -- --no-cache
WARN walker[git]: Path /tmp/tmp.SO08xXzmo1/test.yaml is in the index but appears to have been removed from the filesystem
WARN format: no formatter for path: treefmt.toml
traversed 1 files
emitted 1 files for processing
formatted 0 files (0 changed) in 0s