cmd/go: use overlaid path contents in build cache

When caching actions, use the overlaid file contents, because those
are the ones actually used to produce the outputs.

For #39958

Change-Id: Ia1f85b2fcf1f26e3b5be82f4d35c2726b134a36b
Reviewed-on: https://go-review.googlesource.com/c/go/+/266720
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
diff --git a/src/cmd/go/internal/work/buildid.go b/src/cmd/go/internal/work/buildid.go
index 9ef141c..a88544e 100644
--- a/src/cmd/go/internal/work/buildid.go
+++ b/src/cmd/go/internal/work/buildid.go
@@ -15,6 +15,7 @@
 	"cmd/go/internal/base"
 	"cmd/go/internal/cache"
 	"cmd/go/internal/cfg"
+	"cmd/go/internal/fsys"
 	"cmd/go/internal/str"
 	"cmd/internal/buildid"
 )
@@ -375,6 +376,7 @@
 
 // fileHash returns the content hash of the named file.
 func (b *Builder) fileHash(file string) string {
+	file, _ = fsys.OverlayPath(file)
 	sum, err := cache.FileHash(file)
 	if err != nil {
 		return ""
diff --git a/src/cmd/go/testdata/script/build_overlay.txt b/src/cmd/go/testdata/script/build_overlay.txt
index 2e55887..5614b41 100644
--- a/src/cmd/go/testdata/script/build_overlay.txt
+++ b/src/cmd/go/testdata/script/build_overlay.txt
@@ -47,6 +47,14 @@
 exec ./main_call_asm$GOEXE
 ! stdout .
 
+# Change the contents of a file in the overlay and ensure that makes the target stale
+go install -overlay overlay.json ./test_cache
+go list -overlay overlay.json -f '{{.Stale}}' ./test_cache
+stdout '^false$'
+cp overlay/test_cache_different.go overlay/test_cache.go
+go list -overlay overlay.json -f '{{.Stale}}' ./test_cache
+stdout '^true$'
+
 go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
 cp stdout compiled_cgo_sources.txt
 go run ../print_line_comments.go compiled_cgo_sources.txt
@@ -87,6 +95,13 @@
 exec ./main_call_asm_gccgo$GOEXE
 ! stdout .
 
+go install -gccgo -overlay overlay.json ./test_cache
+go list -gccgo -overlay overlay.json -f '{{.Stale}}' ./test_cache
+stdout '^false$'
+cp overlay/test_cache_different.go overlay/test_cache.go
+go list -gccgo -overlay overlay.json -f '{{.Stale}}' ./test_cache
+stdout '^true$'
+
 -- m/go.mod --
 // TODO(matloob): how do overlays work with go.mod (especially if mod=readonly)
 module m
@@ -114,6 +129,7 @@
 		"printpath/main.go": "overlay/printpath.go",
 		"printpath/other.go": "overlay2/printpath2.go",
 		"call_asm/asm.s": "overlay/asm_file.s",
+		"test_cache/main.go": "overlay/test_cache.go",
 		"cgo_hello_replace/cgo_header.h": "overlay/cgo_head.h",
 		"cgo_hello_replace/hello.c": "overlay/hello.c",
 		"cgo_hello_quote/cgo_hello.go": "overlay/cgo_hello_quote.go",
@@ -230,6 +246,22 @@
 TEXT ·foo(SB),0,$0
 	RET
 
+-- m/overlay/test_cache.go --
+package foo
+
+import "fmt"
+
+func bar() {
+    fmt.Println("something")
+}
+-- m/overlay/test_cache_different.go --
+package foo
+
+import "fmt"
+
+func bar() {
+    fmt.Println("different")
+}
 -- m/cgo_hello_quote/hello.c --
 #include <stdio.h>