Repository navigation
In a go.work workspace, the patch for the Go module version an exclude keeps the build on is refused ("the build selects" the excluded version), and a patch for the excluded version is reported applied with a replace Go ignores: the module graph reader never reads exclude #2412
Description
[agent] Found by the architecture refactor routine on main at 1b3f422, with real Go 1.24.7.
The problem
Since Go 1.16, an exclude in a main module's go.mod makes Go ignore every requirement on that version, whichever go.mod of the graph it comes from. Its main use is to hold a module below what a dependency asks for. In a workspace each use member's excludes apply to the whole graph.
socket-patch answers "which version does the build select" offline, and every Go path asks that one question (core vendor/go_build_list.rs and vendor/go_workspace.rs). The reader takes require and replace lines and never reads exclude. Nothing in core parses the directive: in the Go files the word occurs in a comment on the lines the go.mod editor preserves, in the build-tag code and in a test's name.
- On a go ≥ 1.17
go.modin module mode the answer is therequireline, which Go keeps at the selected version. That answer is right. - In the two cases answered by MVS over the module graph, a dependency's requirement on the excluded version raises the answer past the version Go builds:
- a workspace (
selected_versionsandGoWorkspace::selection, corevendor/go_workspace.rs, lines 539 to 565 and 656 to 700); - a
go.modolder than go 1.17 whosego.sumhas a zip line above itsrequire(module_graph, lines 572 to 591, the path On a go 1.16 go.mod, Go apply, apply --check and vex trust a stale go.sum zip line left by a downgrade, so they report an inert replace as applied (or refuse a selected version) #1862 added).
- a workspace (
What the user gets, in a workspace whose member requires github.com/pkg/errors v0.8.1, excludes v0.9.1, and depends on a module that requires v0.9.1:
- The patch for v0.8.1, the version Go builds, is refused: "the build selects github.com/pkg/errors v0.9.1, not the patched v0.8.1 […] (get a patch for v0.9.1, or require v0.8.1)". The project already requires v0.8.1, and the build never selects v0.9.1. The scan exits 1 and no command of
mainapplies the patch. - On a machine whose module cache also holds v0.9.1 (any other project there built it), the scan patches v0.9.1 as well, writes a
replacefor it intogo.work, and prints "Patched 1 package". Go ignores that replace, and the program links the unpatched v0.8.1. apply --checkthen exits 1 and says "Runsocket-patch applyto regenerate them";applygives the same refusal.vexwrites onenot_affectedstatement, "Patched via Socket patch", for the product with the subcomponentpkg:golang/github.com/pkg/errors@v0.9.1, a version the product does not contain. It omits v0.8.1 with a warning.
The same go.mod with no go.work beside it is patched correctly, so the two graph modes disagree with the plain one and with Go.
The contract states the rule this breaks (CLI_CONTRACT.md, lines 962 to 970): apply refuses a replace "for a version the build list doesn't select", and in workspace mode that is "MVS over the members' module graph".
The same class was fixed six times, one rule of Go's loader at a time: 1d523d3 (#391, #392, #509), 68d2a49 (#1751, the members of a workspace), 103a664 (#1753), d3d3154 (#1823, a replace target), cf9fd1f (#1840), e0916be (#1862, a stale go.sum line). exclude is the directive of go.mod that changes the selection and that the reader still leaves out.
Evidence
With real Go
github.com/rs/zerolog v1.26.1 requires github.com/pkg/errors v0.9.1. The project, built by go mod tidy:
$ cat app/go.mod
module example.com/app
go 1.22
require (
github.com/pkg/errors v0.8.1
github.com/rs/zerolog v1.26.1
)
exclude github.com/pkg/errors v0.9.1
$ cat go.work
go 1.22
use ./app
main.go imports github.com/pkg/errors and github.com/rs/zerolog/pkgerrors and prints errors.New("x").Error(). A stand-in patch API serves one patch for each of v0.8.1 and v0.9.1; each makes that call return PATCHED-<version> x. The binary is built from main.
| Layout | Go selects (go list -m) |
scan --mode agent |
The program prints |
|---|---|---|---|
the module alone, go 1.22 |
v0.8.1 | patches v0.8.1, exit 0 | PATCHED-v0.8.1 x |
the module alone, go 1.16 |
v0.8.1 | patches v0.8.1, exit 0 | PATCHED-v0.8.1 x |
the module under the go.work above, a module cache of its own |
v0.8.1 | refuses v0.8.1, exit 1 | x |
| the same, the machine's cache also holds v0.9.1 | v0.8.1 | refuses v0.8.1, patches v0.9.1, "Patched 1 package", exit 1 | x |
| two members: one requires v0.9.1, the other requires v0.8.1 and excludes v0.9.1 | v0.8.1 | the same | the unpatched string in both |
the module alone, go 1.16, with v0.9.1's zip line left in go.sum |
v0.8.1 | the same | x |
The third row, in full:
$ go list -m github.com/pkg/errors
github.com/pkg/errors v0.8.1
$ socket-patch scan --mode agent --yes -e golang # exit 1
Found 15 packages (15 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: a replace pinned to v0.8.1 would be ignored and the build would link the unpatched module; nothing written (get a patch for v0.9.1, or require v0.8.1)
Applied 0 of 1 patch: 1 failed.
Error: Some patches could not be applied.
The fourth row:
$ socket-patch scan --mode agent --yes -e golang # exit 1
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: […]
Applied 1 of 2 patches: 1 failed.
Error: Some patches could not be applied.
HIGH github.com/pkg/errors@v0.9.1 CVE-2026-1002
Patched 1 package, fixing 1 vulnerability (1 high).
$ tail -1 go.work
replace github.com/pkg/errors v0.9.1 => ./.socket/go-patches/github.com/pkg/errors@v0.9.1
$ go list -m github.com/pkg/errors && (cd app && go run .)
github.com/pkg/errors v0.8.1
x
$ socket-patch apply --check # exit 1
Error: Patches are OUT OF SYNC:
[…]
pkg:golang/github.com/pkg/errors@v0.8.1: patched version v0.8.1 is not the version the build selects (v0.9.1) — go would link the UNPATCHED module
Run `socket-patch apply` to regenerate them.
$ socket-patch apply # exit 1, the same refusal
$ socket-patch vex --product pkg:golang/example.com/app@v0.0.0 # exit 0
Warning: omitting pkg:golang/github.com/pkg/errors@v0.8.1 from VEX: the patched files still hold the original content (not_applied)
Wrote OpenVEX document with 1 statement
That statement is not_affected for the product, with the subcomponent pkg:golang/github.com/pkg/errors@v0.9.1.
Go refuses the one layout where the answer from require could be wrong: a go.mod that requires the version it excludes fails with "go: updates to go.mod needed". So the plain reader needs no change.
Unit tests that fail on main
Two tests beside the existing ones in core vendor/go_workspace.rs, with that file's helpers:
/// A member's `exclude` makes go ignore every requirement on that
/// version, so a dependency that asks for it does not raise the selection.
#[tokio::test]
async fn an_excluded_requirement_does_not_raise_the_selection() {
let tmp = tempfile::tempdir().unwrap();
let root = &tmp.path().join("ws");
let cache = &tmp.path().join("cache");
write(root, "go.work", "go 1.22\n\nuse ./app\n");
member(
root,
"app",
"1.22",
&format!(
"\nrequire (\n\t{DEP} v1.0.0\n\texample.com/mid v1.0.0\n)\n\nexclude {DEP} v1.1.0\n"
),
);
cached(cache, DEP, "v1.0.0", "1.21", "");
cached(cache, DEP, "v1.1.0", "1.21", "");
cached(
cache,
"example.com/mid",
"v1.0.0",
"1.21",
&format!("\nrequire {DEP} v1.1.0\n"),
);
assert_eq!(
graphed(root, cache).await.selection(DEP, "v1.0.0"),
Selection::Selected
);
}
/// The module-mode graph of a go 1.16 go.mod.
#[tokio::test]
async fn module_graph_drops_an_excluded_requirement() {
let tmp = tempfile::tempdir().unwrap();
let (root, cache) = (tmp.path().join("app"), tmp.path().join("cache"));
let go_mod = format!(
"module example.com/app\n\ngo 1.16\n\nrequire (\n\t{DEP} v1.0.0\n\texample.com/mid v1.0.0\n)\n\nexclude {DEP} v1.1.0\n"
);
cached(&cache, DEP, "v1.0.0", "1.16", "");
cached(&cache, DEP, "v1.1.0", "1.16", "");
cached(
&cache,
"example.com/mid",
"v1.0.0",
"1.16",
&format!("\nrequire {DEP} v1.1.0\n"),
);
let graph = module_graph(&root, &go_mod, Some(&cache)).await;
assert!(graph.complete, "{graph:?}");
assert_eq!(graph.selected.get(DEP).map(String::as_str), Some("v1.0.0"));
}Both fail on main (cargo test -p socket-patch-core --lib, run with a probe_ prefix on each name):
an_excluded_requirement_does_not_raise_the_selection:
left: Other("v1.1.0")
right: Selected
module_graph_drops_an_excluded_requirement:
left: Some("v1.1.0")
right: Some("v1.0.0")
How often
exclude is uncommon, and it takes a workspace or a go.mod older than go 1.17 on top. Where a project has both, holding a module below a dependency's requirement is what exclude is for, and the module held back for a reason is a likely one to have an advisory.
Planned fix
One rule in the one graph reader, which serves the workspace and module_graph:
go_mod_edit.rsgains a reader ofexcludelines, in the single-line and the block form, throughfor_each_directive_bodylike itsrequirereader (line 559).GoWorkspaceholds the union of its members' excludedmodule@versionpairs.read_graph_fromdoes not queue a requirement on one, andselected_versionsandselectiondo not count one, from a member'sgo.modor a dependency's.- The two tests above, a two-member case, and a row with the real toolchain in
e2e_golang_workspace_build.rs. - The contract's sentence on workspace mode names the rule.
Vendored mode, run: in the workspace of the third row socket-patch vendor and vendor --dry-run refuse with the same sentence ("Cannot vendor pkg:golang/github.com/pkg/errors@v0.8.1: the build selects github.com/pkg/errors v0.9.1 […]"), exit 1, and write nothing. On the module alone the same record passes that gate and the run goes on to ask the patch service (which the stand-in does not serve, so that run stopped there). The gate is core vendor/golang.rs, line 264, vendor_go_version_not_selected.
Not run: hosted mode and the in-memory hosted engine, which ask the same question before they write, and Go releases other than 1.24.7.
Generated by Claude Code
Activity
refactor: #2412, #2413 and #2414 filed; Go ran