Skip to content

Commit bc724d5

Browse files
Bryan C. Millsjproberts
Bryan C. Mills
authored andcommitted
cmd/go: avoid recording GOROOT_FINAL in precompiled C archives
C archives for packages in GOROOT are shipped along with binary releases of the Go toolchain. Although we build the toolchain with GOROOT_FINAL set, we don't know actually know where the release will be installed: the user's real GOROOT can differ arbitrarily from our GOROOT_FINAL. (In the specific case of toolchains installed through golang.org/dl wrappers, the release's GOROOT_FINAL is /usr/local/go but the actual GOROOT to which the release is installed is $HOME/sdk/$(go env GOVERSION).) Fixes golang#50183 Updates golang#48319 Change-Id: If10a42f90c725300bbcb89c3b5b01a2d93ab6ef7 Reviewed-on: https://go-review.googlesource.com/c/go/+/380915 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 9b05922 commit bc724d5

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,13 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
236236
}
237237
} else if p.Goroot {
238238
// The Go compiler always hides the exact value of $GOROOT
239-
// when building things in GOROOT, but the C compiler
240-
// merely rewrites GOROOT to GOROOT_FINAL.
241-
if len(p.CFiles) > 0 {
242-
fmt.Fprintf(h, "goroot %s\n", cfg.GOROOT_FINAL)
243-
}
239+
// when building things in GOROOT.
240+
//
241+
// The C compiler does not, but for packages in GOROOT we rewrite the path
242+
// as though -trimpath were set, so that we don't invalidate the build cache
243+
// (and especially any precompiled C archive files) when changing
244+
// GOROOT_FINAL. (See https://go.dev/issue/50183.)
245+
//
244246
// b.WorkDir is always either trimmed or rewritten to
245247
// the literal string "/tmp/go-build".
246248
} else if !strings.HasPrefix(p.Dir, b.WorkDir) {
@@ -2337,7 +2339,7 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
23372339
// directives pointing to the source directory. It should not generate those
23382340
// when -trimpath is enabled.
23392341
if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
2340-
if cfg.BuildTrimpath {
2342+
if cfg.BuildTrimpath || p.Goroot {
23412343
// Keep in sync with Action.trimpath.
23422344
// The trimmed paths are a little different, but we need to trim in the
23432345
// same situations.
@@ -2359,8 +2361,6 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
23592361
to = filepath.Join("/_", toPath)
23602362
}
23612363
flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+from+"="+to)
2362-
} else if p.Goroot && cfg.GOROOT_FINAL != cfg.GOROOT {
2363-
flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+cfg.GOROOT+"="+cfg.GOROOT_FINAL)
23642364
}
23652365
}
23662366

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Regression test for https://go.dev/issue/47215 and https://go.dev/issue/50183:
2+
# A mismatched $GOROOT_FINAL or missing $CC caused the C dependencies of the net
3+
# package to appear stale, and it could not be rebuilt due to a missing $CC.
4+
5+
[!cgo] skip
6+
7+
# Control case: net must not already be stale.
8+
! stale net
9+
10+
# https://go.dev/issue/47215: a missing $(go env CC) caused the precompiled net to be stale.
11+
[!plan9] env PATH='' # Guaranteed not to include $(go env CC)!
12+
[plan9] env path=''
13+
! stale net # issue #47215
14+
15+
# https://go.dev/issue/50183: a mismatched GOROOT_FINAL caused net to be stale.
16+
env GOROOT_FINAL=$WORK${/}goroot
17+
! stale net

0 commit comments

Comments
 (0)