Skip to content

Scope the Go scan of a module without go.sum to its go.mod requires - #2169

Merged
Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
fix/go-crawl-scope
Nov 19, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
fix/go-crawl-scope

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A local Go scan looks up only the modules the project's go.sum records and its go.mod requires, but only when a go.sum exists. Without one, the crawl walked the whole module cache. A module that imports only the standard library has no require and no go.sum, so it was reported as having every module other projects left in GOMODCACHE.

A missing go.sum now scopes the crawl to go.mod's requires, when those are the build list: a go.mod that prunes the module graph (go 1.17 or later) requires every module that provides a package to the build, and one with no requires has no module graph.

Behaviour

scan in a module with no require and no go.sum, on a machine whose module cache holds a few hundred modules from other projects, four of which have patches:

Mode Before After
hosted hundreds of packages found; one not in this project's build graph warning per patched module; exit 0 No golang packages found.; exit 0
vendored the four patches are downloaded, then one Cannot vendor error each; exit 1 No golang packages found.; exit 0
agent (--dry-run) offers to patch the four cached modules in place No golang packages found.; exit 0

A go 1.17+ module that has requires but no go.sum now finds only those requires. The required module is patched as before, without the warnings (hosted) or errors and exit 1 (vendored) for unrelated cached modules.

A project with a go.sum is unchanged, with one exception: -modfile (below).

What still walks the cache

The walk is kept where go.mod and go.sum may not describe the build:

  • a Go workspace in effect (go.work, or GOWORK)
  • a -modfile in GOFLAGS: go builds from the named file and its .sum. This now holds with a go.sum present too. Before, a go.sum scoped the crawl to two files go was not reading
  • a go.sum that is there but cannot be read
  • no go.sum, and a go.mod that cannot be read
  • no go.sum, and a go.mod older than go 1.17 (or with no go directive) that has requires: it lists lower bounds only, and what its requires need in turn is named only by go.mod files in the cache
  • --global and --global-prefix

Tests

  • go_crawler unit tests: the test that pinned the old fallback is inverted (without_a_go_sum_only_the_requires_of_go_mod_are_crawled). Two new ones cover the cases that keep the walk (an_older_go_mod_with_requires_and_no_go_sum_keeps_the_walk, another_modfile_or_an_unreadable_go_sum_keeps_the_walk). The fixture now also unsets GOFLAGS and sets GOENV=off, so a developer's own go settings cannot change the result.
  • e2e_golang: three tests staged a cache beside a go.mod with no requires and expected the whole cache back. Their projects now require the modules they stage. The discovery and case-encoding tests also run through --global-prefix, so the walk's cache/ pruning and case decoding stay covered. A new test, scan_of_a_module_without_dependencies_ignores_the_shared_cache, covers the reported case end to end: nothing is scanned and nothing is sent to the API. The helper scrubs the developer's go settings.
  • Run locally on macOS: the full socket-patch-core suite, the 26 CLI test binaries that exercise Go, and CI's clippy command. The rest of the CLI suite was left to CI.
  • Checked by hand with the built binary and real go 1.26: the three modes above, a module with requires and no go.sum (hosted and vendored), the same module declared go 1.16 (walk kept), and a tidy module in hosted mode followed by go build.

Review

/code-review high reported five findings and all five are fixed in the second commit: the pre-1.17 go.mod case, -modfile with a go.sum present, the e2e helper's go settings, walk coverage in e2e_golang, and the crawl_all comment.

Not in this PR

A go.work workspace still walks the whole cache, so it still gets the same false warnings in hosted mode and the same errors and exit 1 in vendored mode. Scoping it to its members' go.mod and go.sum plus go.work.sum is a follow-up. So is scoping a pre-1.17 module without a go.sum from the module graph in the cache.

🤖 Generated with Claude Code

A local Go scan looks up only the modules the project's go.sum records
and its go.mod requires, but only when a go.sum exists. Without one the
crawl walked the whole module cache, so a module on the standard library
alone (no require, no go.sum) was reported as having every module other
projects left in GOMODCACHE. The patch lookup then matched some of them
and the write step refused each one as outside the build graph: a
warning per module in hosted mode, an error per module and exit 1 in
vendored mode, and an offer to patch them in place in agent mode.

A missing go.sum now scopes the crawl to go.mod's requires. go writes a
go.sum line for every module it loads from the cache, so without the
file the build takes nothing from the cache that go.mod does not name.

The walk is kept where go.mod may not describe the build: a Go workspace
in effect, a go.sum that is there but cannot be read, and a -modfile in
GOFLAGS (go then builds from another file with its own .sum). --global
and --global-prefix still walk the cache.

Three e2e_golang tests staged a cache next to a go.mod with no requires
and expected the whole cache back; their projects now require the
modules they stage. A new test covers the reported case end to end:
nothing is scanned and no module is sent to the API.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review of the previous commit:

- A go.mod older than go 1.17 (or with no go directive) lists lower
  bounds only: what its requires need in turn is part of the build and
  is named only by go.mod files in the cache. With no go.sum, such a
  go.mod with requires keeps the whole-cache walk, so those modules are
  still scanned. A go.mod with no requires has no module graph and is
  scoped whatever its go version.
- A -modfile in GOFLAGS keeps the walk whether or not a go.sum is there:
  go builds from the named file and its .sum, not from go.mod and
  go.sum. Before, a go.sum scoped the crawl to files go was not reading.
- e2e_golang scrubs the developer's go settings (GOFLAGS, GOWORK and the
  go env -w file now change which modules a project scan looks up).
- e2e_golang runs the discovery and case-encoding tests through
  --global-prefix too, so the walk's cache/ pruning and case decoding
  stay covered now that the project scans no longer walk.
- crawl_all's comment points at go_sum_scope for the cases that walk.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 19, 2026 20:47
@cursor

cursor Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes which Go modules are discovered and sent to the API for common local scans, affecting patching behavior on machines with large shared caches; edge cases (workspaces, old go.mod, -modfile) intentionally retain the broader walk.

Overview
Local Go scans no longer treat a missing go.sum as “walk the entire module cache.” go_sum_scope now limits project-mode crawls to modules listed in go.sum plus explicit go.mod require lines when that manifest is a go 1.17+ graph that prunes indirects. A stdlib-only project with no requires therefore reports zero packages even if GOMODCACHE is full of other projects’ modules.

The full-cache walk is still used where the build graph cannot be inferred from the project files alone: Go workspaces, -modfile in GOFLAGS, unreadable go.sum, pre-1.17 go.mod with requires, and --global / --global-prefix.

Tests invert the old “no go.sum → walk all” expectation, add cases for walk retention and -modfile, harden fixtures against developer Go env (GOFLAGS, GOENV), and extend e2e coverage (hermetic Go scrubbing, scan_json_with, explicit requires in fixtures, a new end-to-end test for dependency-free modules, and --global-prefix walk assertions).

Reviewed by Cursor Bugbot for commit 5e12b41. Configure here.

Merged via the queue into main with commit d990d0b Nov 19, 2026
83 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the fix/go-crawl-scope branch November 19, 2026 21:22
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.

2 participants