...
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
11env 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
24go build -o example.exe .
25go build -trimpath -o example-trimpath.exe .
26go test -c -o example.test.exe .
27go test -trimpath -c -o example.test-trimpath.exe .
28
29env GOROOT=
30
31exec ./example.exe
32stdout '^GOROOT '$TESTGO_GOROOT'$'
33stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
34
35! exec ./example-trimpath.exe
36stdout '^GOROOT $'
37stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\n\z'
38
39exec ./example.test.exe -test.v
40stdout '^GOROOT '$TESTGO_GOROOT'$'
41stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
42
43! exec ./example.test-trimpath.exe -test.v
44stdout '^GOROOT $'
45stderr '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 .
58stdout '^GOROOT $'
59stderr '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 .
62stdout '^GOROOT $'
63stdout 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
64
65env GOFLAGS=-trimpath
66go generate .
67stdout '^GOROOT '$TESTGO_GOROOT'$'
68stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
69
70-- go.mod --
71module example
72
73go 1.19
74-- main.go --
75package main
76
77//go:generate go run .
78
79import (
80 "fmt"
81 "go/build"
82 "os"
83 "runtime"
84)
85
86func 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 --
97package main
98
99import "testing"
100
101func TestMain(*testing.M) {
102 main()
103}
View as plain text