Repository navigation
fix(loader): include package directory in the package cache key - #1853
Open
TiantianYZJ wants to merge 1 commit into
Open
TiantianYZJ wants to merge 1 commit into
TiantianYZJ wants to merge 1 commit into
Conversation
computeHash keys a package on the build ID of its export data. When go/packages loads with -trimpath, cmd/go leaves the package directory out of that build ID, so two identical packages in different directories produce the same cache key. Cached diagnostics refer to source files by absolute path, and mergeRuns only vetoes a MergeIfAll diagnostic if a run that checked the same file did not report it. A diagnostic replayed from another directory's cache matches no checked file in the current run, so it is printed as-is: with a shared cache, `staticcheck -matrix` in directory b reports a U1000 finding located in directory a. Include PackageSpec.Dir in the hash. Repeated runs in the same directory still reuse their cache entries, but identical packages in different directories no longer share results. Fixes dominikh#1852
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
GOFLAGS=-trimpathand a sharedSTATICCHECK_CACHE,staticcheck -matrixrunning in one directory can print a false U1000 finding whose position points at a file in a different directory that contains an identical package.Root cause, in three parts:
computeHash(go/loader/hash.go:16) keys a package on the build ID of its export data (go/loader/hash.go:31-40). Under-trimpath, cmd/go does not include the package directory in that build ID, so identical packages in different directories get the same cacheActionID.mergeRuns(lintcmd/cmd.go:602) only vetoes aMergeIfAlldiagnostic when some run that checked the same file did not report it. A diagnostic replayed from another directory's cache has a path that is not in the current run'scheckedFiles, so nothing vetoes it and it is printed unchanged.Fix
Add
PackageSpec.Dir(go/loader/loader.go:30, populated frompackages.Package.Diratgo/loader/loader.go:94) and include it in the hash (go/loader/hash.go:25).Cache entries stay reusable for repeated runs in the same directory, but identical packages in different directories no longer share results. This is the first of the two possible fixes listed in the issue; it addresses the stale-diagnostic problem at its source and does not change any analyzer semantics.
Evidence
Reproduction. Following the issue's script (two identical modules
aandb,GOFLAGS=-trimpath, one sharedSTATICCHECK_CACHE, matrixdefault:/integration: -tags=integration):aprints nothing; run inbprints../a/m.go:3:6: func helper is unused [default] (U1000)and exits 1.-matrix, the bug also madebreport the real finding witha's path; after the change it reportsm.go:3:6.Regression test. New
TestGraphHashIncludesPackageDirectory(go/loader/loader_test.go) creates two identical modules in one temporary directory, loads both with-trimpath, and asserts that the package hashes differ. It first checks that the export build IDs are equal and skips if a future Go version includes the directory in the build ID, so it cannot produce a false failure.dirline removed, the test fails withpackage hashes are equal for "...\a" and "...\b".Test runs (go1.27.1, Windows):
go test -count=1 -run TestGraphHashIncludesPackageDirectory ./go/loader— passgo test -count=1 -p 2 ./...— all packages pass exceptgo/ir, which aborted withfatal error: runtime: cannot allocate memoryon this machine (5 GB free RAM).go/irdoes not importgo/loader; rerunning it alone withGOGC=30passes. Not related to this change.gofmt -lon the changed files — clean;go vet ./go/loader— clean.Scope
Only this fix:
go/loader/hash.go,go/loader/loader.go, and the newgo/loader/loader_test.go. No refactoring, no unrelated changes.Fixes #1852