Skip to content

Scope a Go workspace's scan to the modules it builds, not the machine's module cache - #2431

Merged
Mikola Lysenko (mikolalysenko) merged 10 commits into
mainfrom
arch-refactor/2413-go-workspace-crawl-scope
Dec 2, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 10 commits into
mainfrom
arch-refactor/2413-go-workspace-crawl-scope

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code

Part of #2413: the workspace half. The issue stays open for the other half (see "Left out").

Problem

A scan of a Go project offers a patch for every module version its crawl finds. A plain module's crawl is scoped to the modules its go.sum records and its go.mod requires (#1207, #2169). With a go.work in effect the crawl still walked the whole module cache, and the cache belongs to the machine: every module of every project built there was reported, named to the patch API and offered a patch.

For a developer with a workspace this means:

  • the scan sends the names of other projects' modules to the patch API;
  • the patches for modules the workspace does not build fail at apply time, so the scan exits 1;
  • their records stay in the manifest, and apply --check exits 1 on them in every checkout.

Evidence on main (0a1709b)

Real Go 1.24.7. A workspace of one member that requires github.com/pkg/errors v0.9.1, on a machine whose module cache other projects filled with errors v0.8.1, zerolog, logrus and x/sys. A stand-in patch API serves one patch for four of them and logs each request.

$ socket-patch scan --mode agent --yes -e golang            # main, exit 1
Found 5 packages (5 go)
Error: Failed to patch pkg:golang/github.com/pkg/errors@v0.8.1: the build selects github.com/pkg/errors v0.9.1, not the patched v0.8.1 […]
Error: Failed to patch pkg:golang/github.com/rs/zerolog@v1.26.1: github.com/rs/zerolog v1.26.1 is not in this project's build graph […]
Error: Failed to patch pkg:golang/golang.org/x/sys@v0.0.0-20220715151400-c0bba94af5f8: […] is not in this project's build graph […]
Applied 1 of 4 patches: 3 failed.
Error: Some patches could not be applied.
# the manifest then holds four records; apply --check: "Patches are OUT OF SYNC", exit 1

$ socket-patch scan --mode agent --yes -e golang            # this branch, exit 0
Found 1 package (1 go)
Applied 1 patch.
# one record; a second scan, scan --sync and apply --check all exit 0

The same module copied out of the workspace finds 1 package on both.

Two tests, red on main and green here:

test crawlers::go_crawler::tests::go_sum_scope::a_workspace_crawls_only_the_modules_its_sum_files_record ... FAILED
  left: […lib-dep@v1.0.0, …raised@v1.2.0, …unrelated@v0.1.0, …azure-sdk-for-go@v1.0.0-RC1, …gin@v1.8.0, …gin@v1.9.1, …x/net@v0.17.0, …x/text@v0.14.0]
 right: […lib-dep@v1.0.0, …raised@v1.2.0, …azure-sdk-for-go@v1.0.0-RC1, …gin@v1.9.1]

test agent_scan_in_a_workspace_asks_only_about_the_modules_it_builds ... FAILED        (the binary, real go; e2e_golang_build)
  left: [["…/upstream@v1.0.0"], ["…/stranger@v1.0.0", "…/upstream@v1.0.0"], ["…/stranger@v1.0.0", "…/upstream@v1.0.0"]]
 right: [["…/upstream@v1.0.0"], ["…/upstream@v1.0.0"], ["…/upstream@v1.0.0"]]

The second lists what each scan asked the API: the module alone, then with a go.work beside it, then with one above it.

