-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathv2shim.go
More file actions
48 lines (41 loc) · 1.68 KB
/
Copy pathv2shim.go
File metadata and controls
48 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
}