|
| 1 | +# Regression test for https://go.dev/issue/62119: |
| 2 | +# A 'go' command cross-compiled with a different GOHOSTOS |
| 3 | +# should be able to locate its GOROOT using os.Executable. |
| 4 | +# |
| 5 | +# (This also tests a 'go' command built with -trimpath |
| 6 | +# that is not cross-compiled, since we need to build that |
| 7 | +# configuration for the test anyway.) |
| 8 | + |
| 9 | +[short] skip 'builds and links another cmd/go' |
| 10 | + |
| 11 | +mkdir $WORK/new/bin |
| 12 | +mkdir $WORK/new/bin/${GOOS}_${GOARCH} |
| 13 | + |
| 14 | +# In this test, we are specifically checking the logic for deriving |
| 15 | +# the value of GOROOT from os.Executable when runtime.GOROOT is |
| 16 | +# trimmed away. |
| 17 | +# GOROOT_FINAL changes the default behavior of runtime.GOROOT, |
| 18 | +# so we explicitly clear it to remove it as a confounding variable. |
| 19 | +env GOROOT_FINAL= |
| 20 | + |
| 21 | +# $GOROOT/bin/go is whatever the user has already installed |
| 22 | +# (using make.bash or similar). We can't make assumptions about what |
| 23 | +# options it may have been built with, such as -trimpath or GOROOT_FINAL. |
| 24 | +# Instead, we build a fresh copy of the binary with known settings. |
| 25 | +go build -trimpath -o $WORK/new/bin/go$GOEXE cmd/go & |
| 26 | +go build -trimpath -o $WORK/bin/check$GOEXE check.go & |
| 27 | +wait |
| 28 | + |
| 29 | +env TESTGOROOT=$GOROOT |
| 30 | +env GOROOT= |
| 31 | + |
| 32 | +# Relocated Executable |
| 33 | +# Since we built with -trimpath and the binary isn't installed in a |
| 34 | +# normal-looking GOROOT, this command should fail. |
| 35 | + |
| 36 | +! exec $WORK/new/bin/go$GOEXE env GOROOT |
| 37 | +stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$' |
| 38 | + |
| 39 | +# Cross-compiled binaries in cmd are installed to a ${GOOS}_${GOARCH} subdirectory, |
| 40 | +# so we also want to try a copy there. |
| 41 | +# (Note that the script engine's 'exec' engine already works around |
| 42 | +# https://go.dev/issue/22315, so we don't have to do that explicitly in the |
| 43 | +# 'check' program we use later.) |
| 44 | +cp $WORK/new/bin/go$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE |
| 45 | +! exec $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE env GOROOT |
| 46 | +stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$' |
| 47 | + |
| 48 | +# Relocated Tree: |
| 49 | +# If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT, |
| 50 | +# so it should find the new tree. |
| 51 | +mkdir $WORK/new/pkg/tool |
| 52 | +exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new |
| 53 | +exec $WORK/bin/check$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE $WORK/new |
| 54 | + |
| 55 | +-- check.go -- |
| 56 | +package main |
| 57 | + |
| 58 | +import ( |
| 59 | + "fmt" |
| 60 | + "os" |
| 61 | + "os/exec" |
| 62 | + "path/filepath" |
| 63 | + "strings" |
| 64 | +) |
| 65 | + |
| 66 | +func main() { |
| 67 | + exe := os.Args[1] |
| 68 | + want := os.Args[2] |
| 69 | + cmd := exec.Command(exe, "env", "GOROOT") |
| 70 | + out, err := cmd.CombinedOutput() |
| 71 | + if err != nil { |
| 72 | + fmt.Fprintf(os.Stderr, "%s env GOROOT: %v, %s\n", exe, err, out) |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out))) |
| 76 | + if err != nil { |
| 77 | + fmt.Fprintln(os.Stderr, err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + want, err = filepath.EvalSymlinks(want) |
| 81 | + if err != nil { |
| 82 | + fmt.Fprintln(os.Stderr, err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + if !strings.EqualFold(goroot, want) { |
| 86 | + fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want) |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | + fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot) |
| 90 | + |
| 91 | +} |
0 commit comments