}
} else if p.Goroot {
// The Go compiler always hides the exact value of $GOROOT
- // when building things in GOROOT, but the C compiler
- // merely rewrites GOROOT to GOROOT_FINAL.
- if len(p.CFiles) > 0 {
- fmt.Fprintf(h, "goroot %s\n", cfg.GOROOT_FINAL)
- }
+ // when building things in GOROOT.
+ //
+ // The C compiler does not, but for packages in GOROOT we rewrite the path
+ // as though -trimpath were set, so that we don't invalidate the build cache
+ // (and especially any precompiled C archive files) when changing
+ // GOROOT_FINAL. (See https://go.dev/issue/50183.)
+ //
// b.WorkDir is always either trimmed or rewritten to
// the literal string "/tmp/go-build".
} else if !strings.HasPrefix(p.Dir, b.WorkDir) {
// directives pointing to the source directory. It should not generate those
// when -trimpath is enabled.
if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
- if cfg.BuildTrimpath {
+ if cfg.BuildTrimpath || p.Goroot {
// Keep in sync with Action.trimpath.
// The trimmed paths are a little different, but we need to trim in the
// same situations.
to = filepath.Join("/_", toPath)
}
flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+from+"="+to)
- } else if p.Goroot && cfg.GOROOT_FINAL != cfg.GOROOT {
- flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+cfg.GOROOT+"="+cfg.GOROOT_FINAL)
}
}
--- /dev/null
+# Regression test for https://go.dev/issue/47215 and https://go.dev/issue/50183:
+# A mismatched $GOROOT_FINAL or missing $CC caused the C dependencies of the net
+# package to appear stale, and it could not be rebuilt due to a missing $CC.
+
+[!cgo] skip
+
+# Control case: net must not already be stale.
+! stale net
+
+# https://go.dev/issue/47215: a missing $(go env CC) caused the precompiled net to be stale.
+[!plan9] env PATH='' # Guaranteed not to include $(go env CC)!
+[plan9] env path=''
+! stale net # issue #47215
+
+# https://go.dev/issue/50183: a mismatched GOROOT_FINAL caused net to be stale.
+env GOROOT_FINAL=$WORK${/}goroot
+! stale net