The class was fixed twice for the plain module (git log -S'fn go_sum_scope': c52bb9b, #1207; d990d0b, #2169). Both left the workspace on the walk, because the workspace reader did not exist yet.

The fix

One scope for both modes, in core crawlers/go_crawler.rs. go_sum_scope reads the workspace through the loader hosted and vendored modes use (go_workspace::load) and collects, for each use member, what module_scope already collected for a module: the zip lines of its go.sum and its requires. To that it adds:

Source Why
the zip lines of the workspace's sum file go records there what no member's go.sum lists (run: a build with a member's go.sum deleted writes the lines to go.work.sum). The file is named after the work file: GOWORK=/x/ci.work writes ci.work.sum (run).
the versions the workspace's module graph selects A sum file can be missing (git often ignores it) or behind. With a sum file the graph only corrects the version of a module the files name; with none, the build list is the bound.
a version a Socket replace names, unless the build definitely does not select it go keeps no zip line for a replaced version, and an untidy member or an unpruned go.mod does not require a module only a dependency needs. Without this a pruning scan dropped the record of a patch the build still links.

A use member is left out (go builds it from its directory). A module under a go.work that does not use it is scoped to its own files.

The walk stays for --global and --global-prefix, and where the files are not the build's or cannot be read: -modfile, a workspace go refuses (invalid GOWORK, an unparseable or empty work file), a member whose go.mod cannot be read or lies outside the repository, an unreadable sum file, and a workspace whose sum files do not list what it links (no sum file, or a member with an unpruned go.mod and no go.sum) when the module cache cannot complete its graph.

A defect of main this also removes

The third row is not only for workspaces. On main, in a go 1.16 module (real Go): patch a module only a dependency requires, run go mod tidy, and the next scan --sync prints "GC: pruned 1 manifest entry and removed 2 orphan files" for a patch the build still links; apply --check then exits 1 over an orphan replace. Here the scan says "1 already patched" and apply --check passes.

Deleted

workspace_in_effect (the crawler's own test for a workspace) and the hand-built ModuleGraph in go_workspace::module_graph, which now asks GoWorkspace::module_graph. go_build_list::module_selection is the module-mode half of project_selection under its own name; project_selection calls it.

Behavior change

  • A scan in a workspace (agent, hosted and vendored use this crawl) finds the modules the workspace builds, not the cache. An agent scan that failed only over other projects' modules exits 0.
  • scan --prune and --sync in a workspace drop the records of modules it does not build, as in a module. Run on the tree main left with four records: one pruning scan reports the three failures once more, prunes them and keeps the live replace; apply --check then passes.
  • A Socket-replaced module the files cannot place stays in the crawl in both modes. In an unpruned module this also keeps the record of a replaced module the build has since dropped, until it is removed by hand; a pruned go.mod still drops it.
  • No JSON shape, flag, default or file format changes. CLI_CONTRACT.md and docs/ do not describe the crawl's scope, so neither is touched.

Risk

The scope could miss a module the build links in a state not modelled here. The scan would then not offer its patch, and a pruning scan would drop its record. Against that: four sources are unioned, every unreadable state falls back to the walk, and the cases below ran with real Go.

Layout (real Go 1.24.7, a module cache of 9 modules; packages a scan finds) main here
one member, no go.work.sum; from the root and from the member 9 found 1
two members, one never tidied, sums only in go.work.sum 9 2
untidy member, transitive module patched, go.work.sum deleted and rewritten by a build; then scan --sync, apply --check 9 2; record kept, in sync
GOWORK names ci.work (sums in ci.work.sum) 9 2
a module under a go.work that does not use it 9 1
the same module outside any workspace 1 1
go 1.16 module, transitive module patched, after go mod tidy 5; record pruned, --check exit 1 6; kept, in sync

Review

/code-review high ran five times; each finding was run before it was acted on.

Fixed, each with a test: the sum file's name under GOWORK; a missing and an out-of-date sum file; the replaced-module rule in module mode and for an answer of "nothing shows which version is built"; a module the workspace does not use; a work file with no use; workspace members in the scope; one graph read for a workspace instead of one per replace.

One fix was taken back: pass three asked that a requirement on a use member count as read, and pass four showed with real Go that go does look that go.mod up (the build fails without it). The test now models the layout that builds, with a directory replace.

No sixth pass ran: the last pass's three findings and Bugbot's one are fixed and unit-tested, but not reviewed again.

Left out

  • A version the build does not select is still offered, then refused (two members that require different versions of one module; in a module, the line a go get upgrade leaves in go.sum). This is the other half of scan --mode agent fails on, and records, Go modules the project does not build: every module in the machine's module cache under a go.work, and the old version a go get upgrade leaves in go.sum; apply or apply --check then exits 1 in CI #2413. Not offering it turns an exit 1 into an exit 0 that the contract does not settle, and belongs in the scan's selection, in a file an open agent PR edits.
  • A module-to-module replace whose target no sum file lists yet is not added. No mode can patch a replacement target (go does not chain replaces), so only its refusal is lost, and only until the next build writes the line.
  • go_workspace::load_file reads go.work.sum whatever the work file is called. The crawler reads the right file itself; the loader's other readers are not changed here.
  • The scope is read on the runtime, not the blocking pool. It is a handful of stats and small reads, the same calls every other reader of the workspace makes inline. The walk and the lookups are still one blocking-pool task.
  • Not run: Windows and macOS (no cfg code changed), and Go releases other than 1.24.7.

Verification

CI: 54 checks pass on the head (3cce1b9). Bugbot reviewed 789ebfb and found one issue, fixed in the head: a member with no go.sum and an unpruned go.mod that has requires ended the scope and kept the walk; it is now bounded by the module graph, as a workspace with no sum file is. Its thread is answered and resolved. Bugbot has not reviewed the head.

Local, with real Go 1.24.7 and SOCKET_PATCH_GO_E2E_REQUIRED=1:

  • On the head: cargo clippy --locked --workspace --all-features -- -D warnings is clean. Core's Go unit tests (crawler, workspace, build list): 88 passed. Eight CLI suites, 382 passed and 0 failed: e2e_golang, e2e_golang_build, e2e_golang_workspace_build, e2e_golang_hosted_build, e2e_golang_hosted_state, e2e_vendor_golang_build, scan and in_process_scan. The table above ran again on it.
  • On the commit before it (789ebfb, which differs in go_crawler.rs alone): core's 8,809 unit tests passed, and twelve CLI suites (those eight, apply, cli, e2e_vex_lockfile and ecosystem_dispatch_e2e) passed 1,098.
  • Two commits back (b5fbd68): 31 CLI suites (the twelve, and the vendor, vex, rollback, remove, hosted-memory and production suites) passed 1,998.

git diff --stat: 4 files, +745 −88. Production +268 −64 (go_crawler.rs +245 −59, go_build_list.rs +11, go_workspace.rs +12 −5). Tests +477 −24.


Generated by Claude Code

Refs #2413.

Assisted-by: Claude Code
@mikolalysenko Mikola Lysenko (mikolalysenko) added arch-refactor PR opened by the scheduled architecture refactor routine refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code labels Dec 2, 2026
With a go.work in effect the project crawl walked the whole module
cache, so a scan reported every module any project on the machine had
downloaded, sent them to the patch API by name and offered each a
patch. The ones the workspace does not build failed at apply time, the
scan exited 1, and their records stayed in the manifest.

The crawl of a workspace is now scoped as a module's is: to the zip
lines of the sum files go reads for it (each use member's go.sum and
go.work.sum) and the requires of each member, read through the
workspace loader hosted and vendored modes use. The walk stays for
--global and --global-prefix, and where go refuses the workspace or
one of its files cannot be read.

Refs #2413.

Assisted-by: Claude Code
go records no zip line for a version a replace takes over, and keeps
no member's go.mod tidy in a workspace. A patched module that only a
dependency requires was therefore in the scoped crawl only until
go.work.sum was written again: the next scan no longer saw it, and a
pruning scan dropped the record of a patch the build still links.

The scope now adds each version a Socket replace of go.work or of a
member names, while the workspace's module graph selects that version
or cannot say, which is the answer apply goes by. A module the build
dropped or moved past is left out, so its record is pruned as before.

Refs #2413.

Assisted-by: Claude Code
Runs the binary over a module with real go: on its own, as the one
member of a workspace, and under a go.work above it, with another
project's module in the same module cache. Each scan names the one
module the build links to the patch API.

Refs #2413.

Assisted-by: Claude Code
Review of the workspace scope found two.

go names a workspace's sum file after the work file, so under
GOWORK=/x/ci.work it reads and writes ci.work.sum. The scope read
go.work.sum in that directory whatever the file was called, and so
missed the modules only that file records.

The rule that keeps a Socket-replaced module in the crawl ran for a
workspace alone. A module with an unpruned go.mod (go 1.16 or older)
has the same gap, and had it before this branch: go mod tidy drops the
zip line of a replaced module that only a dependency requires, the
next scan no longer sees it, and a pruning scan drops the record of a
patch the build still links. Both modes now ask the question apply
asks of each version a Socket replace names.

Refs #2413.

Assisted-by: Claude Code
A second review pass found the scope still short in one state and
still walking in another.

A workspace with no sum file (go writes it at the first build that
needs it, and git often ignores it) can link a version a dependency
raised past every member's require. No member's go.sum lists that
version, so the scoped crawl missed it. The scope now adds the
versions the workspace's module graph selects, and keeps the walk when
a go.mod the graph needs is not in the module cache.

A module under a go.work that does not use it still walked the whole
cache. go refuses to build it there; it is now scoped to its own
go.mod and go.sum, which are what GOWORK=off builds.

The workspace's graph is read once for both this and the replaced
modules, and the module-mode answer is the half of project_selection
that module mode already used, under its own name.

Refs #2413.

Assisted-by: Claude Code
A third review pass found two more states where the scope fell short.

A sum file that is there but behind (written before a dependency
raised a module) was trusted as the whole record, so the raised
version was missed. The versions the workspace's module graph selects
are now added whether or not the sum file is there; the walk is kept
only when there is no sum file and the graph is not whole.

Members that require each other read as a graph with a hole: go
builds the required member from its directory and never downloads its
go.mod, so the lookup in the module cache failed, and a workspace
with no sum file kept walking. A requirement on a use member now
counts as read.

The sum file's lines join the scope before the replaced modules are
looked for, and an unreadable sum file returns before the graph is
read.

Refs #2413.

Assisted-by: Claude Code
A fourth review pass ran the last change against real go and found it
wrong. go 1.24.7 does look up the go.mod of the version one member
requires of another, and that go.mod takes part in version selection:
the build fails when it is not there. So a requirement on a use member
is no longer counted as read. With the directory replace go needs for
an unpublished member, the graph reads the member's own go.mod and is
whole without any exemption; the test now models that layout.

A use member is left out of the scope: go builds it from its
directory, so a copy of it in the cache (a GOWORK=off build's) is not
this build's, and a patch offered for it would be refused.

A go.work that uses no module keeps the walk, as every other
workspace go refuses does. It scoped the crawl to nothing, and a
pruning scan would have dropped every Go record.

Refs #2413.

Assisted-by: Claude Code
A fifth review pass found three things.

A Socket-replaced version stayed in the scope only when the build
selected it or the answer was unknown. An unpruned go.mod with a
leftover zip line and a module graph the cache cannot complete gives a
third answer, that nothing shows which version is built. The module
was dropped there, and a pruning scan would have dropped its record.
It now stays: only a definite answer that it is not built drops it.

With a sum file the graph added every module any loaded go.mod
requires, a dependency's test-only requirements among them. A sum
file lists what the build links, so the graph now only corrects the
version of a module the files name. With no sum file the build list
is still the bound.

The members' module paths are read once to leave them out of the
scope, where each entry parsed every member's go.mod.

Refs #2413.

Assisted-by: Claude Code
@mikolalysenko Mikola Lysenko (mikolalysenko) changed the title Scope the Go crawl of a go.work workspace to the modules its sum files record Scope a Go workspace's scan to the modules it builds, not the machine's module cache Dec 2, 2026
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 2, 2026 03:08
@cursor

cursor Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core scan/scope logic for all Go local crawls (workspaces and modules) with many fallbacks to full-cache walk; mistakes could omit patchable modules or still over-report, but extensive unit and e2e coverage mitigates this.

Overview
Go workspace scans no longer enumerate the entire GOMODCACHE. Local crawls now use an async, workspace-aware go_sum_scope that unions each use member’s go.sum/requires, the workspace sum file (go.work.sum or the name derived from GOWORK), MVS selections when sums are missing or stale, and Socket-owned replaces the build may still link—while excluding use members built from disk.

Module-only behavior is tightened in the same path: module_scope/own_module_scope replace the old sync single-module logic, and Socket replaces only stay in scope when module_selection says they may still be built.

Supporting API: go_build_list::module_selection exposes module-mode selection; GoWorkspace::module_graph() centralizes graph completeness/selected versions. The old workspace_in_effect bail-out (full cache walk) is removed.

An e2e agent scan test asserts three layouts (plain module, go.work beside go.mod, parent go.work) each POST only the one PURL the build links, ignoring a stranger module staged in the shared cache.

Reviewed by Cursor Bugbot for commit 789ebfb. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 789ebfb. Configure here.

Bugbot's finding. A workspace member with no go.sum and an unpruned
go.mod that has requires made the whole scope give up and walk the
cache, before the workspace's sum file or its module graph were read.
What such a member's requires need in turn is named by the graph, so
the workspace is now treated as one with no sum file: every version
the graph selects is added, and the walk is kept only when a go.mod
the graph needs is not in the module cache.

Refs #2413.

Assisted-by: Claude Code
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
Inside a Go workspace (go.work), a project scan used to walk the machine's entire module cache, so it reported other projects' modules, sent their names to the patch API, and failed at apply time with exit 1. This PR scopes the workspace crawl the same way module mode already is: the members' go.sum and requires, the workspace sum file, the versions the module graph selects, and the versions Socket replaces name. It falls back to the old full walk whenever those files can't be trusted.

Risk: medium. The change only narrows what a project scan offers; --global is untouched and every unreadable or unknown state falls back to the walk. The failure mode is a missed module: a pruning scan (--prune/--sync) could drop the record of a patch the build still links. The PR guards that with the module-graph union and by keeping Socket-replaced versions unless the build definitely doesn't select them.

Look here

Verified

  • Read the full diff (4 files, +745 −88). No CHANGELOG.md change, no debug leftovers, no new flags or JSON shape. Windows: the work-sum path is built with OsString::push and adds no new cfg code.
  • Ran cargo test -p socket-patch-core --lib go_ on the head: 595 passed.
  • CI on 3cce1b9: ci-ok and clippy green, and every other check passed or was skipped.
  • The one Bugbot thread is fixed and resolved.

Changes I made: none.

Open questions


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 2, 2026
@mikolalysenko
Mikola Lysenko (mikolalysenko) added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 6eb4eb9 Dec 2, 2026
54 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the arch-refactor/2413-go-workspace-crawl-scope branch December 2, 2026 15:05
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-refactor PR opened by the scheduled architecture refactor routine Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants