Skip to content

Commit

Permalink
fix: Fix panic when adding children in exact_ dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 30, 2024
1 parent 60cc314 commit b366d0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ DEST_ABS_PATH:
switch sourceStateDir, ok := node.sourceStateEntry.(*SourceStateDir); {
case i != len(nodes)-1 && !ok:
panic(fmt.Errorf("nodes[%d]: unexpected non-terminal source state entry, got %T", i, node.sourceStateEntry))
case sourceStateDir.Attr.External:
case ok && sourceStateDir.Attr.External:
return fmt.Errorf("%s: cannot add entry in external_ directory", destAbsPath)
}
}
Expand Down Expand Up @@ -639,7 +639,7 @@ func (s *SourceState) AddDestAbsPathInfos(
return nil
}
parentRelPath := parentAbsPath.MustTrimDirPrefix(s.destDirAbsPath)
if s.root.get(parentRelPath) != nil {
if _, ok := s.root.get(parentRelPath).(*SourceStateDir); ok {
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/addcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ func TestAddCmd(t *testing.T) {
),
},
},
{
name: "issue_3666",
root: map[string]any{
"/home/user": map[string]any{
".config/helix/themes/ayu_custom.toml": "# contents of ayu_custom.toml\n",
".local/share/chezmoi": map[string]any{
"dot_config/exact_helix": &vfst.Dir{Perm: 0o777 &^ chezmoitest.Umask},
},
},
},
args: []string{"~/.config/helix/themes/ayu_custom.toml"},
tests: []any{
vfst.TestPath("/home/user/.local/share/chezmoi/dot_config/exact_helix/themes/ayu_custom.toml",
vfst.TestModeIsRegular(),
vfst.TestModePerm(0o666&^chezmoitest.Umask),
vfst.TestContentsString("# contents of ayu_custom.toml\n"),
),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
chezmoitest.SkipUnlessGOOS(t, tc.name)
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/testdata/scripts/issue3666.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# test that chezmoi add creates parent directories
exec chezmoi add $HOME${/}.config/helix/themes/ayu_custom.toml
exists $CHEZMOISOURCEDIR/dot_config/exact_helix/themes/ayu_custom.toml

-- home/user/.config/helix/themes/ayu_custom.toml --
# contents of ayu_custom.toml
-- home/user/.local/share/chezmoi/dot_config/exact_helix/.keep --

0 comments on commit b366d0e

Please sign in to comment.