Skip to content

Commit ae66cb5

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

4 files changed

Lines changed: 113 additions & 14 deletions

File tree

cmd/root.go

Lines changed: 14 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,19 @@ 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 (e.g. exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv").
69+
// After upgrading to v3, the old shim may still point to a deleted Cellar
70+
// path, shadowing the real v3 binary. We only remove it if it contains
71+
// "libexec/goenv" — the v2 fingerprint — to avoid deleting anything unexpected.
72+
goenvShim := filepath.Join(cfg.ShimsDir(), "goenv")
73+
if data, err := os.ReadFile(goenvShim); err == nil {
74+
if strings.Contains(string(data), "libexec/goenv") {
75+
_ = os.Remove(goenvShim)
76+
}
77+
}
78+
6579
// Propagate output options
6680
utils.SetOutputOptions(NoColor, Plain)
6781
},

install.ps1

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33

44
$ErrorActionPreference = "Stop"
55

6+
# Ensure TLS 1.2 for GitHub API/downloads (older Windows defaults to TLS 1.0)
7+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
8+
69
# Configuration
7-
$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { "$HOME\.goenv" }
10+
$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { Join-Path ($env:USERPROFILE ?? $HOME) ".goenv" }
811
$GITHUB_REPO = "go-nv/goenv"
9-
$INSTALL_DIR = "$GOENV_ROOT\bin"
12+
$INSTALL_DIR = Join-Path $GOENV_ROOT "bin"
1013

11-
# Colors
12-
function Write-ColorOutput($ForegroundColor) {
13-
$fc = $host.UI.RawUI.ForegroundColor
14-
$host.UI.RawUI.ForegroundColor = $ForegroundColor
15-
if ($args) {
16-
Write-Output $args
17-
}
18-
$host.UI.RawUI.ForegroundColor = $fc
14+
# Colors — use Write-Host which works in non-interactive/piped contexts
15+
function Write-ColorOutput {
16+
param(
17+
[Parameter(Position=0)]
18+
[System.ConsoleColor]$ForegroundColor,
19+
[Parameter(Position=1, ValueFromRemainingArguments)]
20+
[string[]]$Message
21+
)
22+
Write-Host ($Message -join ' ') -ForegroundColor $ForegroundColor
1923
}
2024

2125
# Detect architecture
@@ -85,34 +89,84 @@ function Install-Binary {
8589
# Copy binary
8690
$binaryPath = Join-Path $tmpDir "goenv.exe"
8791
if (Test-Path $binaryPath) {
88-
Copy-Item -Path $binaryPath -Destination "$INSTALL_DIR\goenv.exe" -Force
92+
Copy-Item -Path $binaryPath -Destination (Join-Path $INSTALL_DIR "goenv.exe") -Force
8993
} else {
9094
throw "Binary not found in archive"
9195
}
9296

9397
# Copy completions if they exist
9498
$completionsPath = Join-Path $tmpDir "completions"
9599
if (Test-Path $completionsPath) {
96-
$targetCompletions = "$GOENV_ROOT\completions"
100+
$targetCompletions = Join-Path $GOENV_ROOT "completions"
97101
New-Item -ItemType Directory -Path $targetCompletions -Force | Out-Null
98102
Copy-Item -Path "$completionsPath\*" -Destination $targetCompletions -Recurse -Force -ErrorAction SilentlyContinue
99103
}
100104

101105
Write-ColorOutput Green "goenv installed successfully!"
106+
107+
# Remove stale goenv shim from v2 installations.
108+
# v2's goenv-rehash bakes the Cellar/libexec path into shims at creation time.
109+
# Only remove if it contains "libexec/goenv" — the v2 fingerprint.
110+
$staleShim = Join-Path $GOENV_ROOT "shims" "goenv"
111+
if (Test-Path $staleShim) {
112+
$shimContent = Get-Content $staleShim -Raw -ErrorAction SilentlyContinue
113+
if ($shimContent -and $shimContent -match "libexec/goenv") {
114+
Write-ColorOutput Yellow "Removing stale v2 goenv shim..."
115+
Remove-Item -Path $staleShim -Force -ErrorAction SilentlyContinue
116+
Write-ColorOutput Green "Stale shim removed"
117+
}
118+
}
102119
}
103120
catch {
104121
Write-ColorOutput Red "Installation failed: $_"
105122
exit 1
106123
}
107124
finally {
108-
# Cleanup
125+
# Cleanup temp directory
109126
if (Test-Path $tmpDir) {
110127
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
111128
}
112129
}
113130
}
114131

