Skip to content

Commit 8c8a881

Browse files
Jay ConrodBryan C. Mills
Jay Conrod
authored and
Bryan C. Mills
committed
[release-branch.go1.13] cmd/go: don't include package dir in cache key when -trimpath is set
The '-trimpath' flag tells 'go build' to trim any paths from the output files that are tied to the current workspace or toolchain. When this flag is set, we do not need to include the package directory in the text hashed to construct the action ID for each package. Updates #33772 Fixes #34326 Change-Id: I20b902d2f58019709b15864ca79aa0d9255ae707 Reviewed-on: https://go-review.googlesource.com/c/go/+/195318 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> (cherry picked from commit aa680c0) Reviewed-on: https://go-review.googlesource.com/c/go/+/198259 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 0c07603 commit 8c8a881

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
200200
// same compiler settings and can reuse each other's results.
201201
// If not, the reason is already recorded in buildGcflags.
202202
fmt.Fprintf(h, "compile\n")
203+
// Only include the package directory if it may affect the output.
204+
// We trim workspace paths for all packages when -trimpath is set.
203205
// The compiler hides the exact value of $GOROOT
204-
// when building things in GOROOT,
205-
// but it does not hide the exact value of $GOPATH.
206-
// Include the full dir in that case.
206+
// when building things in GOROOT.
207207
// Assume b.WorkDir is being trimmed properly.
208-
if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) {
208+
if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
209209
fmt.Fprintf(h, "dir %s\n", p.Dir)
210210
}
211211
fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)

src/cmd/go/script_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,24 +441,34 @@ func (ts *testScript) cmdCmp(neg bool, args []string) {
441441
// It would be strange to say "this file can have any content except this precise byte sequence".
442442
ts.fatalf("unsupported: ! cmp")
443443
}
444+
quiet := false
445+
if len(args) > 0 && args[0] == "-q" {
446+
quiet = true
447+
args = args[1:]
448+
}
444449
if len(args) != 2 {
445450
ts.fatalf("usage: cmp file1 file2")
446451
}
447-
ts.doCmdCmp(args, false)
452+
ts.doCmdCmp(args, false, quiet)
448453
}
449454

450455
// cmpenv compares two files with environment variable substitution.
451456
func (ts *testScript) cmdCmpenv(neg bool, args []string) {
452457
if neg {
453458
ts.fatalf("unsupported: ! cmpenv")
454459
}
460+
quiet := false
461+
if len(args) > 0 && args[0] == "-q" {
462+
quiet = true
463+
args = args[1:]
464+
}
455465
if len(args) != 2 {
456466
ts.fatalf("usage: cmpenv file1 file2")
457467
}
458-
ts.doCmdCmp(args, true)
468+
ts.doCmdCmp(args, true, quiet)
459469
}
460470

461-
func (ts *testScript) doCmdCmp(args []string, env bool) {
471+
func (ts *testScript) doCmdCmp(args []string, env, quiet bool) {
462472
name1, name2 := args[0], args[1]
463473
var text1, text2 string
464474
if name1 == "stdout" {
@@ -484,7 +494,9 @@ func (ts *testScript) doCmdCmp(args []string, env bool) {
484494
return
485495
}
486496

487-
fmt.Fprintf(&ts.log, "[diff -%s +%s]\n%s\n", name1, name2, diff(text1, text2))
497+
if !quiet {
498+
fmt.Fprintf(&ts.log, "[diff -%s +%s]\n%s\n", name1, name2, diff(text1, text2))
499+
}
488500
ts.fatalf("%s and %s differ", name1, name2)
489501
}
490502

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
[short] skip
22

33
env -r GOROOT_REGEXP=$GOROOT
4-
env -r WORK_REGEXP=$WORK
4+
env -r WORK_REGEXP='$WORK' # don't expand $WORK; grep replaces $WORK in text before matching.
55
env GOROOT GOROOT_REGEXP WORK WORK_REGEXP
66

7+
# A binary built without -trimpath should contain the current workspace
8+
# and GOROOT for debugging and stack traces.
9+
cd a
10+
go build -o hello.exe hello.go
11+
grep -q $WORK_REGEXP hello.exe
12+
grep -q $GOROOT_REGEXP hello.exe
13+
14+
# A binary built with -trimpath should not contain the current workspace
15+
# or GOROOT.
716
go build -trimpath -o hello.exe hello.go
817
! grep -q $GOROOT_REGEXP hello.exe
918
! grep -q $WORK_REGEXP hello.exe
19+
cd ..
1020

21+
# A binary from an external module built with -trimpath should not contain
22+
# the current workspace or GOROOT.
1123
env GO111MODULE=on
1224
go build -trimpath -o fortune.exe rsc.io/fortune
1325
! grep -q $GOROOT_REGEXP fortune.exe
1426
! grep -q $WORK_REGEXP fortune.exe
1527

16-
-- hello.go --
28+
# Two binaries built from identical packages in different directories
29+
# should be identical.
30+
mkdir b
31+
cp a/go.mod a/hello.go b
32+
cd a
33+
go build -trimpath -o ../a.exe .
34+
cd ../b
35+
go build -trimpath -o ../b.exe .
36+
cd ..
37+
cmp -q a.exe b.exe
38+
39+
-- a/hello.go --
1740
package main
1841
func main() { println("hello") }
1942

20-
-- go.mod --
43+
-- a/go.mod --
2144
module m

0 commit comments

Comments
 (0)