Skip to content

Commit 0dbf728

Browse files
committed
Remove stale goenv shims from v2 installations during upgrade to v3
1 parent f07bf43 commit 0dbf728

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

cmd/root.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"strings"
78

89
"github.com/go-nv/goenv/internal/cmdutil"
@@ -62,6 +63,17 @@ var RootCmd = &cobra.Command{
6263
// Store updated context back to command
6364
cmd.SetContext(ctx)
6465

66+
// Remove stale goenv shim left over from v2.
67+
// v2's goenv-rehash bakes the Homebrew Cellar path into shims at creation
68+
// time. After upgrading to v3, an old $GOENV_ROOT/shims/goenv may still
69+
// point to a deleted Cellar path, shadowing the real v3 binary. v3 never
70+
// creates a goenv shim (it skips itself in Rehash), so any shim present
71+
// is stale and safe to remove.
72+
goenvShim := filepath.Join(cfg.ShimsDir(), "goenv")
73+
if _, err := os.Stat(goenvShim); err == nil {
74+
_ = os.Remove(goenvShim)
75+
}
76+
6577
// Propagate output options
6678
utils.SetOutputOptions(NoColor, Plain)
6779
},

install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ install_binary() {
122122
# Cleanup
123123
rm -rf "$tmp_dir"
124124

125+
# Remove stale goenv shim from v2 installations
126+
# v2's goenv-rehash bakes the Homebrew Cellar path into shims at creation time.
127+
# When upgrading to v3, the old shim at $GOENV_ROOT/shims/goenv may still point
128+
# to a deleted Cellar path (e.g. /opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv),
129+
# which shadows the real v3 binary and causes "No such file or directory" errors.
130+
if [ -f "$GOENV_ROOT/shims/goenv" ]; then
131+
echo -e "${YELLOW}Removing stale goenv shim from previous installation...${NC}"
132+
rm -f "$GOENV_ROOT/shims/goenv"
133+
echo -e "${GREEN}✓ Stale shim removed${NC}"
134+
fi
135+
125136
echo -e "${GREEN}✓ goenv installed successfully!${NC}"
126137
}
127138

scripts/swap/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,21 @@ Options:
407407
swapGoenvBinary(target)
408408
}
409409

410+
// Remove stale goenv shim from v2 that may shadow the new binary
411+
goenvRoot := os.Getenv("GOENV_ROOT")
412+
if goenvRoot == "" {
413+
homeDir, _ := os.UserHomeDir()
414+
goenvRoot = filepath.Join(homeDir, ".goenv")
415+
}
416+
staleShim := filepath.Join(goenvRoot, "shims", "goenv")
417+
if utils.FileExists(staleShim) {
418+
if err := os.Remove(staleShim); err == nil {
419+
success("Removed stale goenv shim from " + staleShim)
420+
} else {
421+
warn(fmt.Sprintf("Could not remove stale shim %s: %v", staleShim, err))
422+
}
423+
}
424+
410425
fmt.Println()
411426
success("Switch successful!")
412427
warn("IMPORTANT: Reload your shell before testing:")

0 commit comments

Comments
 (0)