Repository navigation
Commit 19f96ed
Keep Go module copies under .socket out of Bazel packages (#2123)
* Test the vendored and agent Go flows through a gazelle run on real Bazel
`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>
* Keep Go module copies under .socket out of Bazel packages
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>
---------
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>1 parent 1f56209 commit 19f96ed
21 files changed
Lines changed: 2487 additions & 47 deletions
File tree
- .github/workflows
- crates
- socket-patch-cli
- src/commands
- scan
- tests
- e2e_bazel_build
- fixtures/bazel/gazelle/bzlmod/common
- socket-patch-core/src
- bazel
- utils
- vendor
- docs
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | | - | |
24 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| |||
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
| 47 | + | |
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
| |||
60 | 64 | | |
61 | 65 | | |
62 | 66 | | |
| 67 | + | |
63 | 68 | | |
64 | 69 | | |
65 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2532 | 2532 | | |
2533 | 2533 | | |
2534 | 2534 | | |
| 2535 | + | |
2535 | 2536 | | |
2536 | 2537 | | |
2537 | 2538 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1900 | 1900 | | |
1901 | 1901 | | |
1902 | 1902 | | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
1903 | 1906 | | |
1904 | 1907 | | |
1905 | 1908 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
54 | 112 | | |
55 | 113 | | |
56 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1116 | 1116 | | |
1117 | 1117 | | |
1118 | 1118 | | |
1119 | | - | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
1120 | 1124 | | |
1121 | 1125 | | |
1122 | 1126 | | |
| |||
1860 | 1864 | | |
1861 | 1865 | | |
1862 | 1866 | | |
| 1867 | + | |
| 1868 | + | |
1863 | 1869 | | |
1864 | 1870 | | |
1865 | 1871 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
| 212 | + | |
211 | 213 | | |
212 | 214 | | |
213 | 215 | | |
| |||
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
| 229 | + | |
227 | 230 | | |
228 | 231 | | |
229 | 232 | | |
| |||
248 | 251 | | |
249 | 252 | | |
250 | 253 | | |
| 254 | + | |
251 | 255 | | |
252 | 256 | | |
253 | 257 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2694 | 2694 | | |
2695 | 2695 | | |
2696 | 2696 | | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
2697 | 2700 | | |
2698 | 2701 | | |
2699 | 2702 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2175 | 2175 | | |
2176 | 2176 | | |
2177 | 2177 | | |
| 2178 | + | |
| 2179 | + | |
| 2180 | + | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
2178 | 2184 | | |
2179 | 2185 | | |
2180 | 2186 | | |
| |||
2219 | 2225 | | |
2220 | 2226 | | |
2221 | 2227 | | |
| 2228 | + | |
2222 | 2229 | | |
2223 | 2230 | | |
2224 | 2231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
620 | 620 | | |
621 | 621 | | |
622 | 622 | | |
| 623 | + | |
| 624 | + | |
623 | 625 | | |
624 | 626 | | |
625 | 627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1299 | 1299 | | |
1300 | 1300 | | |
1301 | 1301 | | |
| 1302 | + | |
| 1303 | + | |
1302 | 1304 | | |
1303 | 1305 | | |
1304 | 1306 | | |
| |||
2470 | 2472 | | |
2471 | 2473 | | |
2472 | 2474 | | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
2473 | 2479 | | |
2474 | 2480 | | |
2475 | 2481 | | |
| |||
5393 | 5399 | | |
5394 | 5400 | | |
5395 | 5401 | | |
| 5402 | + | |
| 5403 | + | |
| 5404 | + | |
| 5405 | + | |
| 5406 | + | |
5396 | 5407 | | |
5397 | 5408 | | |
5398 | 5409 | | |
| |||
0 commit comments