summaryrefslogtreecommitdiffstats
path: root/src/cmd/go/testdata/script/build_trimpath.txt
blob: 2c3bee8fdc7a925bb9bd9bdcee078a36b0ed9581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
[short] skip

# If GOROOT_FINAL is set, 'go build -trimpath' bakes that into the resulting
# binary instead of GOROOT. Explicitly unset it here.
env GOROOT_FINAL=

# Set up two identical directories that can be used as GOPATH.
env GO111MODULE=on
mkdir $WORK/a/src/paths $WORK/b/src/paths
cp paths.go $WORK/a/src/paths
cp paths.go $WORK/b/src/paths
cp overlay.json $WORK/a/src/paths
cp overlay.json $WORK/b/src/paths
cp go.mod $WORK/a/src/paths/
cp go.mod $WORK/b/src/paths/


# A binary built without -trimpath should contain the module root dir
# and GOROOT for debugging and stack traces.
cd $WORK/a/src/paths
go build -o $WORK/paths-dbg.exe .
exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
stdout 'binary contains module root: true'
stdout 'binary contains GOROOT: true'

# A binary built with -trimpath should not contain the current workspace
# or GOROOT.
go build -trimpath -o $WORK/paths-a.exe .
exec $WORK/paths-a.exe $WORK/paths-a.exe
stdout 'binary contains module root: false'
stdout 'binary contains GOROOT: false'

# A binary from an external module built with -trimpath should not contain
# the current workspace or GOROOT.
go get -trimpath rsc.io/fortune
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
stdout 'binary contains module root: false'
stdout 'binary contains GOROOT: false'
go mod edit -droprequire rsc.io/fortune

# Two binaries built from identical packages in different directories
# should be identical.
cd $WORK/b/src/paths
go build -trimpath -o $WORK/paths-b.exe
cmp -q $WORK/paths-a.exe $WORK/paths-b.exe


# Same sequence of tests but with overlays.
# A binary built without -trimpath should contain the module root dir
# and GOROOT for debugging and stack traces.
cd $WORK/a/src/paths
go build -overlay overlay.json -o $WORK/paths-dbg.exe ./overlaydir
exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
stdout 'binary contains module root: true'
stdout 'binary contains GOROOT: true'

# A binary built with -trimpath should not contain the current workspace
# or GOROOT.
go build -overlay overlay.json -trimpath -o $WORK/paths-a.exe ./overlaydir
exec $WORK/paths-a.exe $WORK/paths-a.exe
stdout 'binary contains module root: false'
stdout 'binary contains GOROOT: false'

# Two binaries built from identical packages in different directories
# should be identical.
cd $WORK/b/src/paths
go build -overlay overlay.json -trimpath -o $WORK/paths-b.exe ./overlaydir
cmp -q $WORK/paths-a.exe $WORK/paths-b.exe


# Same sequence of tests but in GOPATH mode.
# A binary built without -trimpath should contain GOPATH and GOROOT.
env GO111MODULE=off
cd $WORK
env GOPATH=$WORK/a
go build -o paths-dbg.exe paths
exec ./paths-dbg.exe paths-dbg.exe
stdout 'binary contains GOPATH: true'
stdout 'binary contains GOROOT: true'

# A binary built with -trimpath should not contain GOPATH or GOROOT.
go build -trimpath -o paths-a.exe paths
exec ./paths-a.exe paths-a.exe
stdout 'binary contains GOPATH: false'
stdout 'binary contains GOROOT: false'

# Two binaries built from identical packages in different GOPATH roots
# should be identical.
env GOPATH=$WORK/b
go build -trimpath -o paths-b.exe paths
cmp -q paths-a.exe paths-b.exe


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

# A binary built with gccgo without -trimpath should contain the current
# GOPATH and GOROOT.
go build -compiler=gccgo -o paths-dbg.exe paths
exec ./paths-dbg.exe paths-dbg.exe
stdout 'binary contains GOPATH: true'
stdout 'binary contains GOROOT: false' # gccgo doesn't load std from GOROOT.

# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
go build -compiler=gccgo -trimpath -o paths-a.exe paths
exec ./paths-a.exe paths-a.exe
stdout 'binary contains GOPATH: false'
stdout 'binary contains GOROOT: false'

# Two binaries built from identical packages in different directories
# should be identical.
env GOPATH=$WORK/b
go build -compiler=gccgo -trimpath -o paths-b.exe paths
cmp -q paths-a.exe paths-b.exe

-- paths.go --
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	exe := os.Args[1]
	data, err := ioutil.ReadFile(exe)
	if err != nil {
		log.Fatal(err)
	}

	if os.Getenv("GO111MODULE") == "on" {
		out, err := exec.Command("go", "env", "GOMOD").Output()
		if err != nil {
			log.Fatal(err)
		}
		modRoot := filepath.Dir(strings.TrimSpace(string(out)))
		check(data, "module root", modRoot)
	} else {
		check(data, "GOPATH", os.Getenv("GOPATH"))
	}
	check(data, "GOROOT", os.Getenv("GOROOT"))
}

func check(data []byte, desc, dir string) {
	containsDir := bytes.Contains(data, []byte(dir))
	containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
	fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
}
-- overlay.json --
{ "Replace": { "overlaydir/paths.go": "paths.go" } }
-- go.mod --
module paths

go 1.14