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