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

View as plain text