cmd/go: include module root in package index key

The package index format includes the directory relative to the module
root. The module root for a given directory can change even if the
contents of the directory itself do not (by adding or removing a
go.mod file in some parent directory).

Thus, we need to invalidate the index for a package when its module
root location changes.

Fixes #53586 (I think).

Change-Id: I2d9f4de80e16bce75b3106a2bad4a11d8378d037
Reviewed-on: https://go-review.googlesource.com/c/go/+/415475
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-06-30 13:39:57 -04:00 committed by Gopher Robot
parent 84db00ffd1
commit 981d5947af
2 changed files with 26 additions and 3 deletions

View file

@ -73,6 +73,10 @@ func moduleHash(modroot string, ismodcache bool) (cache.ActionID, error) {
}
h := cache.NewHash("moduleIndex")
// TODO(bcmills): Since modules in the index are checksummed, we could
// probably improve the cache hit rate by keying off of the module
// path@version (perhaps including the checksum?) instead of the module root
// directory.
fmt.Fprintf(h, "module index %s %s %v\n", runtime.Version(), indexVersion, modroot)
return h.Sum(), nil
}
@ -81,8 +85,9 @@ const modTimeCutoff = 2 * time.Second
// dirHash returns an ActionID corresponding to the state of the package
// located at filesystem path pkgdir.
func dirHash(pkgdir string) (cache.ActionID, error) {
func dirHash(modroot, pkgdir string) (cache.ActionID, error) {
h := cache.NewHash("moduleIndex")
fmt.Fprintf(h, "modroot %s\n", modroot)
fmt.Fprintf(h, "package %s %s %v\n", runtime.Version(), indexVersion, pkgdir)
entries, err := fsys.ReadDir(pkgdir)
if err != nil {
@ -206,8 +211,8 @@ func openIndexPackage(modroot, pkgdir string) (*IndexPackage, error) {
pkg *IndexPackage
err error
}
r := pcache.Do(pkgdir, func() any {
id, err := dirHash(pkgdir)
r := pcache.Do([2]string{modroot, pkgdir}, func() any {
id, err := dirHash(modroot, pkgdir)
if err != nil {
return result{nil, err}
}

View file

@ -0,0 +1,18 @@
[short] skip # sleeps to make mtime cacheable
go mod init example
cd subdir
go mod init example/subdir
sleep 2s # allow go.mod mtime to be cached
go list -f '{{.Dir}}: {{.ImportPath}}' ./pkg
stdout $PWD${/}pkg': example/subdir/pkg$'
rm go.mod # expose ../go.mod
go list -f '{{.Dir}}: {{.ImportPath}}' ./pkg
stdout $PWD${/}pkg': example/subdir/pkg$'
-- subdir/pkg/pkg.go --
package pkg