8000 config: compare files against GOCACHE, not the user cache dir by CloudCorpRecords · Pull Request #1849 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

config: compare files against GOCACHE, not the user cache dir - #1849

Open
CloudCorpRecords wants to merge 1 commit into
dominikh:masterfrom
CloudCorpRecords:fix-1847-config-build-cache
Open

CloudCorpRecords wants to merge 1 commit into
dominikh:masterfrom
CloudCorpRecords:fix-1847-config-build-cache

Conversation

@CloudCorpRecords
Copy link
Copy Markdown

Fixes #1847.

config.Dir skipped every file whose path started with os.UserCacheDir(), taking it for a file in Go's build cache. A package whose files all live below the user cache directory (for example a git worktree kept in ~/.cache) therefore ended up with DefaultConfig, its staticcheck.conf silently ignored. The string-prefix test also caught paths that merely share a prefix with the cache directory (such as ~/.cache-like), and it missed the build cache whenever GOCACHE was set to a directory outside the user cache directory.

This change compares against Go's build cache itself, as suggested in the issue:

  • The build cache directory is resolved once: the GOCACHE environment variable first, then go env GOCACHE (which also accounts for GOCACHE set in the go/env file), falling back to the default location (go-build below the user cache directory) if neither works. GOCACHE=off disables the skipping.
  • Whether a file lies in the build cache is now tested with filepath.Rel, i.e. on a path boundary, instead of strings.HasPrefix. This also resolves the FIXME that was next to the old test.

The guard itself is preserved: a package consisting only of files in the build cache (such as the test main package synthesized by go list -test) still yields no configuration directory.

Tests

New config/config_test.go covers the directory logic directly (the package previously had no tests): a package below the user cache dir, packages sharing a string prefix with the user cache dir and with the build cache, packages consisting only of build-cache files, mixed file lists, and an empty build cache.

Verified on Windows (the same code path reads %LocalAppData% there), Go 1.27.3:

  • Before the fix, a test driving the public Dir API with the real os.UserCacheDir() failed both bug cases: Dir returned "" for a package below the user cache dir and for one below a -like prefix sibling. After the fix it passes, including the build-cache-only guard case.
  • go test ./config/ -count=1 passes (9 table cases + the public-API test); go vet ./config/ is clean; go build ./... passes.
  • End-to-end with staticcheck binaries built from pristine master and from this branch, on the issue's repro project (checks = ["inherit", "-ST1005"] and a capitalized error string) placed under the user cache dir: pristine master reports repro.go:5:12: error strings should not be capitalized (ST1005), the fixed binary reports nothing. Controls: the fixed binary still reports ST1005 for the same project without a staticcheck.conf, and both binaries honour the config for a project outside the cache dir. The result is the same when the project has a test file, i.e. when a synthesized test main package lives in the build cache.

Dir skipped every file below os.UserCacheDir, taking it for a file
in Go's build cache. That silently ignored the staticcheck.conf of
any package living below the user cache directory (such as git
worktrees kept in ~/.cache), as well as packages below paths that
merely share a string prefix with it, and it missed the build cache
whenever GOCACHE pointed elsewhere.

Compare against the actual build cache instead: the GOCACHE
environment variable, then `go env GOCACHE` (which also accounts
for the go/env file), falling back to the default location below
the user cache directory. Membership is now tested on a path
boundary instead of a string prefix.

Fixes dominikh#1847

Signed-off-by: Rene Turcios <95189389+CloudCorpRecords@users.noreply.github.com>
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.

staticcheck.conf is silently ignored for packages under os.UserCacheDir

1 participant

0