1 [short] skip
2
3 # If GOROOT_FINAL is set, 'go build -trimpath' bakes that into the resulting
4 # binary instead of GOROOT. Explicitly unset it here.
5 env GOROOT_FINAL=
6
7 # Set up two identical directories that can be used as GOPATH.
8 env GO111MODULE=on
9 mkdir $WORK/a/src/paths $WORK/b/src/paths
10 cp paths.go $WORK/a/src/paths
11 cp paths.go $WORK/b/src/paths
12 cp overlay.json $WORK/a/src/paths
13 cp overlay.json $WORK/b/src/paths
14 cp go.mod $WORK/a/src/paths/
15 cp go.mod $WORK/b/src/paths/
16
17
18 # A binary built without -trimpath should contain the module root dir
19 # and GOROOT for debugging and stack traces.
20 cd $WORK/a/src/paths
21 go build -o $WORK/paths-dbg.exe .
22 exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
23 stdout 'binary contains module root: true'
24 stdout 'binary contains GOROOT: true'
25
26 # A binary built with -trimpath should not contain the current workspace
27 # or GOROOT.
28 go build -trimpath -o $WORK/paths-a.exe .
29 exec $WORK/paths-a.exe $WORK/paths-a.exe
30 stdout 'binary contains module root: false'
31 stdout 'binary contains GOROOT: false'
32
33 # A binary from an external module built with -trimpath should not contain
34 # the current workspace or GOROOT.
35 go get rsc.io/fortune
36 go install -trimpath rsc.io/fortune
37 exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
38 stdout 'binary contains module root: false'
39 stdout 'binary contains GOROOT: false'
40 go mod edit -droprequire rsc.io/fortune
41
42 # Two binaries built from identical packages in different directories
43 # should be identical.
44 cd $WORK/b/src/paths
45 go build -trimpath -o $WORK/paths-b.exe
46 cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
47
48
49 # Same sequence of tests but with overlays.
50 # A binary built without -trimpath should contain the module root dir
51 # and GOROOT for debugging and stack traces.
52 cd $WORK/a/src/paths
53 go build -overlay overlay.json -o $WORK/paths-dbg.exe ./overlaydir
54 exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
55 stdout 'binary contains module root: true'
56 stdout 'binary contains GOROOT: true'
57
58 # A binary built with -trimpath should not contain the current workspace
59 # or GOROOT.
60 go build -overlay overlay.json -trimpath -o $WORK/paths-a.exe ./overlaydir
61 exec $WORK/paths-a.exe $WORK/paths-a.exe
62 stdout 'binary contains module root: false'
63 stdout 'binary contains GOROOT: false'
64
65 # Two binaries built from identical packages in different directories
66 # should be identical.
67 cd $WORK/b/src/paths
68 go build -overlay overlay.json -trimpath -o $WORK/paths-b.exe ./overlaydir
69 cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
70
71
72 # Same sequence of tests but in GOPATH mode.
73 # A binary built without -trimpath should contain GOPATH and GOROOT.
74 env GO111MODULE=off
75 cd $WORK
76 env GOPATH=$WORK/a
77 go build -o paths-dbg.exe paths
78 exec ./paths-dbg.exe paths-dbg.exe
79 stdout 'binary contains GOPATH: true'
80 stdout 'binary contains GOROOT: true'
81
82 # A binary built with -trimpath should not contain GOPATH or GOROOT.
83 go build -trimpath -o paths-a.exe paths
84 exec ./paths-a.exe paths-a.exe
85 stdout 'binary contains GOPATH: false'
86 stdout 'binary contains GOROOT: false'
87
88 # Two binaries built from identical packages in different GOPATH roots
89 # should be identical.
90 env GOPATH=$WORK/b
91 go build -trimpath -o paths-b.exe paths
92 cmp -q paths-a.exe paths-b.exe
93
94
95 # Same sequence of tests but with gccgo.
96 # gccgo does not support builds in module mode.
97 [!exec:gccgo] stop
98 env GOPATH=$WORK/a
99
100 # A binary built with gccgo without -trimpath should contain the current
101 # GOPATH and GOROOT.
102 go build -compiler=gccgo -o paths-dbg.exe paths
103 exec ./paths-dbg.exe paths-dbg.exe
104 stdout 'binary contains GOPATH: true'
105 stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
106
107 # A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
108 go build -compiler=gccgo -trimpath -o paths-a.exe paths
109 exec ./paths-a.exe paths-a.exe
110 stdout 'binary contains GOPATH: false'
111 stdout 'binary contains GOROOT: false'
112
113 # Two binaries built from identical packages in different directories
114 # should be identical.
115 env GOPATH=$WORK/b
116 go build -compiler=gccgo -trimpath -o paths-b.exe paths
117 cmp -q paths-a.exe paths-b.exe
118
119 -- paths.go --
120 package main
121
122 import (
123 "bytes"
124 "fmt"
125 "io/ioutil"
126 "log"
127 "os"
128 "os/exec"
129 "path/filepath"
130 "strings"
131 )
132
133 func main() {
134 exe := os.Args[1]
135 data, err := ioutil.ReadFile(exe)
136 if err != nil {
137 log.Fatal(err)
138 }
139
140 if os.Getenv("GO111MODULE") == "on" {
141 out, err := exec.Command("go", "env", "GOMOD").Output()
142 if err != nil {
143 log.Fatal(err)
144 }
145 modRoot := filepath.Dir(strings.TrimSpace(string(out)))
146 check(data, "module root", modRoot)
147 } else {
148 check(data, "GOPATH", os.Getenv("GOPATH"))
149 }
150 check(data, "GOROOT", os.Getenv("GOROOT"))
151 }
152
153 func check(data []byte, desc, dir string) {
154 containsDir := bytes.Contains(data, []byte(dir))
155 containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
156 fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
157 }
158 -- overlay.json --
159 { "Replace": { "overlaydir/paths.go": "paths.go" } }
160 -- go.mod --
161 module paths
162
163 go 1.14
164
View as plain text