Skip to content

Commit 3ba1274

Browse files
fix: handle os.UserHomeDir errors across all packages
Check os.UserHomeDir() return error instead of silently ignoring it. Prevents operations in wrong directory paths when home directory lookup fails (containers, restricted environments). Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent dd7169f commit 3ba1274

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

internal/brew/brew.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ func CheckNetwork() error {
553553

554554
func CheckDiskSpace() (float64, error) {
555555
var stat syscall.Statfs_t
556-
home, _ := os.UserHomeDir()
556+
home, err := os.UserHomeDir()
557+
if err != nil {
558+
return 0, fmt.Errorf("cannot determine home directory: %w", err)
559+
}
557560
if err := syscall.Statfs(home, &stat); err != nil {
558561
return 0, err
559562
}

internal/cli/doctor.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,14 @@ func checkGit() []checkResult {
171171
func checkShell() []checkResult {
172172
var results []checkResult
173173

174-
home, _ := os.UserHomeDir()
174+
home, err := os.UserHomeDir()
175+
if err != nil {
176+
return []checkResult{{
177+
name: "Shell",
178+
status: "error",
179+
message: "cannot determine home directory",
180+
}}
181+
}
175182
omzPath := filepath.Join(home, ".oh-my-zsh")
176183

177184
if _, err := os.Stat(omzPath); os.IsNotExist(err) {

internal/dotfiles/dotfiles.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ func Clone(repoURL string, dryRun bool) error {
1515
return nil
1616
}
1717

18-
home, _ := os.UserHomeDir()
18+
home, err := os.UserHomeDir()
19+
if err != nil {
20+
return fmt.Errorf("cannot determine home directory: %w", err)
21+
}
1922
dotfilesPath := filepath.Join(home, defaultDotfilesDir)
2023

2124
if _, err := os.Stat(dotfilesPath); err == nil {
@@ -35,7 +38,10 @@ func Clone(repoURL string, dryRun bool) error {
3538
}
3639

3740
func Link(dryRun bool) error {
38-
home, _ := os.UserHomeDir()
41+
home, err := os.UserHomeDir()
42+
if err != nil {
43+
return fmt.Errorf("cannot determine home directory: %w", err)
44+
}
3945
dotfilesPath := filepath.Join(home, defaultDotfilesDir)
4046

4147
if _, err := os.Stat(dotfilesPath); os.IsNotExist(err) {
@@ -75,7 +81,10 @@ func linkWithStow(dotfilesPath string, dryRun bool) error {
7581
return err
7682
}
7783

78-
home, _ := os.UserHomeDir()
84+
home, err := os.UserHomeDir()
85+
if err != nil {
86+
return fmt.Errorf("cannot determine home directory: %w", err)
87+
}
7988

8089
for _, entry := range entries {
8190
if !entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
@@ -108,7 +117,10 @@ func linkWithStow(dotfilesPath string, dryRun bool) error {
108117
}
109118

110119
func linkDirect(dotfilesPath string, dryRun bool) error {
111-
home, _ := os.UserHomeDir()
120+
home, err := os.UserHomeDir()
121+
if err != nil {
122+
return fmt.Errorf("cannot determine home directory: %w", err)
123+
}
112124

113125
entries, err := os.ReadDir(dotfilesPath)
114126
if err != nil {

internal/macos/macos.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ var DefaultPreferences = []Preference{
5050

5151
func expandHome(path string) string {
5252
if strings.HasPrefix(path, "~/") {
53-
home, _ := os.UserHomeDir()
53+
home, err := os.UserHomeDir()
54+
if err != nil {
55+
return path
56+
}
5457
return filepath.Join(home, path[2:])
5558
}
5659
return path
@@ -89,7 +92,10 @@ func Configure(prefs []Preference, dryRun bool) error {
8992
}
9093

9194
func CreateScreenshotsDir(dryRun bool) error {
92-
home, _ := os.UserHomeDir()
95+
home, err := os.UserHomeDir()
96+
if err != nil {
97+
return fmt.Errorf("cannot determine home directory: %w", err)
98+
}
9399
dir := filepath.Join(home, "Screenshots")
94100

95101
if dryRun {
@@ -109,8 +115,9 @@ func RestartAffectedApps(dryRun bool) error {
109115
continue
110116
}
111117

118+
// killall returns an error if the app isn't running, which is expected and safe to ignore
112119
cmd := exec.Command("killall", app)
113-
cmd.Run()
120+
cmd.Run() //nolint:errcheck // non-fatal: app may not be running
114121
}
115122

116123
return nil

internal/shell/shell.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
)
99

1010
func IsOhMyZshInstalled() bool {
11-
home, _ := os.UserHomeDir()
12-
_, err := os.Stat(filepath.Join(home, ".oh-my-zsh"))
11+
home, err := os.UserHomeDir()
12+
if err != nil {
13+
return false
14+
}
15+
_, err = os.Stat(filepath.Join(home, ".oh-my-zsh"))
1316
return err == nil
1417
}
1518

@@ -31,15 +34,21 @@ func InstallOhMyZsh(dryRun bool) error {
3134
return err
3235
}
3336

34-
home, _ := os.UserHomeDir()
37+
home, err := os.UserHomeDir()
38+
if err != nil {
39+
return fmt.Errorf("cannot determine home directory: %w", err)
40+
}
3541
zshrcPath := filepath.Join(home, ".zshrc")
3642
os.Remove(zshrcPath)
3743

3844
return nil
3945
}
4046

4147
func ConfigureZshrc(dryRun bool) error {
42-
home, _ := os.UserHomeDir()
48+
home, err := os.UserHomeDir()
49+
if err != nil {
50+
return fmt.Errorf("cannot determine home directory: %w", err)
51+
}
4352
zshrcPath := filepath.Join(home, ".zshrc")
4453

4554
additions := `

0 commit comments

Comments
 (0)