8000 fix(loader): include package directory in the package cache key by TiantianYZJ · Pull Request #1853 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

fix(loader): include package directory in the package cache key - #1853

Open
TiantianYZJ wants to merge 1 commit into
dominikh:masterfrom
TiantianYZJ:fix/trimpath-package-cache-key
Open

TiantianYZJ wants to merge 1 commit into
dominikh:masterfrom
TiantianYZJ:fix/trimpath-package-cache-key

Conversation

@TiantianYZJ
Copy link
Copy Markdown

Problem

With GOFLAGS=-trimpath and a shared STATICCHECK_CACHE, staticcheck -matrix running 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:

  1. The cache key omits the directory. 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 cache ActionID.
  2. Cached diagnostics keep absolute positions. They are stored with the source paths of the run that produced them.
  3. The merge matches on paths. mergeRuns (lintcmd/cmd.go:602) only vetoes a MergeIfAll diagnostic 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's checkedFiles, so nothing vetoes it and it is printed unchanged.

Fix

Add PackageSpec.Dir (go/loader/loader.go:30, populated from packages.Package.Dir at go/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 a and b, GOFLAGS=-trimpath, one shared STATICCHECK_CACHE, matrix default: / integration: -tags=integration):

  • Before: run in a prints nothing; run in b prints ../a/m.go:3:6: func helper is unused [default] (U1000) and exits 1.
  • After: both runs print nothing and exit 0.
  • Without -matrix, the bug also made b report the real finding with a's path; after the change it reports m.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.

  • With the dir line removed, the test fails with package hashes are equal for "...\a" and "...\b".
  • With the fix, it passes.

Test runs (go1.27.1, Windows):

  • go test -count=1 -run TestGraphHashIncludesPackageDirectory ./go/loader — pass
  • go test -count=1 -p 2 ./... — all packages pass except go/ir, which aborted with fatal error: runtime: cannot allocate memory on this machine (5 GB free RAM). go/ir does not import go/loader; rerunning it alone with GOGC=30 passes. Not related to this change.
  • gofmt -l on the changed files — clean; go vet ./go/loader — clean.

Scope

Only this fix: go/loader/hash.go, go/loader/loader.go, and the new go/loader/loader_test.go. No refactoring, no unrelated changes.

Fixes #1852

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-matrix reports false U1000 findings from a cache entry made in another directory under -trimpath

1 participant

0