Skip to content

[shell] respect ZDOTDIR for zsh startup files, and copy startup files instead of linking #1587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions internal/impl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ func initShellBinaryFields(path string) *DevboxShell {
shell.userShellrcPath = rcfilePath(".bashrc")
case "zsh":
shell.name = shZsh
shell.userShellrcPath = rcfilePath(".zshrc")
if zdotdir := os.Getenv("ZDOTDIR"); zdotdir != "" {
shell.userShellrcPath = filepath.Join(os.ExpandEnv(zdotdir), ".zshrc")
} else {
shell.userShellrcPath = rcfilePath(".zshrc")
}
case "ksh":
shell.name = shKsh
shell.userShellrcPath = rcfilePath(".kshrc")
Expand Down Expand Up @@ -357,14 +361,15 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
func (s *DevboxShell) linkShellStartupFiles(shellSettingsDir string) {
// For now, we only need to do this for zsh shell
if s.name == shZsh {
// Useful explanation of zsh startup files: https://zsh.sourceforge.io/FAQ/zshfaq03.html#l20
filenames := []string{".zshenv", ".zprofile", ".zlogin"}
// List of zsh startup files: https://zsh.sourceforge.io/Intro/intro_3.html
filenames := []string{".zshenv", ".zprofile", ".zlogin", ".zlogout"}

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

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

fileNew := filepath.Join(shellSettingsDir, filename)

if err := os.Link(fileOld, fileNew); err == nil {
debug.Log("Linked shell startup file %s to %s", fileOld, fileNew)
} else {
cmd := exec.Command("cp", fileOld, fileNew)
if err := cmd.Run(); err != nil {
// This is a best-effort operation. If there's an error then log it for visibility but continue.
debug.Log("Error linking zsh setting file from %s to %s: %v", fileOld, fileNew, err)
debug.Log("Error copying zsh setting file from %s to %s: %v", fileOld, fileNew, err)
continue
}
}
}
Expand Down