-
Notifications
You must be signed in to change notification settings - Fork 257
Remove stale goenv shims from v2 installations during upgrade to v3 #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
30fffbe
Remove stale goenv shims from v2 installations during upgrade to v3
AdamDrewsTR 8f0fb13
Apply suggestions from code review
ChronosMasterOfAllTime 5356010
consolidate duplicated logic into helper funcs and add tests
ChronosMasterOfAllTime f792f2b
fix windows test failure
ChronosMasterOfAllTime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.