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

     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  
    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.
    22  go build -trimpath -o $WORK/new/bin/go$GOEXE cmd/go &
    23  go build -trimpath -o $WORK/bin/check$GOEXE check.go &
    24  wait
    25  
    26  env TESTGOROOT=$GOROOT
    27  env 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.
    31  env GOPATH=
    32  env HOME=''
    33  env USERPROFILE=''
    34  env 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
    41  stderr '^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.)
    49  cp $WORK/new/bin/go$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE
    50  ! exec $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE env GOROOT
    51  stderr '^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.
    57  mkdir $WORK/new/pkg/tool
    58  exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
    59  exec $WORK/bin/check$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE $WORK/new
    60  ! stderr 'GOPATH set to GOROOT'
    61  
    62  -- check.go --
    63  package main
    64  
    65  import (
    66  	"fmt"
    67  	"os"
    68  	"os/exec"
    69  	"path/filepath"
    70  	"strings"
    71  )
    72  
    73  func 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  }
    99  

View as plain text