Skip to content

Commit 0584b13

Browse files
committed
[shell] respect ZDOTDIR for zsh startup files, and copy startup files instead of linking
1 parent dfd5d53 commit 0584b13

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

internal/impl/shell.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func initShellBinaryFields(path string) *DevboxShell {
152152
shell.userShellrcPath = rcfilePath(".bashrc")
153153
case "zsh":
154154
shell.name = shZsh
155-
shell.userShellrcPath = rcfilePath(".zshrc")
155+
if zdotdir := os.Getenv("ZDOTDIR"); zdotdir != "" {
156+
shell.userShellrcPath = filepath.Join(os.ExpandEnv(zdotdir), ".zshrc")
157+
} else {
158+
shell.userShellrcPath = rcfilePath(".zshrc")
159+
}
156160
case "ksh":
157161
shell.name = shKsh
158162
shell.userShellrcPath = rcfilePath(".kshrc")
@@ -357,14 +361,15 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
357361
func (s *DevboxShell) linkShellStartupFiles(shellSettingsDir string) {
358362
// For now, we only need to do this for zsh shell
359363
if s.name == shZsh {
360-
// Useful explanation of zsh startup files: https://zsh.sourceforge.io/FAQ/zshfaq03.html#l20
361-
filenames := []string{".zshenv", ".zprofile", ".zlogin"}
364+
// List of zsh startup files: https://zsh.sourceforge.io/Intro/intro_3.html
365+
filenames := []string{".zshenv", ".zprofile", ".zlogin", ".zlogout"}
362366

363367
// zim framework
364368
// https://zimfw.sh/docs/install/
365369
filenames = append(filenames, ".zimrc")
366370

367371
for _, filename := range filenames {
372+
// The userShellrcPath should be set to ZDOTDIR already.
368373
fileOld := filepath.Join(filepath.Dir(s.userShellrcPath), filename)
369374
_, err := os.Stat(fileOld)
370375
if errors.Is(err, fs.ErrNotExist) {
@@ -376,12 +381,11 @@ func (s *DevboxShell) linkShellStartupFiles(shellSettingsDir string) {
376381
}
377382

378383
fileNew := filepath.Join(shellSettingsDir, filename)
379-
380-
if err := os.Link(fileOld, fileNew); err == nil {
381-
debug.Log("Linked shell startup file %s to %s", fileOld, fileNew)
382-
} else {
384+
cmd := exec.Command("cp", fileOld, fileNew)
385+
if err := cmd.Run(); err != nil {
383386
// This is a best-effort operation. If there's an error then log it for visibility but continue.
384-
debug.Log("Error linking zsh setting file from %s to %s: %v", fileOld, fileNew, err)
387+
debug.Log("Error copying zsh setting file from %s to %s: %v", fileOld, fileNew, err)
388+
continue
385389
}
386390
}
387391
}

0 commit comments

Comments
 (0)