Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-nv/goenv/internal/cmdutil"
"github.com/go-nv/goenv/internal/config"
"github.com/go-nv/goenv/internal/manager"
"github.com/go-nv/goenv/internal/migration"
"github.com/go-nv/goenv/internal/utils"
"github.com/go-nv/goenv/internal/vscode"
"github.com/go-nv/goenv/internal/workflow"
Expand Down Expand Up @@ -62,6 +63,12 @@ var RootCmd = &cobra.Command{
// Store updated context back to command
cmd.SetContext(ctx)

// Remove stale goenv shim left over from v2 installations.
// Uses helper function to handle both forward and backslash paths.
if _, err := migration.RemoveStaleV2Shim(cfg.ShimsDir()); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "warning: %v\n", err)
}

// Propagate output options
utils.SetOutputOptions(NoColor, Plain)
},
Expand Down
55 changes: 40 additions & 15 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@

$ErrorActionPreference = "Stop"

# Ensure TLS 1.2 for GitHub API/downloads (older Windows defaults to TLS 1.0)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Configuration
$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { "$HOME\.goenv" }
# Use if/else instead of ?? operator for PowerShell 5.1 compatibility
$defaultHome = if ($env:USERPROFILE) { $env:USERPROFILE } else { $HOME }
$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { Join-Path $defaultHome ".goenv" }
$GITHUB_REPO = "go-nv/goenv"
$INSTALL_DIR = "$GOENV_ROOT\bin"
$INSTALL_DIR = Join-Path $GOENV_ROOT "bin"

