1
1
[short] skip
2
+
3
+ # Set up two identical directories that can be used as GOPATH.
2
4
env GO111MODULE=on
5
+ mkdir $WORK/a/src/paths $WORK/b/src/paths
6
+ cp paths.go $WORK/a/src/paths
7
+ cp paths.go $WORK/b/src/paths
8
+ cp go.mod $WORK/a/src/paths/
9
+ cp go.mod $WORK/b/src/paths/
10
+
3
11
4
- # A binary built without -trimpath should contain the current workspace
12
+ # A binary built without -trimpath should contain the module root dir
5
13
# and GOROOT for debugging and stack traces.
6
- cd a
7
- go build -o $WORK/paths-a .exe paths.go
8
- exec $WORK/paths-a .exe $WORK/paths-a .exe
9
- stdout 'binary contains GOPATH : true'
14
+ cd $WORK/a/src/paths
15
+ go build -o $WORK/paths-dbg .exe .
16
+ exec $WORK/paths-dbg .exe $WORK/paths-dbg .exe
17
+ stdout 'binary contains module root : true'
10
18
stdout 'binary contains GOROOT: true'
11
19
12
20
# A binary built with -trimpath should not contain the current workspace
13
21
# or GOROOT.
14
- go build -trimpath -o $WORK/paths-a.exe paths.go
22
+ go build -trimpath -o $WORK/paths-a.exe .
15
23
exec $WORK/paths-a.exe $WORK/paths-a.exe
16
- stdout 'binary contains GOPATH : false'
24
+ stdout 'binary contains module root : false'
17
25
stdout 'binary contains GOROOT: false'
18
26
19
27
# A binary from an external module built with -trimpath should not contain
20
28
# the current workspace or GOROOT.
21
- cd $WORK
22
29
go get -trimpath rsc.io/fortune
23
30
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
24
- stdout 'binary contains GOPATH : false'
31
+ stdout 'binary contains module root : false'
25
32
stdout 'binary contains GOROOT: false'
33
+ go mod edit -droprequire rsc.io/fortune
26
34
27
35
# Two binaries built from identical packages in different directories
28
36
# should be identical.
29
- # TODO(golang.org/issue/35435): at the moment, they are not.
30
- #mkdir $GOPATH/src/b
31
- #cp $GOPATH/src/a/go.mod $GOPATH/src/b/go.mod
32
- #cp $GOPATH/src/a/paths.go $GOPATH/src/b/paths.go
33
- #cd $GOPATH/src/b
34
- #go build -trimpath -o $WORK/paths-b.exe .
35
- #cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
37
+ cd $WORK/b/src/paths
38
+ go build -trimpath -o $WORK/paths-b.exe
39
+ cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
40
+
41
+
42
+ # Same sequence of tests but in GOPATH mode.
43
+ # A binary built without -trimpath should contain GOPATH and GOROOT.
44
+ env GO111MODULE=off
45
+ cd $WORK
46
+ env GOPATH=$WORK/a
47
+ go build -o paths-dbg.exe paths
48
+ exec ./paths-dbg.exe paths-dbg.exe
49
+ stdout 'binary contains GOPATH: true'
50
+ stdout 'binary contains GOROOT: true'
51
+
52
+ # A binary built with -trimpath should not contain GOPATH or GOROOT.
53
+ go build -trimpath -o paths-a.exe paths
54
+ exec ./paths-a.exe paths-a.exe
55
+ stdout 'binary contains GOPATH: false'
56
+ stdout 'binary contains GOROOT: false'
57
+
58
+ # Two binaries built from identical packages in different GOPATH roots
59
+ # should be identical.
60
+ env GOPATH=$WORK/b
61
+ go build -trimpath -o paths-b.exe paths
62
+ cmp -q paths-a.exe paths-b.exe
36
63
64
+
65
+ # Same sequence of tests but with gccgo.
66
+ # gccgo does not support builds in module mode.
37
67
[!exec:gccgo] stop
68
+ env GOPATH=$WORK/a
38
69
39
70
# A binary built with gccgo without -trimpath should contain the current
40
71
# GOPATH and GOROOT.
41
- env GO111MODULE=off # The current released gccgo does not support builds in module mode.
42
- cd $GOPATH/src/a
43
- go build -compiler=gccgo -o $WORK/gccgo-paths-a.exe .
44
- exec $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-a.exe
72
+ go build -compiler=gccgo -o paths-dbg.exe paths
73
+ exec ./paths-dbg.exe paths-dbg.exe
45
74
stdout 'binary contains GOPATH: true'
46
75
stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
47
76
48
77
# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
49
- go build -compiler=gccgo -trimpath -o $WORK/gccgo- paths-b .exe .
50
- exec $WORK/gccgo- paths-a.exe $WORK/gccgo- paths-b .exe
78
+ go build -compiler=gccgo -trimpath -o paths-a .exe paths
79
+ exec ./ paths-a.exe paths-a .exe
51
80
stdout 'binary contains GOPATH: false'
52
81
stdout 'binary contains GOROOT: false'
53
82
54
83
# Two binaries built from identical packages in different directories
55
84
# should be identical.
56
- # TODO(golang.org/issue/35435): at the moment, they are not.
57
- #cd ../b
58
- #go build -compiler=gccgo -trimpath -o $WORK/gccgo-paths-b.exe .
59
- #cmp -q $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe
85
+ env GOPATH=$WORK/b
86
+ go build -compiler=gccgo -trimpath -o paths-b.exe paths
87
+ cmp -q paths-a.exe paths-b.exe
60
88
61
- -- $GOPATH/src/a/ paths.go --
89
+ -- paths.go --
62
90
package main
63
91
64
92
import (
@@ -67,7 +95,9 @@ import (
67
95
"io/ioutil"
68
96
"log"
69
97
"os"
98
+ "os/exec"
70
99
"path/filepath"
100
+ "strings"
71
101
)
72
102
73
103
func main() {
@@ -77,17 +107,26 @@ func main() {
77
107
log.Fatal(err)
78
108
}
79
109
80
- gopath := []byte(filepath.ToSlash(os.Getenv("GOPATH")))
81
- if len(gopath) == 0 {
82
- log.Fatal("GOPATH not set")
110
+ if os.Getenv("GO111MODULE") == "on" {
111
+ out, err := exec.Command("go", "env", "GOMOD").Output()
112
+ if err != nil {
113
+ log.Fatal(err)
114
+ }
115
+ modRoot := filepath.Dir(strings.TrimSpace(string(out)))
116
+ check(data, "module root", modRoot)
117
+ } else {
118
+ check(data, "GOPATH", os.Getenv("GOPATH"))
83
119
}
84
- fmt.Printf("binary contains GOPATH: %v\n", bytes.Contains(data, gopath))
120
+ check(data, "GOROOT", os.Getenv("GOROOT"))
121
+ }
85
122
86
- goroot := []byte(filepath.ToSlash(os.Getenv("GOROOT")))
87
- if len(goroot) == 0 {
88
- log.Fatal("GOROOT not set")
89
- }
90
- fmt.Printf("binary contains GOROOT: %v\n", bytes.Contains(data, goroot))
123
+ func check(data []byte, desc, dir string) {
124
+ containsDir := bytes.Contains(data, []byte(dir))
125
+ containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
126
+ fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
91
127
}
92
- -- $GOPATH/src/a/go.mod --
93
- module example.com/a
128
+
129
+ -- go.mod --
130
+ module paths
131
+
132
+ go 1.14
0 commit comments