Skip to content

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

Backlog

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.

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 main applies 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 replace for it into go.work, and prints "Patched 1 package". Go ignores that replace, and the program links the unpatched v0.8.1.
  • apply --check then exits 1 and says "Run socket-patch apply to regenerate them"; apply gives the same refusal.
  • vex writes one not_affected statement, "Patched via Socket patch", for the product with the subcomponent pkg: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.rs gains a reader of exclude lines, in the single-line and the block form, through for_each_directive_body like its require reader (line 559).
  • GoWorkspace holds the union of its members' excluded module@version pairs. read_graph_from does not queue a requirement on one, and selected_versions and selection do not count one, from a member's go.mod or 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

  1. added
    bug
    arch-audit
    on Dec 1, 2026
  2. added a commit that references this issue on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedarch-auditbugpriority:p2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions