Skip to content

Commit fd13459

Browse files
authored
[shell] respect ZDOTDIR for zsh startup files, and copy startup files instead of linking (#1587)
## Summary Seeks to address two concerns that have arisen: 1. Zsh startup files are linked, not copied. We should copy. This was affecting a user who was mounting the filesystem which disallows linking in that scenario. Fixes #1536 2. ZDOTDIR was not respected for finding the ZSH startup files. We should support it as the zsh manual specifies that its the location of its startup files: https://zsh.sourceforge.io/Intro/intro_3.html (Originally for #1582 , but it seems that issue is unrelated to ZDOTDIR) EDIT: Fixes #1582, as per user who reported the issue ## How was it tested? Tested by setting `ZDOTDIR=~/zsh-dot-dir` and moving my `.zshenv` and `.zshrc` files there. Starting a `devbox shell` showed that these startup files were run. Moved them back to my homedir, and unset `ZDOTDIR` and starting a `devbox shell` still worked as expected.
1 parent 200ee00 commit fd13459

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
@@ -149,7 +149,11 @@ func initShellBinaryFields(path string) *DevboxShell {
149149
shell.userShellrcPath = rcfilePath(".bashrc")
150150
case "zsh":
151151
shell.name = shZsh
152-
shell.userShellrcPath = rcfilePath(".zshrc")
152+
if zdotdir := os.Getenv("ZDOTDIR"); zdotdir != "" {
153+
shell.userShellrcPath = filepath.Join(os.ExpandEnv(zdotdir), ".zshrc")
154+
} else {
155+
shell.userShellrcPath = rcfilePath(".zshrc")
156+
}
153157
case "ksh":
154158
shell.name = shKsh
155159
shell.userShellrcPath = rcfilePath(".kshrc")
@@ -348,14 +352,15 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
348352
func (s *DevboxShell) linkShellStartupFiles(shellSettingsDir string) {
349353
// For now, we only need to do this for zsh shell
350354
if s.name == shZsh {
351-
// Useful explanation of zsh startup files: https://zsh.sourceforge.io/FAQ/zshfaq03.html#l20
352-
filenames := []string{".zshenv", ".zprofile", ".zlogin"}
355+
// List of zsh startup files: https://zsh.sourceforge.io/Intro/intro_3.html
356+
filenames := []string{".zshenv", ".zprofile", ".zlogin", ".zlogout"}
353357

354358
// zim framework
355359
// https://zimfw.sh/docs/install/
356360
filenames = append(filenames, ".zimrc")
357361

358362
for _, filename := range filenames {
363+
// The userShellrcPath should be set to ZDOTDIR already.
359364
fileOld := filepath.Join(filepath.Dir(s.userShellrcPath), filename)
360365
_, err := os.Stat(fileOld)
361366
if errors.Is(err, fs.ErrNotExist) {
@@ -367,12 +372,11 @@ func (s *DevboxShell) linkShellStartupFiles(shellSettingsDir string) {
367372
}
368373

369374
fileNew := filepath.Join(shellSettingsDir, filename)
370-
371-
if err := os.Link(fileOld, fileNew); err == nil {
372-
debug.Log("Linked shell startup file %s to %s", fileOld, fileNew)
373-
} else {
375+
cmd := exec.Command("cp", fileOld, fileNew)
376+
if err := cmd.Run(); err != nil {
374377
// This is a best-effort operation. If there's an error then log it for visibility but continue.
375-
debug.Log("Error linking zsh setting file from %s to %s: %v", fileOld, fileNew, err)
378+
debug.Log("Error copying zsh setting file from %s to %s: %v", fileOld, fileNew, err)
379+
continue
376380
}
377381
}
378382
}

0 commit comments

Comments
 (0)