Skip to content

Commit 661d0f9

Browse files
brosutimvw
authored andcommitted
fix: respect ZDOTDIR for zsh init
1 parent 0c58a2c commit 661d0f9

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ After running `wt init`, restart your shell or run:
105105

106106
```bash
107107
source ~/.bashrc # for bash
108-
source ~/.zshrc # for zsh
108+
source "${ZDOTDIR:-$HOME}/.zshrc" # for zsh
109109
```
110110

111111
Shell integration enables:

init.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var initCmd = &cobra.Command{
3232
3333
Automatically detects your shell and updates the appropriate config file:
3434
- bash: ~/.bashrc
35-
- zsh: ~/.zshrc
35+
- zsh: ~/.zshrc (or $ZDOTDIR/.zshrc if ZDOTDIR is set)
3636
- powershell: $PROFILE (Windows only)
3737
3838
The configuration is wrapped in markers so it can be safely updated or removed.
@@ -135,6 +135,14 @@ func getShellConfigPath(shell string) string {
135135
}
136136
return bashrc
137137
case "zsh":
138+
// Respect ZDOTDIR if set: zsh reads its startup files from $ZDOTDIR (default: $HOME).
139+
// This avoids writing to the wrong (potentially unused or broken) ~/.zshrc.
140+
if zdotdir := strings.TrimSpace(os.Getenv("ZDOTDIR")); zdotdir != "" {
141+
if !filepath.IsAbs(zdotdir) {
142+
zdotdir = filepath.Join(home, zdotdir)
143+
}
144+
return filepath.Join(filepath.Clean(zdotdir), ".zshrc")
145+
}
138146
return filepath.Join(home, ".zshrc")
139147
case "powershell":
140148
// Check $PROFILE env var first (works for both Windows PowerShell 5.1 and PowerShell Core)
@@ -258,9 +266,9 @@ func installShellConfig(configPath, shell string, dryRun, noPrompt bool) error {
258266
fmt.Println("To activate, run:")
259267
switch shell {
260268
case "bash":
261-
fmt.Println(" source ~/.bashrc")
269+
fmt.Printf(" source %s\n", configPath)
262270
case "zsh":
263-
fmt.Println(" source ~/.zshrc")
271+
fmt.Printf(" source %s\n", configPath)
264272
case "powershell":
265273
fmt.Println(" . $PROFILE")
266274
}

init_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func TestGetShellConfigPath(t *testing.T) {
9797
t.Fatalf("Failed to get home dir: %v", err)
9898
}
9999

100+
// Ensure tests are stable even when the developer machine has ZDOTDIR set.
101+
origZdotdir := os.Getenv("ZDOTDIR")
102+
os.Setenv("ZDOTDIR", "")
103+
t.Cleanup(func() { os.Setenv("ZDOTDIR", origZdotdir) })
104+
100105
tests := []struct {
101106
name string
102107
shell string
@@ -119,6 +124,25 @@ func TestGetShellConfigPath(t *testing.T) {
119124
}
120125
}
121126

127+
func TestGetShellConfigPathZshRespectsZdotdir(t *testing.T) {
128+
home, err := os.UserHomeDir()
129+
if err != nil {
130+
t.Fatalf("Failed to get home dir: %v", err)
131+
}
132+
133+
orig := os.Getenv("ZDOTDIR")
134+
t.Cleanup(func() { os.Setenv("ZDOTDIR", orig) })
135+
136+
zdotdir := filepath.Join(home, ".config", "zsh")
137+
os.Setenv("ZDOTDIR", zdotdir)
138+
139+
got := getShellConfigPath("zsh")
140+
want := filepath.Join(zdotdir, ".zshrc")
141+
if got != want {
142+
t.Fatalf("getShellConfigPath(%q) = %q, want %q", "zsh", got, want)
143+
}
144+
}
145+
122146
func TestGetShellConfigContent(t *testing.T) {
123147
tests := []struct {
124148
name string

scripts/postinstall.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ echo "wt has been installed successfully!"
3333
echo ""
3434
echo "To activate shell integration in your current session:"
3535
echo " source ~/.bashrc # for bash"
36-
echo " source ~/.zshrc # for zsh"
36+
echo " source \"${ZDOTDIR:-$HOME}/.zshrc\" # for zsh"
3737
echo ""
3838
echo "Or simply start a new terminal session."

0 commit comments

Comments
 (0)