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

     1  # Regression test for https://go.dev/issue/51461 and https://go.dev/issue/51483.
     2  #
     3  # When built with -trimpath, runtime.GOROOT() returned the bogus string "go"
     4  # if GOROOT was not set explicitly in the environment.
     5  # It should instead return the empty string, since we know that we don't
     6  # have a valid path to return.
     7  
     8  [trimpath] env GOROOT=
     9  [trimpath] ! go env GOROOT
    10  [trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
    11  [trimpath] env GOROOT=$TESTGO_GOROOT
    12  
    13  [short] stop
    14  
    15  # With GOROOT still set, 'go build' and 'go test -c'
    16  # should cause runtime.GOROOT() to report either the correct GOROOT
    17  # (without -trimpath) or no GOROOT at all (with -trimpath).
    18  
    19  go build -o example.exe .
    20  go build -trimpath -o example-trimpath.exe .
    21  go test -c -o example.test.exe .
    22  go test -trimpath -c -o example.test-trimpath.exe .
    23  
    24  env GOROOT=
    25  
    26  exec ./example.exe
    27  stdout '^GOROOT '$TESTGO_GOROOT'$'
    28  stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    29  
    30  ! exec ./example-trimpath.exe
    31  stdout '^GOROOT $'
    32  stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\n\z'
    33  
    34  exec ./example.test.exe -test.v
    35  stdout '^GOROOT '$TESTGO_GOROOT'$'
    36  stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    37  
    38  ! exec ./example.test-trimpath.exe -test.v
    39  stdout '^GOROOT $'
    40  stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
    41  
    42  # If a correct GOROOT is baked in to the 'go' command itself, 'go run' and
    43  # 'go test' should not implicitly set GOROOT in the process environment
    44  # (because that could mask an unexpected production dependency on the GOROOT
    45  # environment variable), but 'go generate' should (because the generator may
    46  # reasonably expect to be able to locate the GOROOT for which it is generating
    47  # code).
    48  
    49  [trimpath] stop
    50  
    51  ! go run -trimpath .
    52  stdout '^GOROOT $'
    53  stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\nexit status 1\n\z'
    54  
    55  ! go test -trimpath -v .
    56  stdout '^GOROOT $'
    57  stdout 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
    58  
    59  env GOFLAGS=-trimpath
    60  go generate .
    61  stdout '^GOROOT '$TESTGO_GOROOT'$'
    62  stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    63  
    64  -- go.mod --
    65  module example
    66  
    67  go 1.19
    68  -- main.go --
    69  package main
    70  
    71  //go:generate go run .
    72  
    73  import (
    74  	"fmt"
    75  	"go/build"
    76  	"os"
    77  	"runtime"
    78  )
    79  
    80  func main() {
    81  	fmt.Println("GOROOT", runtime.GOROOT())
    82  
    83  	p, err := build.Default.Import("runtime", "", build.FindOnly)
    84  	if err != nil {
    85  		fmt.Fprintln(os.Stderr, err)
    86  		os.Exit(1)
    87  	}
    88  	fmt.Println("runtime", p.Dir)
    89  }
    90  -- main_test.go --
    91  package main
    92  
    93  import "testing"
    94  
    95  func TestMain(*testing.M) {
    96  	main()
    97  }
    98  

View as plain text