Skip to content

Commit 8324aca

Browse files
author
Jay Conrod
committed
cmd/go: fix and re-enable build_trimpath test
The test was comparing a binary built from a list of files to a test build from a named package. That should not (and did not) work. The test now compares two binaries built the same way in different directories. Also add a portion of the test for GOPATH and fix the gccgo portion of the test (verified manually). Fixes #35435 Change-Id: I2535a0011c9d97d2274e5550ae277302dbb91e6f Reviewed-on: https://go-review.googlesource.com/c/go/+/208234 Run-TryBot: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 941ac9c commit 8324aca

File tree

1 file changed

+77
-38
lines changed

1 file changed

+77
-38
lines changed
Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,92 @@
11
[short] skip
2+
3+
# Set up two identical directories that can be used as GOPATH.
24
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+
311

4-
# A binary built without -trimpath should contain the current workspace
12+
# A binary built without -trimpath should contain the module root dir
513
# 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'
1018
stdout 'binary contains GOROOT: true'
1119

1220
# A binary built with -trimpath should not contain the current workspace
1321
# or GOROOT.
14-
go build -trimpath -o $WORK/paths-a.exe paths.go
22+
go build -trimpath -o $WORK/paths-a.exe .
1523
exec $WORK/paths-a.exe $WORK/paths-a.exe
16-
stdout 'binary contains GOPATH: false'
24+
stdout 'binary contains module root: false'
1725
stdout 'binary contains GOROOT: false'
1826

1927
# A binary from an external module built with -trimpath should not contain
2028
# the current workspace or GOROOT.
21-
cd $WORK
2229
go get -trimpath rsc.io/fortune
2330
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
24-
stdout 'binary contains GOPATH: false'
31+
stdout 'binary contains module root: false'
2532
stdout 'binary contains GOROOT: false'
33+
go mod edit -droprequire rsc.io/fortune
2634

2735
# Two binaries built from identical packages in different directories
2836
# 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
3663

64+
65+
# Same sequence of tests but with gccgo.
66+
# gccgo does not support builds in module mode.
3767
[!exec:gccgo] stop
68+
env GOPATH=$WORK/a
3869

3970
# A binary built with gccgo without -trimpath should contain the current
4071
# 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
4574
stdout 'binary contains GOPATH: true'
4675
stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.
4776

4877
# 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
5180
stdout 'binary contains GOPATH: false'
5281
stdout 'binary contains GOROOT: false'
5382

5483
# Two binaries built from identical packages in different directories
5584
# 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
6088

61-
-- $GOPATH/src/a/paths.go --
89+
-- paths.go --
6290
package main
6391

6492
import (
@@ -67,7 +95,9 @@ import (
6795
"io/ioutil"
6896
"log"
6997
"os"
98+
"os/exec"
7099
"path/filepath"
100+
"strings"
71101
)
72102

73103
func main() {
@@ -77,17 +107,26 @@ func main() {
77107
log.Fatal(err)
78108
}
79109

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"))
83119
}
84-
fmt.Printf("binary contains GOPATH: %v\n", bytes.Contains(data, gopath))
120+
check(data, "GOROOT", os.Getenv("GOROOT"))
121+
}
85122

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)
91127
}
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

Comments
 (0)