Skip to content

Keep Go module copies under .socket out of Bazel packages - #2123

Merged
Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
bazel-small/21-go-bazelignore
Nov 19, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
bazel-small/21-go-bazelignore

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

Go vendored and agent mode in a Bazel workspace: socket-patch names its module copies in the root's .bazelignore, so the build stays patched after bazel run //:gazelle, and bazel build //... passes.

Two commits on main at 89c482d80. The first (tests only) was pushed first, so that CI records what today's code does: it is green on GitHub's Linux runners on Bazel 9.3.2, 8.8.2 and 7.7.2 (run 44193996968: the four Go flows pass while asserting today's break). The second (the fix, cdf3a5fb6) is green too, all 63 checks. Because the shared test harness changed slightly, its run covers the Bazel legs of all four ecosystems on 9.3.2, 8.8.2 and 7.7.2: Go (four flows: 192 s, 243 s, 215 s), npm (64 s, 38 s, 40 s), PyPI (52 s, 26 s, 34 s) and RubyGems (65 s, 41 s, 45 s). The Windows and macOS test jobs were run by dispatch on the same commit (Windows, macOS); both are green. The macOS run needed one rerun of one job: utils::process::tests::an_interrupt_pass_ends_the_whole_build_group failed once and passed on the rerun; that test and its file (utils/process.rs) are not part of this change. main has moved since the branch was cut; the pull request merges with it without a conflict, and CI tests the merged tree.

Decisions for the owner

(a) socket-patch writes a Bazel file for the first time. Decided by the owner on 2026-11-17: yes. It is the .bazelignore at the root of the workspace, which gets two lines for each directory of copies:

# socket-patch: patched Go module copies, not Bazel packages (to opt out, comment out the next line)
.socket/vendor/golang

What the yes costs:

  • The line also takes the copies out of every glob() above them. That is (b), and a user can decline it.
  • While the file is edited, socket-patch holds an advisory lock on the workspace root directory. That is (d).
  • After an upgrade, the first command that reaches this step creates or changes .bazelignore in every Bazel workspace that already has a Go copy. The apply of a CI install is such a command in an agent-mode project. A job that checks for a clean tree sees that change until the file is committed.

The alternative was a warning that names the line to add. That leaves the break in place for everyone who does not act on it: on today's code the build breaks at the first bazel run //:gazelle, and at once for bazel build //... when the module ships a BUILD file.

(b) The line also takes the copies out of every glob() above them. A user can decline it. Measured once as a build on Bazel 9.3.2 (macOS arm64, sandboxed, gazelle 0.54.0, rules_go 0.65.0, Go 1.26.0), in a scratch copy of the Go fixture with a vendored copy, outside the committed tests and not repeated for this revision. A genrule there runs the Go SDK's go build . over glob(["**/*.go", "**/go.mod", "**/go.sum"]):

State The glob gives That genrule The rules_go target
No line (today), before a gazelle run the project's three files and the copy's two builds, prints the patched marker builds patched
No line (today), after bazel run //:gazelle wrote a BUILD file into the copy the project's three files fails: replacement directory ./.socket/vendor/golang/... does not exist fails: No repository visible as '@rules_go'
With the line the project's three files fails with the same error, from the first build builds patched
With the line, the copy's files named as labels beside the glob those and the two labels builds patched not run
With the line, --spawn_strategy=local the project's three files builds patched not run

The workspace this makes worse has such a target, never runs gazelle, and patches a module without a BUILD file: it builds today and fails with the line. That workspace comments the path line out and leaves the comment line above it:

# socket-patch: patched Go module copies, not Bazel packages (to opt out, comment out the next line)
# .socket/vendor/golang

After that no command adds a line for that directory, warns about it, or touches the two lines again. The commented path may end in one / or start with ./. Deleting both lines is no opt-out: the next command writes them back. Neither is commenting out both lines, or changing the path line in another way (a space after it, a second #). socket-patch knows its two lines, and the opt-out, by the start of the comment line, # socket-patch:, so a later release can reword the rest of that line. Recommended: accept.

(c) An empty .bazelignore the user had is deleted with the last pair. When the last pair is removed and nothing else is left, the file is deleted, whether socket-patch created it or the user had it empty; socket-patch keeps no record of which. An empty file that never held a pair is left alone. Every other file comes back byte for byte, a missing final line break and a last byte that is a carriage return on its own included. Recommended: accept.

(d) The file is edited under an advisory lock on the Bazel root directory itself. Projects under one Bazel root share the file, so each edit, from its read to its write, is made under a lock: flock on a handle to the root directory, released when the edit is done. No lock file is made, in the workspace or anywhere else (the previous revision kept one per Bazel root in the system's temporary directory, for ever). A command that has nothing to change takes no lock. Where the lock cannot be taken, the edit is made without it and one race is back: a pair that one project adds while another project's pair is removed can be lost until the next command in that project. That is the case on Windows (a directory cannot be opened this way), on a file system without such locks, and when the lock stays held for a second. Pairs that are only added are safe without it: each is one write at the end of the file. Measured through the built binary, two commands at the same moment in sibling projects, 200 rounds: no added pair lost with the lock, 47 lost without it. Recommended: accept.

(e) A Bazel root that socket-patch does not look for gets no line and no warning. socket-patch looks for the workspace file in the project's own directory and, when the project is in a git checkout it trusts, up to the root of that checkout. Nothing above that is read. So three shapes get nothing, and the docs tell their users to add the line by hand, with the directory's path from that root:

  • the Bazel root is above the root of the checkout (the project is a git submodule, or several checkouts sit below one workspace);
  • the project is in no git checkout (a Docker build context, an unpacked source archive) and sits below its Bazel root;
  • the checkout's .git belongs to another user. This is the rule socket-patch already has for the repository root: on Unix .git must belong to the user, to root, or to the user sudo ran for (a root process takes any owner's).

The second shape had a warning in the previous revision: the walk went on above the project, read the .bazelignore it found there, and warned on every command until that file held the line. The third review asked for it to go. It read a file above a project that is in no checkout, which nothing else in socket-patch does, and an unrelated WORKSPACE file in a parent directory made every command warn. Two ways to give these shapes a warning, neither built: allow this one read above the boundary (the warning then ends once the line is there), or accept a warning that never ends. Recommended: accept the limit.

(f) In a Bazel workspace nested in a larger one, the line is in the inner file only. The Bazel root is the nearest directory with a workspace file. With outer/MODULE.bazel, outer/inner/MODULE.bazel and the project in inner, the line goes into inner/.bazelignore, which only a run from inner reads. Measured on Bazel 9.3.2 (macOS arm64, gazelle 0.54.0, rules_go 0.65.0), in two scratch workspaces made from the Go fixture, outside the committed tests:

Run Result
From inner: bazel run //:consumer builds, prints the patched marker
From outer: bazel query //... lists the targets of inner as //inner:...: the inner tree is part of the outer workspace's packages
From outer, with a BUILD file the module ships in the copy: bazel build //... and bazel query //... both report error loading package 'inner/.socket/vendor/golang/...'
The same query from inner passes: the inner file is honoured there
From outer: bazel run //:gazelle writes a BUILD.bazel into the copy
From inner, after that: bazel run //:consumer fails: Unable to find package for @@[unknown repo 'rules_go' ..., the break this change is about
With inner/.socket/vendor/golang in outer/.bazelignore: the gazelle run, the build and the query from outer, then the build from inner gazelle writes nothing into the copy, no error names the copy, and the build from inner prints the patched marker

The writes are not widened for this. The docs name the limit and the line to add in the outer file, unless that file already names inner. Recommended: accept.

(g) A question: should --json name the file when it changed? Under --json a file that could not be updated is a warnings[] entry. A file that was created, changed or deleted is not mentioned; a human run reads Updated <path> or Removed <path>. The envelope has no place for it today. A hosted scan lists the lock and registry files it rewrote (redirect.rewrittenFiles), rollback gives a count (hosted.editedFiles), and the vendored and agent commands, which are the ones that write a Go copy, name no file they wrote, go.mod included. So nothing was added. If it should be there, it is a new additive key on those commands' envelopes: a few lines and a contract entry.

What it contains

Two signed commits on 89c482d80.

  1. 6f85989f7 Test the vendored and agent Go flows through a gazelle run on real Bazel. Tests only: a gazelle target in the Go fixture, and three real-Bazel tests that pass on today's product code while asserting today's break.
  2. cdf3a5fb6 Keep Go module copies under .socket out of Bazel packages. bazel::bazel_root and bazel::ignore::sync in core, one helper in the CLI with ten call sites, the same three tests flipped to the fixed outcome, tests without Bazel, docs. The test harness gains a runner without --json, for what a person reads.
Added / removed lines
Implementation (crates/*/src outside test modules) +413 / -3 (262 code lines)
Tests (test files, test modules, workflow, budget file) +1946 / -43
Fixture +15 / -1
Docs +113

Implementation by file: bazel/ignore.rs 264 lines (167 code), bazel/mod.rs 51 (25), the helper in commands/mod.rs 58 (42), its ten call sites 37 (25), lib.rs, repo_root.rs and go_mod_edit.rs one line each. Code lines are counted with the rules of scripts/loc-budget.py, where the new group bazel is at 192 / 192. lock-readers and lock-writers are unchanged and were over on main.

What today's code does (commit 1)

On Bazel 9.3.2, 8.8.2 and 7.7.2 with gazelle's go_deps:

  • scan --mode vendored, and agent mode (scan --mode agent, apply): the next build prints the patched marker. Then bazel run //:gazelle writes a BUILD.bazel into the copy, and the build fails: No repository visible as '@rules_go'.
  • A module that ships its own BUILD file: bazel build //... fails right after the command: No repository visible as '@io_bazel_rules_go'.
  • The undo is clean after vendor --revert; an agent rollback leaves the emptied .socket/manifest.json.

What changes

Only for a project in a Bazel workspace: its directory, or one above it up to the root of its git checkout, holds a regular file MODULE.bazel, REPO.bazel, WORKSPACE or WORKSPACE.bazel. Those are the names Bazel's client looks for (src/main/cpp/workspace_layout.cc line 36, the same at the tags 7.7.2, 8.8.2 and 9.3.2). The nearest such directory is the Bazel root; a directory of that name does not count. The checkout is the one socket-patch already trusts as the repository root, and a project in no such checkout looks at its own directory only: see (e). Without a workspace file there, nothing is read or written.

  • While <project>/.socket/vendor/golang or <project>/.socket/go-patches exists, the root's .bazelignore names it by its path from the root, under the comment line shown above. The pair is added at the end of the file, in the file's line breaks, and the file is created when missing. Never .socket alone.
  • Nothing is added when a line already covers the directory (the path or a directory above it, with or without a trailing / or a leading ./), or when the user declined it as in (b). A line with a blank before or after it, a pattern or a string prefix covers nothing.
  • socket-patch knows its pair by the start of the comment line, # socket-patch:. A path line under a line that starts so is its own, whatever the rest of that line says, and only such a line is ever removed.
  • Once the directory is gone the pair is removed, and the file is the one of before, byte for byte. A file whose last line has no line break gets one and the pair without a final one, and loses exactly that line break again with the pair. Only a directory that is not found is gone: one that cannot be looked at (a directory above it that may not be searched) keeps its pair, and gets none.
  • The line follows the directory, not the replace: rollback --preserve-state keeps the vendored copy, and with it the line.
  • A read-only file is neither replaced nor deleted, on an add or on a removal. A symbolic link is never written through.
  • A file that is not updated is the new warning bazelignore_not_updated: it could not be read or written (a directory or a link in its place, a read-only directory, a read-only file). The detail names the line to add, or says that the lines of a copy that is gone can be deleted. The command succeeds as before.
  • A human run reads Updated <path> or Removed <path> on stdout when the file changed, once per file, and the warning on stderr. --json carries the warning in warnings[]; there is no new field (see (g)).
  • A dry run of any command writes nothing. Neither does a scan that socket.yml lets write nothing (patches.enabled: false).

Which commands keep the file in line: every command that can write or remove a copy, once, at its end: scan, get, apply, vendor, vendor --revert, rollback, remove, repair. Not in a --global run.

  • A run whose --ecosystems leaves golang out adds no line. It still takes away the pair of a copy that is gone, because remove of a Go patch and vendor --revert remove the Go copy whatever that option says.
  • An eject (vendor in a hosted project) does it after its rollback, so a rolled-back eject leaves no pair for a copy that is gone again.
  • A hosted scan does it only when it took the apply lock, which it takes only when it has something to write.
  • scan --prune comes to the file a second time after a pass that removed a vendored copy. That second look only takes a pair away: it adds nothing and warns of nothing.
  • With a --manifest-path into another project, the agent copies are those of --cwd and the vendored ones those of the manifest's project, and the command brings the file of each project's Bazel root in line.
  • apply and vendor in a project with no manifest end before that step.

After the first review: ten findings

Finding What was done
No opt-out, and no word to the user The opt-out of (b); a human run is told that the file changed.
A Bazel root above the checkout boundary is silently skipped Stated as a limit: decision (e). The warning this review got for a project in no checkout came out again in the third review.
A removal could drop a pair a sibling project added at that moment The lock of (d). A removal against an add keeps the added pair in 25 of 25 rounds of a unit test (on Unix); without the lock the test fails.
A rolled-back eject left a pair for a copy that was gone The file is brought in line after the rollback. A flow test reproduced it first, and passes now.
The undo left a line break behind Byte-for-byte undo, as above.
A removal replaced or deleted a read-only file The file is opened for appending before any change.
Other ecosystems' vendored trees Not covered; see below.
Six call sites with different gates One helper, one gate, one call per command; a scan --prune comes a second time after a pass that removed a copy, only to take its pair away. A repair of another ecosystem adds no line for a Go copy, a prune pass no longer leaves a stale pair, no command warns twice.
A branch for a write inside a group commit that nothing reached Removed with its test.
Two walks up, on the async runtime One walk, on the blocking pool. Outside a Bazel workspace a run costs that walk and nothing else: for a project d directories below its checkout root, 2 path canonicalisations, d + 2 stats of .git and 4 (d + 1) stats of the four names. Counted from the code, not traced.

After the second review: one defect and ten smaller points

Point What was done
A .bazelignore whose last byte is a carriage return on its own lost that byte when the pair was added and removed again Fixed: the undo takes back exactly the line break that was written before the pair. Held by a unit test, a flow test and the round trips below.
Four wrong statements in this description Corrected, with every count taken again.
remove and vendor --revert with --ecosystems npm removed the Go copy and left its pair A run that leaves Go out now takes the pair of a copy that is gone away. It still adds nothing.
Updated was printed in words that fit only an added line The words now hold for lines that came and for lines that went. A removal that keeps the file and one that deletes it are asserted.
scan --prune came to the file twice, and a flag kept for the life of the process hid the second warning The flag is gone. Done differently from the proposal, which was one call made by the prune pass. That pass also follows the nested apply of scan --mode agent --prune, and it does not run at all when it cannot take the apply lock, so a scan that left its call to the pass could warn twice or leave a new copy without its line. Instead the scan's own call stays, and the pass's look only takes a pair away and reports nothing. The cost is in Caveats.
The near misses of the opt-out opt the user in again without a word The docs say what is no opt-out, and the commented path may now end in one / or start with ./.
A lock could be waited for where nothing could change: the file cannot be read and the project has no copy No lock there. A unit test holds it.
The root of the file system could be taken for a Bazel root, which Bazel's client never does for a directory below it The rule written for this came out again in the third review, with the walk above a project in no checkout that it belonged to.
A --manifest-path into another project was untested, and printed Updated twice for one file One flow test (one shared root, and two roots); the line is printed once per file.
The hosted scan's test for its apply lock was called unneeded Kept, and now held by a test. Without it a hosted scan in a project whose socket.yml says patches.enabled: false (report only, nothing is written) writes .bazelignore: measured with the test dropped.
The docs promised the line from commands that do not give it, and (a) lacked one cost The docs name the commands, from a run of twelve commands in both modes. (a) has the cost.

After the third review: nine findings

Finding What was done
A .git of another user above the project let socket-patch write there: with another user's /tmp/.git and /tmp/MODULE.bazel, a command in /tmp/job/proj created /tmp/.bazelignore Fixed. The checkout counts only when it is the one socket-patch trusts as the repository root, which is the existing rule and its own code, not a second rule. A unit test hands the walk a checkout of an owner that is not trusted.
A project in no checkout read the .bazelignore beside any workspace file above it, and warned on every command Removed, with the "read, never written" root and the rule about the root of the file system that only it needed. Such a project looks at its own directory only: decision (e). A flow test runs both modes below a Bazel root with no checkout: no line, no warning, and nothing above the project is read.
Three tests assumed that no .git lies above the temporary directory Gone with that walk. The unit tests and the flow tests pass with the temporary directory outside any checkout, and inside one that has a workspace file at its root. The one flow test without a checkout of its own sets GIT_CEILING_DIRECTORIES; without that it fails inside a checkout.
The whole sentence of the comment line was the only mark of ownership and of the opt-out, so a later rewording would orphan every pair this release writes The start of the line, # socket-patch:, is the mark. A pair under another wording stays, alone, while the copy is there, goes with the copy, and is declined by a commented-out path line; no second pair is appended.
Any failure to look at a copy directory counted as "gone", so a copy below a directory that may not be searched lost its pair Only "not found" is gone. Any other failure changes nothing for that directory. A unit test takes the search permission off the parent directory (Unix).
The lock was a file in the temporary directory that was never deleted, and the tests left one per run The lock is on the root directory itself: decision (d). No file is made anywhere, and the unit and flow tests leave the temporary directory empty (listed before and after).
Nested workspaces: a run from the outer workspace does not read the inner file Measured, and it is so: decision (f). Stated in the docs; the writes are not widened.
--json does not say that the file changed Nothing added, since the envelope has no place for it: question (g).
The docs of the test target said every test in it is ignored Corrected: the tests without Bazel run with a plain cargo test.

The review's two other points change nothing here: the line is on by default and takes the copies out of glob() (decided, (a) and (b)), and other ecosystems (Not covered).

Product code over the three reviews: implementation went from 289 to 416 added lines in the first, stayed at 416 in the second (code lines 273 to 269), and went to 413 in the third (code lines 269 to 262). In the third, bazel/ignore.rs lost 4 lines (8 code lines) and bazel/mod.rs gained one. What went: the root above a project in no checkout, with its flag and its warning, and the lock file's name and open options. What came: the trust rule, the mark, and a probe that tells "not found" from "cannot tell".

What it proves, and where

Everything ran locally on macOS arm64 with the pinned Bazel binaries, on the tree of cdf3a5fb6.

  • Real Bazel at the head, 9.3.2 / 8.8.2 / 7.7.2: 4 of 4 Go tests pass on each (legs 117 s, 124 s, 90 s; 39 to 57 s per test). The three kept-copy tests build the patched marker after the command and pass bazel build //... and a --lockfile_mode=error build. The two tests of a module without a BUILD file build it again after bazel run //:gazelle, which writes nothing. The agent test starts from a .bazelignore of the user's whose last line has no line break, and Bazel and gazelle honour the line that then ends the file. The undo removes the pair, and the build prints the unpatched marker again.
  • The other ecosystems' real-Bazel tests, 9.3.2: the test harness they share changed here, so each ran once: npm 2 of 2 (20 s), PyPI 1 of 1 (19 s), RubyGems 1 of 1 (37 s).
  • Real Bazel at commit 1: the commit is unchanged since the previous revision, where the same tests passed on main's product code on 9.3.2 while asserting the two errors above. Not run again for this revision.
  • The tests can fail: with the product change switched off, the three kept-copy tests fail on real Bazel 9.3.2 and the hosted one passes. For each behaviour this revision changed, a run with the behaviour taken out fails the test that holds it: the trust rule, the no-checkout rule (in the unit test and through the built binary), the mark (the whole sentence again), the probe (any failure counts as gone), the lock (none taken), the lock taken where nothing can change, and the flow test without its ceiling inside a checkout. The mutation runs of the earlier revisions (the opt-out and its spellings, the undo's line break, the lone carriage return, the read-only removal, the rolled-back eject, the prune pass, the dry-run gate, a run that leaves Go out, the human output, the hosted scan's lock test, REPO.bazel) were not repeated: their code and tests did not change, apart from the mark.
  • Byte round trips: 858 add-and-remove round trips over 102 starting files through the built binary: every mix of LF, CRLF and lone CR with and without a final line break, a byte order mark, bytes that are not UTF-8, NUL bytes, a one-megabyte line, only blank lines, the user's own covering lines, the declined forms, one and two pairs, a pair that is already there, lines appended after a pair, a project below the root. Every file came back byte for byte, the empty and the missing file of (c) excepted.
  • Two commands at once: the numbers of (d).
  • Without Bazel: 22 unit tests of the root finder, the line rules, the write and the lock; 17 flow tests through the built binary. Among them: ten commands with --dry-run in both modes write nothing; six scans under patches.enabled: false write nothing; a declined pair stays declined over scan, repair, apply, rollback and revert; a file that cannot be written is exactly one warning for each command, in the envelope and on a person's stderr, and the patch stands; a project in no git checkout below a Bazel root gets no line and no warning; the same run in a checkout without a Bazel workspace file writes the same files, byte for byte, and no .bazelignore (the timestamped marker of a vendored copy is left out of that comparison).
  • The rest of the suite: the whole core suite (8518 tests), the CLI unit suite (949), the seven command targets (1064), the nineteen Go and vendor targets (459), cli, spawn_env_hygiene, command_module_layering and help_text_hygiene (136), the script tests (411): all pass, and no test that is on main had to change. cargo fmt, both clippy runs, cargo check, actionlint and the tool-pin check pass.

Not covered

  • Other ecosystems. Only Go module copies get a line. Other ecosystems keep unpacked packages under .socket/vendor/<ecosystem> too, and a crate can ship a BUILD.bazel. Nothing was measured for them under Bazel, and for Cargo a wider ignore line is suspected to break the ruleset's own tooling, so nothing is written for them.
  • A Bazel root socket-patch does not look for: decision (e). Nested workspaces: decision (f).
  • A gazelle BUILD file that is already in a copy stays. In a project patched by an earlier release the build stays broken until that file is gone. Vendored mode: the next scan --mode vendored rebuilds the copy. Agent mode: delete the BUILD.bazel files under .socket/go-patches by hand.
  • Which command gives the line to a project patched by an earlier release. A scan or get that patches in the project's mode, repair, rollback, remove, vendor --revert, and apply or vendor where the project has a manifest. Not scan --mode agent in a vendored project, not a scan that writes nothing else, not list, vex or the --check audits.
  • Project paths .bazelignore cannot name. A project directory below the root whose path starts with # or white space, or holds *, ? or [, gets its line, and Bazel or gazelle does not honour it.
  • ignore_directories in REPO.bazel is not read; a workspace that has it gets the line too, which is harmless.
  • Not run: Linux and Windows (the real-Bazel and flow tests are Unix only, so on Windows only the unit tests cover this, and CI will be the first run of every test on Linux; the directory lock was tried there only as the bare flock call on a directory, in a container, on overlayfs and tmpfs); a lock on a network file system; a checkout of another user through the built binary (the unit test hands the owner in); commit 1 on any release at this revision; the mutations on 8.8.2 and 7.7.2; the other ecosystems' real-Bazel tests on 8.8.2 and 7.7.2; the nested-workspace measurement on 8.8.2 and 7.7.2; WORKSPACE go_repository workspaces; gazelle 2.0.0-beta.2; a project below the root on real Bazel; two patched modules; the merge with today's main beyond what the second paragraph says.

Caveats

  • The file is written at the end of a command, after its group commits, and is no part of a commit journal. A run that dies in between leaves a copy without its line, which the next command adds. A command that fails after it removed a copy can leave the pair behind, which ignores nothing, until the next command.
  • A pair that is only added is one write at the end of the file that is there, not the stage-and-rename the other writers use; a removal is a stage-and-rename, or a delete.
  • After the prune pass of a scan removed a copy, a warning the scan gave for that copy before is out of date, and a file the pass cannot write is reported by the next command, not by that scan.
  • The lock is flock on the workspace root directory, held for the edit. Another tool that takes the same kind of lock on that directory waits for that long; none is known to.
  • The tests that run gazelle fetch gazelle's own Go modules from proxy.golang.org, which is last in their GOPROXY list after the loopback stand-in. The hosted flow's list is unchanged.
  • The fixture's BUILD.bazel changed, so the repository-cache key of the Go legs changes and the first CI run downloads again.
  • The changes made after the third review have not been reviewed again.

Generated by Claude Code

`scan --mode vendored` and agent mode (`scan --mode agent`, `apply`)
patch a Go module by keeping a patched copy of it in the checkout, under
`.socket/vendor/golang` or `.socket/go-patches`, and pointing `go.mod`
at it with a directory `replace`. Only the hosted flow had a test on a
real Bazel. This adds the two other modes for the same gazelle `go_deps`
workspace, on Bazel 9.3.2, 8.8.2 and 7.7.2, and records what the code
does there today.

The next build is patched in both modes, and the undo is clean
(`vendor --revert`) or leaves only the emptied `.socket/manifest.json`
(`rollback`). The user's next step breaks the build:

- `bazel run //:gazelle` writes a BUILD.bazel into the copy. gazelle's
  `go_deps` copies the directory with that file, and every target that
  uses the module fails to load:
  `No repository visible as '@rules_go'`.
- When the module ships a BUILD file of its own, `bazel build //...`
  fails right after the command: the copy is a package of the checkout
  whose load of `@io_bazel_rules_go` nothing maps.

Each test asserts that break by its error, so that the commit that
fixes it has to change the expectation.

The fixture gains a `gazelle` target, and its BUILD file is now the one
gazelle writes, so a gazelle run on the unpatched checkout changes
nothing and what a later run writes comes from the patch. Agent mode
copies the module from the user's module cache; the test lays that
directory out itself, so the runner needs no Go toolchain beyond the one
rules_go downloads. gazelle is built from Go modules of its own: a flow
that runs it has proxy.golang.org last in its GOPROXY list, after the
stand-in that serves the module under test.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are confined to e2e tests, fixture BUILD files, and workflow documentation; no production command logic in this diff.

Overview
Extends the real-Bazel Go e2e suite beyond hosted mode by documenting and exercising vendored and agent flows where patched module copies live under .socket with a go.mod replace.

Adds a shared kept_copy_flow harness (stand-ins, module zips including optional shipped BUILD.bazel, scan/apply, bazel run //:gazelle, undo) plus three new #[ignore] tests that assert today's behavior: patching succeeds, then either bazel build //... or gazelle writing BUILD.bazel into the copy breaks the build with rules_go visibility errors, while revert/rollback restores the tree.

Updates the gazelle Bzlmod fixture with a gazelle target and a consumer go_binary/go_library layout so gazelle runs are part of the scenario. CI workflow comments now call out all three modes and that gazelle legs may fetch tooling modules from proxy.golang.org.

Reviewed by Cursor Bugbot for commit 6f85989. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: this PR holds only the tests-only commit 6f85989f7, and its CI is green (ci-ok and clippy pass). The description says the fix commit 876416d7d comes next, but that commit is not on GitHub (GET commits/876416d7d returns 422). As it stands, the PR would merge tests that assert today's broken behaviour, so I have not reviewed or labelled it for approval.

Question for the owner: please push 876416d7d to bazel-small/21-go-bazelignore. Or, if the plan changed, say so here. I added agent:needs-human so that agents leave this branch alone until then. Remove the label once the fix commit is pushed, and the shepherd will review it on its next run.


Generated by Claude Code

Vendored and agent mode patch a Go module by keeping a copy of it under
the project's `.socket` directory and pointing `go.mod` at it with a
directory `replace`. In a Bazel workspace that copy is a directory
Bazel and gazelle walk like any other. The next build is patched, and
then the user's ordinary steps break it, on Bazel 9.3.2, 8.8.2 and
7.7.2 with gazelle's `go_deps`:

- `bazel run //:gazelle` writes a BUILD.bazel into the copy, `go_deps`
  copies the directory with it, and every target that uses the module
  fails to load (`No repository visible as '@rules_go'`);
- when the module ships a BUILD file of its own, `bazel build //...`
  fails right after the command.

One line in the `.bazelignore` at the root of the workspace prevents
both, and socket-patch now writes it. This is the first Bazel file
socket-patch writes.

Where it applies

A project is in a Bazel workspace when its directory, or one above it
up to the root of its git checkout, holds a regular file `MODULE.bazel`,
`REPO.bazel`, `WORKSPACE` or `WORKSPACE.bazel`. Those are the names
Bazel's client looks for from the working directory up
(`WorkspaceLayout::InWorkspace`, src/main/cpp/workspace_layout.cc line
36 at the tags 7.7.2, 8.8.2 and 9.3.2). The nearest such directory is
the Bazel root; a directory of that name does not count. Without such a
file nothing is read or written, and the run writes the same files as
before, byte for byte.

The checkout is the one `repo_root::find_git_repo` finds, and it
counts only when its `.git` belongs to a trusted owner
(`GitRepo::trusted`: the user, root, or the user sudo ran for; a root
process trusts any), which is the rule the `socket.yml` lookup has for
the repository root. Without it, another user's `/tmp/.git` and
`/tmp/MODULE.bazel` on a shared machine would make a command in
`/tmp/job/proj` create `/tmp/.bazelignore`. A project in no such
checkout (a Docker build context, an unpacked source archive, a
checkout of another user) is looked for in its own directory only:
nothing above it is read, so a workspace file there gives no line and
no warning. Neither does a Bazel root above the root of the checkout (a
git submodule, several checkouts below one workspace): this repository
reads no project file outside the checkout, so a look there could see
the workspace file and not whether the line is in place, and its
warning would never end.

The nearest directory with a workspace file is the root. In a workspace
that is nested in a larger one the line is therefore in the inner file,
which a run from the outer workspace does not read. Measured on Bazel
9.3.2 with gazelle 0.54.0, outside the committed tests: from the outer
root `bazel build //...` loads the packages of the inner tree, a BUILD
file in the copy among them, and `bazel run //:gazelle` writes a
BUILD.bazel into the copy, after which the inner build fails as above;
with the line in the outer file, by its path from the outer root,
neither happens. The writes are not widened for that. The docs name
these limits and the line to add by hand.

What is written

- While `<project>/.socket/vendor/golang` or
  `<project>/.socket/go-patches` exists, the root's `.bazelignore`
  names it by its path from the root, on a line of its own under the
  comment line `# socket-patch: patched Go module copies, not Bazel
  packages (to opt out, comment out the next line)`. The pair is added
  at the end of the file, in the file's line breaks, and the file is
  created when missing. Never `.socket` alone: other things live there.
- Nothing is added when a line already covers the directory: the path
  or a directory above it, with or without a trailing `/` or a leading
  `./`. Bazel reads a line literally, so a line with a blank before or
  after it, a pattern or a string prefix covers nothing.
- Nothing is added either when the user declined the pair: the comment
  line with the path line commented out under it (`#`, blanks, the
  path, which may end in one `/` or start with `./` as in a line that
  covers). No command adds a line for that directory, warns about it or
  touches the two lines again, on removal either. Deleting the pair is
  no opt-out: the next command writes it back. Neither is commenting
  out both lines, or a path line changed in another way.
- socket-patch knows its pair by the start of the comment line,
  `# socket-patch:`, not by the whole sentence: a path line under a
  line that starts so is its own, and that path line commented out is
  the opt-out. A later release can reword the comment without leaving
  the pairs of this one behind.
- Only a directory that is not found is gone. One that cannot be probed
  (a directory above it that may not be searched, a transient error)
  keeps its pair, and gets none.
- Once the directory is gone the pair is removed, and the file is the
  one of before, byte for byte. A file whose last line has no line
  break gets a line break and the pair without a final one, and loses
  that line break again with the pair: exactly the one the comment line
  ends in, which is the one that was written before it, so that a last
  byte that is a carriage return on its own stays. Bazel and gazelle
  honour a last line without a line break: the agent test on real Bazel
  has one.
  A file left empty is deleted, an empty one the user had included:
  it cannot be told from one socket-patch created. Only a line under
  such a comment line is ever removed.
- The line follows the directory, not the `replace`:
  `rollback --preserve-state` drops the `replace` and keeps the
  vendored copy, whose BUILD files would be back in the workspace
  without the line.
- A read-only file is neither replaced nor deleted. The file is opened
  for appending before any change, a removal included, because a
  rename or an unlink succeeds in a writable directory.
- A file that is not updated is the new warning
  `bazelignore_not_updated`: it could not be read or written (a
  directory or a symbolic link in its place, a read-only directory, a
  read-only file). The detail names the line to add, or says that the
  lines of a copy that is gone can be deleted. The command succeeds as
  before.
- A human run reads `Updated <path>` or `Removed <path>` on stdout when
  the file changed, once for each file, in words that hold for lines
  that came and for lines that went, and the warning on stderr. Under
  `--json` the warning is a run-level `warnings[]` entry; there is no
  new field.

Which commands

`commands::sync_bazelignore` is the one place the CLI asks for the
file, behind one gate: not in a dry run and not in a `--global` run.
Each command that can write or remove a copy calls it once, after its
last step that can change a copy or take one back: `apply` (and with
it `scan --mode agent` and `get`), the vendored step of `scan` and
`get`, `vendor`, `vendor --revert`, `rollback`, `remove`, `repair`
(also when it failed), and a hosted scan that took the lock, since a
takeover removes the vendored copy. A hosted scan that took no lock may
write nothing (`patches.enabled: false` in `socket.yml`, a rollout cap,
no granted reference) and leaves this file alone too. The eject of a
hosted project calls it after its rollback, so that an eject which
vendored the Go module and was then rolled back leaves no pair for a
copy that is gone again. `apply` and `vendor` in a project with no
manifest end before that step.

A run whose `--ecosystems` leaves `golang` out adds no line. It still
takes away the pair of a copy that is gone, because `remove` of a Go
patch and `vendor --revert` remove the Go copy whatever that option
says.

With a `--manifest-path` into another project the agent copies are
those of `--cwd` and the vendored ones those of the manifest's project,
so the helper brings the file of each project's Bazel root in line.

`scan --prune` comes to the file a second time, after a pass that
reverted a vendored entry or swept an orphan directory, which can
remove the last copy after the scan's own step. That second call only
takes a pair away: it adds no line and gives no warning, so the run
warns once and no state is kept between the two. What it costs: a
warning the scan's own step gave for a copy the pass then removed is
out of date, and a file the pass cannot write is reported by the next
command. The scan's own call cannot be left to that pass instead: the
pass also follows the nested apply of `scan --mode agent --prune`, and
it does not run at all when it cannot take the apply lock.

The file is written after the commands' group commits, outside them:
it is no part of a commit journal. A run that dies in between leaves a
copy without its line, which the next command adds; a command that
fails after it removed a copy can leave the pair behind, which ignores
nothing, until the next command.

A file several projects share

Two projects under one Bazel root share the file, and each command
holds only its own project's apply lock. So the edit, from its read to
its write, is made under an advisory lock on the root directory itself:
`fs2` on a handle to the directory, with contention told as the apply
lock tells it (`apply_lock::is_lock_contended`). No lock file is made,
in the workspace or anywhere else. A run that finds nothing to change
takes no lock, also when the file cannot be read and the project has no
copy to name in it. Where the directory cannot be locked (Windows, where
it cannot be opened this way; a file system without such locks), or the
lock stays held for a second, the edit is made without it: an add is
still one appending write, which keeps what another project adds at that
moment, and a removal replaces the file and can then lose such a pair
until the next command in that project. With the lock, a removal in one
project against an add in its sibling keeps the added pair in every one
of 25 rounds of a unit test, which asserts that half on Unix only;
without the lock that test fails.

What a run costs

The Bazel root and the bound of the checkout come from one walk up
(`search_dirs`), which runs with the rest of the edit on the blocking
pool. For a project outside a Bazel workspace, `d` directories below
the root of its checkout, that is 2 path canonicalisations, `d + 2`
stats of `.git` and `4 (d + 1)` stats of the four names, and nothing
else. In a Bazel workspace it adds two stats of the copy directories
and one read of `.bazelignore`; the lock and the write come only when
something changes. (Counted from the code, not traced.)

A side effect

The line does more than keep gazelle out. Bazel leaves an ignored
directory out of every `glob()` of a package above it (9.3.2, 8.8.2 and
7.7.2), so a target that hands the project's Go files to a `go` command
of its own through such a glob no longer receives the copies. Built on
Bazel 9.3.2 with a genrule that runs the Go SDK's `go build` over
`glob(["**/*.go", "**/go.mod", "**/go.sum"])`, outside the committed
tests: without the line it builds patched until the first
`bazel run //:gazelle`, whose BUILD file in the copy takes the copy out
of the glob in the same way, and then fails (`replacement directory
./.socket/vendor/golang/... does not exist`) together with every
rules_go target that uses the module. With the line the rules_go
targets build, and that genrule fails with the same error from the
start; it builds when it names the copy's files as labels beside the
glob, or runs outside the sandbox. A workspace that has such a target
and never runs gazelle can decline the line as above.

Not covered

Only Go module copies get a line. Other ecosystems keep unpacked
packages under `.socket/vendor/<ecosystem>` too (a crate can ship a
BUILD.bazel); nothing was measured for them under Bazel, so nothing is
written for them. A BUILD file that gazelle wrote into a copy before
the line existed stays there: the next `scan --mode vendored` rebuilds
the copy, and in agent mode the BUILD files under `.socket/go-patches`
have to be deleted by hand. A project directory whose path from the
root starts with `#` or white space, or holds `*`, `?` or `[`, cannot
be named in `.bazelignore`: its line is written, and Bazel or gazelle
does not honour it. `ignore_directories` in REPO.bazel is not read; a
workspace that has it gets the line too.

Tests

The real-Bazel tests now assert the fixed outcome: the build prints
the patched marker after the command and `bazel build //...` passes,
the two tests of a module without a BUILD file build it again after
`bazel run //:gazelle`, which writes nothing, and the undo takes the
line away. The agent test starts from a `.bazelignore` of the user's
whose last line has no line break, and ends with that file as it was.

Tests without Bazel hold: the root finder (each of the four names, a
directory of such a name, a link, a file above the checkout, a checkout
of an owner that is not trusted and a project in no checkout, which
both reach no further than the project); the line rules (what covers,
the opt-out and what is none, a pair under another wording of the
comment, the pair in LF and CRLF files and after a last line without a
line break, the removal that leaves the file of before, a last
carriage return included, a run that leaves Go out); the write
(appended to the file that is there, a read-only file and a link left
alone on an add and on a removal, a copy directory that cannot be
probed, the lock on the root directory and none where nothing can
change); two edits at once; and through the built binary each
command's call: that a dry run of ten commands writes nothing in either
mode, that a declined pair stays declined over scan, repair, apply,
rollback and revert, that a rolled-back eject leaves the project as it
was, that a prune pass and a hosted takeover take the line away with
the last copy, that `--ecosystems npm` adds no line and takes the pair
of a copy it removed away, that a scan which `socket.yml` lets write
nothing leaves the file alone in every mode, that a manifest in another
project brings both files in line, that a file which cannot be written
is one warning for each command (in the envelope, and on stderr for a
scan whose prune pass comes to the file a second time), what a human
run prints for lines that came, lines that went and a file that went,
that a project in no git checkout below a Bazel root gets no line and
no warning, and that the same run in a project with no Bazel workspace
file writes the same files, byte for byte but for the time in a
vendored copy's marker, and no `.bazelignore`. No test depends on what
lies above the temp directory: they pass with it inside a git checkout
and outside one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. In a Bazel workspace, the Go modes that keep patched module copies under .socket/vendor/golang and .socket/go-patches now add a commented # socket-patch: line and the directory's path to the workspace's .bazelignore, so Bazel and gazelle stop treating the copies as packages (before this, bazel run //:gazelle broke the build). Every command that removes a copy removes the pair again, giving back the original bytes. An unwritable file is a non-fatal bazelignore_not_updated warning, and commenting out the path line opts out.

Risk: medium. The code is careful, but this is the first time socket-patch writes a Bazel file in a user's repo. The first agent-mode apply after an upgrade, including one in CI, changes .bazelignore and will trip clean-tree checks. The ignore also takes the copies out of every glob() above them. The PR describes both as accepted trade-offs.

Look here

  • bazel/ignore.rs:57-131: sync_blocking, covering the lock-free fast path, the re-read under the lock and the warning
  • bazel/ignore.rs:227-263: edited, which adds and removes pairs byte for byte (CRLF, lone CR, no final newline)
  • bazel/ignore.rs:140-176: the lock, and the write: append vs. atomic replace vs. delete, with O_NOFOLLOW
  • bazel/mod.rs:34-50: how the Bazel root is found, which never goes above the project unless the project is in a trusted git checkout
  • commands/mod.rs:67-110: the sync_bazelignore gate for dry run, --global, --ecosystems and --manifest-path, and its call sites

Verified. The fix commit cdf3a5f is now on the branch, and the three real-Bazel gazelle tests no longer assert the old No repository visible as '@rules_go' failure: they assert that .bazelignore is written and the build passes, so they would fail without the fix. I traced CRLF, no final newline, an empty file, re-runs, declined or reworded comment lines and the removal order. A symlinked .bazelignore is refused, and a read-only one leaves the file unchanged and warns. cargo test -p socket-patch-core --lib bazel passed (22), as did --lib go_mod_edit (56). CHANGELOG.md is untouched. CI on 4dfe93d: ci-ok and clippy are green.

Changes I made: none.

Open questions: none that block a merge. Minor points:

  • Human (non---json) output gains an Updated/Removed <path> line.
  • A .bazelignore write failure in the scan --prune pass (vendor.rs:5332) is reported only on the next command, as the Caveats say.

This PR is the base of the stack #2139 → #2148 → #2157, which is already approved.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 19, 2026
Merged via the queue into main with commit 19f96ed Nov 19, 2026
66 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the bazel-small/21-go-bazelignore branch November 19, 2026 20:40
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 19, 2026
The parent branches landed on main as squash commits (#2123, #2139,
#2148), so this re-applies only this PR's own changes on top of main.

main (#2155) turned build_document_with into the one builder, taking an
Attested struct and adding code_not_present; this PR had added a
separate build_document_with_affected. The affected PURLs are now an
`affected` field of Attested, handled by build_document_with:

- an affected PURL still gets a statement of its own with status
  `affected` and its action statement;
- a not_affected statement keeps main's justification rule
  (vulnerable_code_not_present only when no subcomponent is patched);
- `vex` passes both code_not_present and affected.

Tests of the affected statements now go through build_document_with.
CLI_CONTRACT.md keeps main's folded warning text and adds this PR's
partialFailure-on-affected sentence. The bazel LOC budget is main's
plus this PR's growth.

Assisted-by: Claude Code:claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants