twpayne/chezmoi

Segfault when adding a file

Closed this issue · 4 comments

Describe the bug

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x11 pc=0x102e7a890]

goroutine 1 [running]:
github.com/twpayne/chezmoi/v2/internal/chezmoi.(*SourceState).Add(0x14000222780, {0x103ee1d60, 0x140000e6c80}, {0x103edf0b0, 0x140006242d0}, {0x103ee1ec0, 0x14000705890}, 0x14000398180, 0x14000739868)
       github.com/twpayne/chezmoi/v2/internal/chezmoi/sourcestate.go:420 +0x1540
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).runAddCmd(0x140002dd008, 0x14000260908?, {0x14000704a70?, 0x0?, 0x0?}, 0x14000222780)
       github.com/twpayne/chezmoi/v2/internal/cmd/addcmd.go:179 +0x280
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).newAddCmd.(*Config).makeRunEWithSourceState.func2(0x14000260908, {0x14000704a70, 0x1, 0x1})
       github.com/twpayne/chezmoi/v2/internal/cmd/config.go:1533 +0x88
github.com/spf13/cobra.(*Command).execute(0x14000260908, {0x14000704a40, 0x1, 0x1})
       github.com/spf13/cobra@v1.8.0/command.go:983 +0x840
github.com/spf13/cobra.(*Command).ExecuteC(0x14000260308)
       github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
       github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).execute(0x14000729dd8?, {0x14000194310, 0x2, 0x2})
       github.com/twpayne/chezmoi/v2/internal/cmd/config.go:1202 +0x70
github.com/twpayne/chezmoi/v2/internal/cmd.runMain({{0x103b89790, 0x6}, {0x103b8a9e0, 0x28}, {0x103b8a720, 0x14}, {0x103b8a638, 0x8}}, {0x14000194310, 0x2, ...})
       github.com/twpayne/chezmoi/v2/internal/cmd/cmd.go:269 +0x18c
github.com/twpayne/chezmoi/v2/internal/cmd.Main({{0x103b89790, 0x6}, {0x103b8a9e0, 0x28}, {0x103b8a720, 0x14}, {0x103b8a638, 0x8}}, {0x14000194310?, 0x102949468?, ...})
       github.com/twpayne/chezmoi/v2/internal/cmd/cmd.go:105 +0x54
main.main()
       github.com/twpayne/chezmoi/v2/main.go:24 +0xd8

To reproduce

Running:

chezmoi add ~/.config/helix/themes/ayu_custom.toml

Expected behavior

No seg fault due to null pointer ever, regardless of the system state. graceful error handling with proper message should be printed.

Output of command with the --verbose flag

Exactly the same as above

Output of chezmoi doctor

Output
$ chezmoi doctor

RESULT    CHECK                       MESSAGE
ok        version                     v2.47.2, commit f4904293d9e3d5297f481b0b1ec00981716fff39, built at 2024-03-17T00:45:31Z, built by Homebrew
ok        latest-version              v2.47.2
ok        os-arch                     darwin/arm64
ok        uname                       Darwin mbp.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64
ok        go-version                  go1.22.1 (gc)
ok        executable                  /opt/homebrew/bin/chezmoi
ok        upgrade-method              brew-upgrade
ok        config-file                 ~/.config/chezmoi/chezmoi.toml, last modified 2024-03-21T08:15:38-07:00
warning   source-dir                  ~/.local/share/chezmoi is a git working tree (dirty)
ok        suspicious-entries          no suspicious entries
warning   working-tree                ~/.local/share/chezmoi is a git working tree (dirty)
ok        dest-dir                    ~ is a directory
ok        umask                       022
ok        cd-command                  found /opt/homebrew/bin/nu
ok        cd-args                     /opt/homebrew/bin/nu
info      diff-command                not set
ok        edit-command                found /opt/homebrew/bin/hx
ok        edit-args                   /opt/homebrew/bin/hx
ok        git-command                 found /opt/homebrew/bin/git, version 2.44.0
ok        merge-command               found /usr/bin/vimdiff
ok        shell-command               found /opt/homebrew/bin/zsh
ok        shell-args                  /opt/homebrew/bin/zsh
info      age-command                 age not found in $PATH
ok        gpg-command                 found /opt/homebrew/bin/gpg, version 2.4.5
info      pinentry-command            not set
ok        1password-command           found /opt/homebrew/bin/op, version 2.26.1
info      bitwarden-command           bw not found in $PATH
info      bitwarden-secrets-command   bws not found in $PATH
info      dashlane-command            dcli not found in $PATH
info      doppler-command             doppler not found in $PATH
info      gopass-command              gopass not found in $PATH
info      keepassxc-command           keepassxc-cli not found in $PATH
info      keepassxc-db                not set
info      keeper-command              keeper not found in $PATH
info      lastpass-command            lpass not found in $PATH
info      pass-command                pass not found in $PATH
info      passhole-command            ph not found in $PATH
ok        rbw-command                 found ~/.cargo/bin/rbw, version 1.8.3
info      vault-command               vault not found in $PATH
info      vlt-command                 vlt not found in $PATH
info      secret-command              not set

Additional context

I think my chezmoi config has been modified by chezmoi init has't been run yet

Thanks for reporting this.

I need more information to be able to reproduce this:

  1. Is there anything special about any of the path elements in ~/.config/helix/themes/ayu_custom.toml, for example, are you using .chezmoiexternal or external_?
  2. What is the output of chezmoi dump ~/.config/helix?

Hey thanks for getting back to me @twpayne.
I manually worked around this issue by doing the copy myself, so i can't reproduce it anymore with the same commands, but I think i narrowed down the issue to doing chemoi add on a file in a directory that doesn't exist in the source...

i.e.

$ mkdir ~/.config/helix/t/
$ cp ~/.config/helix/themes/ayu_custom.toml ~/.config/helix/t/custom.toml
$ chezmoi add ~/.config/helix/t/custom.toml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x11 pc=0x10356e890]

goroutine 1 [running]:
github.com/twpayne/chezmoi/v2/internal/chezmoi.(*SourceState).Add(0x140001aed80, {0x1045d5d60, 0x14000588500}, {0x1045d30b0, 0x1400045acf0}, {0x1045d5ec0, 0x140002f6ba0}, 0x14000637740, 0x140007b5868)
        github.com/twpayne/chezmoi/v2/internal/chezmoi/sourcestate.go:420 +0x1540
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).runAddCmd(0x140003dd008, 0x14000254908?, {0x1400042b1c0?, 0x0?, 0x0?}, 0x140001aed80)
        github.com/twpayne/chezmoi/v2/internal/cmd/addcmd.go:179 +0x280
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).newAddCmd.(*Config).makeRunEWithSourceState.func2(0x14000254908, {0x1400042b1c0, 0x1, 0x1})
        github.com/twpayne/chezmoi/v2/internal/cmd/config.go:1533 +0x88
github.com/spf13/cobra.(*Command).execute(0x14000254908, {0x1400042b190, 0x1, 0x1})
        github.com/spf13/cobra@v1.8.0/command.go:983 +0x840
github.com/spf13/cobra.(*Command).ExecuteC(0x14000254308)
        github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).execute(0x140007b5dd8?, {0x1400003c340, 0x2, 0x2})
        github.com/twpayne/chezmoi/v2/internal/cmd/config.go:1202 +0x70
github.com/twpayne/chezmoi/v2/internal/cmd.runMain({{0x10427d790, 0x6}, {0x10427e9e0, 0x28}, {0x10427e720, 0x14}, {0x10427e638, 0x8}}, {0x1400003c340, 0x2, ...})
        github.com/twpayne/chezmoi/v2/internal/cmd/cmd.go:269 +0x18c
github.com/twpayne/chezmoi/v2/internal/cmd.Main({{0x10427d790, 0x6}, {0x10427e9e0, 0x28}, {0x10427e720, 0x14}, {0x10427e638, 0x8}}, {0x1400003c340?, 0x10303d468?, ...})
        github.com/twpayne/chezmoi/v2/internal/cmd/cmd.go:105 +0x54
main.main()
        github.com/twpayne/chezmoi/v2/main.go:24 +0xd8

$ mkdir ~/.local/share/chezmoi/dot_config/exact_helix/t/

$ chezmoi add ~/.config/helix/t/custom.toml
works fine here ^

Fails with seg fault. my source path looks like ~/.local/share/chezmoi/dot_config/exact_helix/

i wonder if it has to do with exact_helix adding a new file + dir into that?

to you questions:

  1. nothing special. I don't use externals.
  2. not sure this is needed given the repro details above... it just spits out a json dump of that directory...

I'd guess that the sourceStateDir is nil here:

case sourceStateDir.Attr.External:

Thank you for the extra information. It's the exact_ directory that triggers the bug. I can reproduce this now.