...

Text file src/cmd/go/testdata/script/goroot_executable_trimpath.txt

Documentation: cmd/go/testdata/script

     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
    11mkdir $WORK/new/bin
    12mkdir $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
    18# $GOROOT/bin/go is whatever the user has already installed
    19# (using make.bash or similar). We can't make assumptions about what
    20# options it may have been built with, such as -trimpath or not.
    21# Instead, we build a fresh copy of the binary with known settings.
    22go build -trimpath -o $WORK/new/bin/go$GOEXE cmd/go &
    23go build -trimpath -o $WORK/bin/check$GOEXE check.go &
    24wait
    25
    26env TESTGOROOT=$GOROOT
    27env GOROOT=
    28
    29# Unset GOPATH and any variables that its default may be derived from,
    30# so that we can check for a spurious warning.
    31env GOPATH=
    32env HOME=''
    33env USERPROFILE=''
    34env home=''
    35
    36# Relocated Executable
    37# Since we built with -trimpath and the binary isn't installed in a
    38# normal-looking GOROOT, this command should fail.
    39
    40! exec $WORK/new/bin/go$GOEXE env GOROOT
    41stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
    42! stderr 'GOPATH set to GOROOT'
    43
    44# Cross-compiled binaries in cmd are installed to a ${GOOS}_${GOARCH} subdirectory,
    45# so we also want to try a copy there.
    46# (Note that the script engine's 'exec' engine already works around
    47# https://go.dev/issue/22315, so we don't have to do that explicitly in the
    48# 'check' program we use later.)
    49cp $WORK/new/bin/go$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE
    50! exec $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE env GOROOT
    51stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
    52! stderr 'GOPATH set to GOROOT'
    53
    54# Relocated Tree:
    55# If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
    56# so it should find the new tree.
    57mkdir $WORK/new/pkg/tool
    58exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
    59exec $WORK/bin/check$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE $WORK/new
    60! stderr 'GOPATH set to GOROOT'
    61
    62-- check.go --
    63package main
    64
    65import (
    66	"fmt"
    67	"os"
    68	"os/exec"
    69	"path/filepath"
    70	"strings"
    71)
    72
    73func main() {
    74	exe := os.Args[1]
    75	want := os.Args[2]
    76	cmd := exec.Command(exe, "env", "GOROOT")
    77	out, err := cmd.CombinedOutput()
    78	if err != nil {
    79		fmt.Fprintf(os.Stderr, "%s env GOROOT: %v, %s\n", exe, err, out)
    80		os.Exit(1)
    81	}
    82	goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
    83	if err != nil {
    84		fmt.Fprintln(os.Stderr, err)
    85		os.Exit(1)
    86	}
    87	want, err = filepath.EvalSymlinks(want)
    88	if err != nil {
    89		fmt.Fprintln(os.Stderr, err)
    90		os.Exit(1)
    91	}
    92	if !strings.EqualFold(goroot, want) {
    93		fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
    94		os.Exit(1)
    95	}
    96	fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)
    97
    98}

View as plain text