# Colors
function Write-ColorOutput($ForegroundColor) {
$fc = $host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = $ForegroundColor
if ($args) {
Write-Output $args
}
$host.UI.RawUI.ForegroundColor = $fc
# Colors — use Write-Host which works in non-interactive/piped contexts
function Write-ColorOutput {
param(
[Parameter(Position=0)]
[System.ConsoleColor]$ForegroundColor,
[Parameter(Position=1, ValueFromRemainingArguments)]
[string[]]$Message
)
Write-Host ($Message -join ' ') -ForegroundColor $ForegroundColor
}

# Detect architecture
Expand Down Expand Up @@ -85,35 +91,54 @@ function Install-Binary {
# Copy binary
$binaryPath = Join-Path $tmpDir "goenv.exe"
if (Test-Path $binaryPath) {
Copy-Item -Path $binaryPath -Destination "$INSTALL_DIR\goenv.exe" -Force
Copy-Item -Path $binaryPath -Destination (Join-Path $INSTALL_DIR "goenv.exe") -Force
} else {
throw "Binary not found in archive"
}

# Copy completions if they exist
$completionsPath = Join-Path $tmpDir "completions"
if (Test-Path $completionsPath) {
$targetCompletions = "$GOENV_ROOT\completions"
$targetCompletions = Join-Path $GOENV_ROOT "completions"
New-Item -ItemType Directory -Path $targetCompletions -Force | Out-Null
Copy-Item -Path "$completionsPath\*" -Destination $targetCompletions -Recurse -Force -ErrorAction SilentlyContinue
}

Write-ColorOutput Green "goenv installed successfully!"

# Remove stale goenv shim from v2 installations.
# v2's goenv-rehash bakes the Cellar/libexec path into shims at creation time.
# Match both forward and backslashes to support all v2 installations.
# Use nested Join-Path for PowerShell 5.1 compatibility (no 3-arg support).
$shimsPath = Join-Path $GOENV_ROOT "shims"
$staleShim = Join-Path $shimsPath "goenv"
if (Test-Path $staleShim) {
$shimContent = Get-Content $staleShim -Raw -ErrorAction SilentlyContinue
if ($shimContent -and $shimContent -match "libexec[\\/]goenv") {
Write-ColorOutput Yellow "Removing stale v2 goenv shim..."
$removed = Remove-Item -Path $staleShim -Force -ErrorAction SilentlyContinue -PassThru
if ($removed) {
Write-ColorOutput Green "Stale shim removed"
} else {
Write-ColorOutput Yellow "Warning: Failed to remove stale v2 goenv shim"
}
}
}
}
catch {
Write-ColorOutput Red "Installation failed: $_"
exit 1
}
finally {
# Cleanup
# Cleanup temp directory
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}

# Auto-configure PowerShell profile
function Setup-PowerShellProfile {
function Initialize-PowerShellProfile {
$profilePath = $PROFILE

# Create profile directory if it doesn't exist
Expand Down Expand Up @@ -184,7 +209,7 @@ function Main {

$version = Get-LatestVersion
Install-Binary -Version $version -Arch $arch
Setup-PowerShellProfile
Initialize-PowerShellProfile
Show-Instructions
}

Expand Down
13 changes: 13 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ install_binary() {
# Cleanup
rm -rf "$tmp_dir"

# Remove stale goenv shim from v2 installations.
# v2's goenv-rehash bakes the Cellar/libexec path into shims at creation time
# (e.g. exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv"). After
# upgrading to v3, the old shim shadows the real v3 binary. We only remove
# it if it contains "libexec/goenv" or "libexec\goenv" — the v2 fingerprint.
if [ -f "$GOENV_ROOT/shims/goenv" ] && grep -qE 'libexec[/\\]goenv' "$GOENV_ROOT/shims/goenv" 2>/dev/null; then
echo -e "${YELLOW}Removing stale v2 goenv shim...${NC}"
if rm -f "$GOENV_ROOT/shims/goenv" 2>/dev/null; then
echo -e "${GREEN}✓ Stale shim removed${NC}"
else
echo -e "${YELLOW}⚠ Warning: Failed to remove stale v2 goenv shim${NC}"
fi

echo -e "${GREEN}✓ goenv installed successfully!${NC}"
}

Expand Down
48 changes: 48 additions & 0 deletions internal/migration/v2shim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package migration

import (
"fmt"
"os"
"path/filepath"
"regexp"
)

// V2ShimPattern matches v2's libexec/goenv path with both forward slashes and backslashes.
// The pattern ensures we match "libexec/goenv" or "libexec\goenv" where "goenv" is the final
// path component (followed by quote, space, or directory separator, but not a hyphen or letter).
var V2ShimPattern = regexp.MustCompile(`libexec[\\/]goenv(["'\s/\\]|$)`)

// RemoveStaleV2Shim removes the stale goenv shim left over from v2 installations.
// v2's goenv-rehash bakes the Homebrew Cellar path into shims at creation time
// (e.g. exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" on macOS/Linux
// or "C:\path\to\libexec\goenv" on Windows). After upgrading to v3, the old shim
// may still point to a deleted path, shadowing the real v3 binary.
//
// We only remove the shim if it contains "libexec/goenv" or "libexec\goenv" —
// the v2 fingerprint — to avoid deleting anything unexpected.
//
// Returns true if the shim was removed, false otherwise.
// If removal fails, it returns an error that can be logged as a warning.
func RemoveStaleV2Shim(shimsDir string) (bool, error) {
goenvShim := filepath.Join(shimsDir, "goenv")

// Read the shim file
data, err := os.ReadFile(goenvShim)
if err != nil {
// File doesn't exist or can't be read - nothing to remove
return false, nil
}

// Check if it contains the v2 fingerprint (supports both / and \)
if !V2ShimPattern.Match(data) {
// Not a v2 shim - leave it alone
return false, nil
}

// Remove the stale shim
if err := os.Remove(goenvShim); err != nil {
return false, fmt.Errorf("failed to remove stale v2 goenv shim %q: %w", goenvShim, err)
}

return true, nil
}
175 changes: 175 additions & 0 deletions internal/migration/v2shim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package migration

import (
"os"
"path/filepath"
"runtime"
"testing"
)

func TestRemoveStaleV2Shim(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "goenv-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

shimsDir := filepath.Join(tmpDir, "shims")
if err := os.MkdirAll(shimsDir, 0755); err != nil {
t.Fatalf("Failed to create shims dir: %v", err)
}

t.Run("removes v2 shim with forward slash", func(t *testing.T) {
// Create a v2 shim with forward slash
shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `#!/usr/bin/env bash
exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}

removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !removed {
t.Error("Expected shim to be removed")
}
if _, err := os.Stat(shimPath); !os.IsNotExist(err) {
t.Error("Shim file should not exist")
}
})

t.Run("removes v2 shim with backslash", func(t *testing.T) {
// Create a v2 shim with backslash (Windows-style)
shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `@echo off
"C:\Program Files\goenv\libexec\goenv" %*
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}

removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !removed {
t.Error("Expected shim to be removed")
}
if _, err := os.Stat(shimPath); !os.IsNotExist(err) {
t.Error("Shim file should not exist")
}
})

t.Run("does not remove non-v2 shim", func(t *testing.T) {
// Create a non-v2 shim
shimPath := filepath.Join(shimsDir, "goenv")
v3Content := `#!/usr/bin/env bash
# This is a v3 shim
exec "goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v3Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}

removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if removed {
t.Error("Expected shim to NOT be removed")
}
if _, err := os.Stat(shimPath); os.IsNotExist(err) {
t.Error("Shim file should still exist")
}
})

t.Run("handles missing shim file", func(t *testing.T) {
// Ensure no shim exists
shimPath := filepath.Join(shimsDir, "goenv")
os.Remove(shimPath)

removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if removed {
t.Error("Expected no removal when file doesn't exist")
}
})

t.Run("returns error when removal fails", func(t *testing.T) {
// Skip on Windows - Unix permission model doesn't apply
if runtime.GOOS == "windows" {
t.Skip("Skipping permission test on Windows - different permission model")
}

shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `#!/usr/bin/env bash
exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}

// Make directory read-only (Unix-like systems only)
if err := os.Chmod(shimsDir, 0555); err != nil {
t.Skipf("Cannot test permission error: %v", err)
}
defer os.Chmod(shimsDir, 0755) // Restore permissions

removed, err := RemoveStaleV2Shim(shimsDir)
if err == nil {
t.Error("Expected error when removal fails")
}
if removed {
t.Error("Should not report as removed when error occurs")
}
})
}

func TestV2ShimPattern(t *testing.T) {
tests := []struct {
name string
content string
matches bool
}{
{
name: "forward slash",
content: `exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"`,
matches: true,
},
{
name: "backslash",
content: `"C:\Program Files\goenv\libexec\goenv" %*`,
matches: true,
},
{
name: "no match",
content: `exec "goenv" "$@"`,
matches: false,
},
{
name: "libexec only",
content: `exec "/usr/local/libexec/goenv-other" "$@"`,
matches: false,
},
{
name: "goenv only",
content: `exec "/usr/local/bin/goenv" "$@"`,
matches: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matches := V2ShimPattern.MatchString(tt.content)
if matches != tt.matches {
t.Errorf("Expected match=%v, got match=%v for content: %s", tt.matches, matches, tt.content)
}
})
}
}
15 changes: 15 additions & 0 deletions scripts/swap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"runtime"

"github.com/go-nv/goenv/internal/migration"
"github.com/go-nv/goenv/internal/platform"
"github.com/go-nv/goenv/internal/utils"
)
Expand Down Expand Up @@ -407,6 +408,20 @@ Options:
swapGoenvBinary(target)
}

// Remove stale goenv shim from v2 that may shadow the new binary.
// Uses helper function to handle both forward and backslash paths.
goenvRoot := os.Getenv("GOENV_ROOT")
if goenvRoot == "" {
homeDir, _ := os.UserHomeDir()
goenvRoot = filepath.Join(homeDir, ".goenv")
}
shimsDir := filepath.Join(goenvRoot, "shims")
if removed, err := migration.RemoveStaleV2Shim(shimsDir); err != nil {
warn(err.Error())
} else if removed {
success("Removed stale v2 goenv shim from " + filepath.Join(shimsDir, "goenv"))
}

Comment thread
ChronosMasterOfAllTime marked this conversation as resolved.
fmt.Println()
success("Switch successful!")
warn("IMPORTANT: Reload your shell before testing:")
Expand Down
Loading