115-
# Print setup instructions
132+
# Auto-configure PowerShell profile
133+
function Initialize-PowerShellProfile {
134+
$profilePath = $PROFILE
135+
136+
# Create profile directory if it doesn't exist
137+
$profileDir = Split-Path -Parent $profilePath
138+
if (-not (Test-Path $profileDir)) {
139+
New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
140+
}
141+
142+
# Create profile file if it doesn't exist
143+
if (-not (Test-Path $profilePath)) {
144+
New-Item -ItemType File -Path $profilePath -Force | Out-Null
145+
}
146+
147+
# Check if goenv is already configured
148+
$profileContent = Get-Content $profilePath -Raw -ErrorAction SilentlyContinue
149+
if ($profileContent -and $profileContent -match "goenv init") {
150+
Write-ColorOutput Green "goenv is already configured in $profilePath"
151+
return
152+
}
153+
154+
# Add goenv configuration with comment marker
155+
Write-ColorOutput Yellow "Adding goenv configuration to $profilePath..."
156+
157+
$goenvConfig = @"
158+
159+
# goenv - Go version manager (auto-configured by installer)
160+
`$env:GOENV_ROOT = "`$HOME\.goenv"
161+
`$env:PATH = "`$env:GOENV_ROOT\bin;`$env:PATH"
162+
& goenv init - | Invoke-Expression
163+
"@
164+
165+
Add-Content -Path $profilePath -Value $goenvConfig
166+
Write-ColorOutput Green "PowerShell profile configured successfully!"
167+
}
168+
169+
# Print setup completion message
116170
function Show-Instructions {
117171
$profilePath = $PROFILE
118172

@@ -158,6 +212,7 @@ function Main {
158212

159213
$version = Get-LatestVersion
160214
Install-Binary -Version $version -Arch $arch
215+
Initialize-PowerShellProfile
161216
Show-Instructions
162217
}
163218

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+
# (e.g. exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv"). After
128+
# upgrading to v3, the old shim shadows the real v3 binary. We only remove
129+
# it if it contains "libexec/goenv" — the v2 fingerprint.
130+
if [ -f "$GOENV_ROOT/shims/goenv" ] && grep -q 'libexec/goenv' "$GOENV_ROOT/shims/goenv" 2>/dev/null; then
131+
echo -e "${YELLOW}Removing stale v2 goenv shim...${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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"path/filepath"
99
"runtime"
10+
"strings"
1011

1112
"github.com/go-nv/goenv/internal/platform"
1213
"github.com/go-nv/goenv/internal/utils"
@@ -407,6 +408,24 @@ Options:
407408
swapGoenvBinary(target)
408409
}
409410

411+
// Remove stale goenv shim from v2 that may shadow the new binary.
412+
// Only remove if it contains "libexec/goenv" — the v2 fingerprint.
413+
goenvRoot := os.Getenv("GOENV_ROOT")
414+
if goenvRoot == "" {
415+
homeDir, _ := os.UserHomeDir()
416+
goenvRoot = filepath.Join(homeDir, ".goenv")
417+
}
418+
staleShim := filepath.Join(goenvRoot, "shims", "goenv")
419+
if data, err := os.ReadFile(staleShim); err == nil {
420+
if strings.Contains(string(data), "libexec/goenv") {
421+
if err := os.Remove(staleShim); err == nil {
422+
success("Removed stale v2 goenv shim from " + staleShim)
423+
} else {
424+
warn(fmt.Sprintf("Could not remove stale shim %s: %v", staleShim, err))
425+
}
426+
}
427+
}
428+
410429
fmt.Println()
411430
success("Switch successful!")
412431
warn("IMPORTANT: Reload your shell before testing:")

0 commit comments

Comments
 (0)