diff --git a/.github/workflows/bazel-maven.yml b/.github/workflows/bazel-maven.yml index c28b174c8..6d85d6a7f 100644 --- a/.github/workflows/bazel-maven.yml +++ b/.github/workflows/bazel-maven.yml @@ -12,7 +12,11 @@ name: Bazel patch compatibility (Maven) # mode that repository is a directory of the checkout with the committed # jar: the flows hold the build from the checkout alone with no host to # ask, the audit of the committed jar, the undo, and the way from hosted -# to vendored and back. bazel-compatibility.yml holds the legs and what +# to vendored and back. Agent mode patches the jar in the Maven local +# repository and no hub: its flow holds the build to the upstream jar, +# every VEX document to `affected` with the command that patches the hub, +# and after that command the build and the document to the patched jar. +# bazel-compatibility.yml holds the legs and what # every ecosystem # shares; this file says when the Maven legs run and on which releases. # @@ -62,7 +66,7 @@ on: # rewriter's index of a pom, the model of what Maven selects with the # pom walk under it (the hosted lane pins only where Maven selects the # patch's version), the crawler that says where the Maven local - # repository is, lock discovery and the upstream restore of `rollback` + # repository is, the upstream restore of `rollback` # (for a pom and for the block of MODULE.bazel), the vendored Maven # backend, which writes the hub's jar and its entry, the dispatch that # hands a vendored command the hub's declines (vendor/refusals.rs), and what @@ -70,7 +74,12 @@ on: # hubs of MODULE.bazel, their locks and the block). Nothing another # ecosystem's flow owns is here but that dispatch, which the Composer # legs watch too, so a change to one ecosystem starts that ecosystem's - # legs only. + # legs only. Last, what the agent flow runs through: the three files + # with which agent mode patches a jar, and the VEX code (`vex/` of the + # core, where lock discovery is too, and the files of the `vex` + # command). That flow holds the exact + # document socket-patch writes about its build, so a change to how a + # document is built or worded starts the legs of every ecosystem. # # What it does not cover: shared engine code (`rewrite_maven_pom` is in # patch/redirect/mod.rs, the file of every ecosystem's rewriter; hosted/, @@ -92,13 +101,16 @@ on: - 'crates/socket-patch-core/src/crawlers/maven_selection.rs' - 'crates/socket-patch-core/src/crawlers/maven_scope.rs' - 'crates/socket-patch-core/src/crawlers/maven_crawler.rs' - - 'crates/socket-patch-core/src/vex/discover/maven.rs' - - 'crates/socket-patch-core/src/vex/discover/bazel_maven.rs' - 'crates/socket-patch-core/src/patch/redirect/upstream/maven.rs' - 'crates/socket-patch-core/src/patch/redirect/upstream/bazel_maven.rs' - 'crates/socket-patch-core/src/vendor/maven_repo.rs' - 'crates/socket-patch-core/src/vendor/refusals.rs' - 'crates/socket-patch-core/src/bazel/**' + - 'crates/socket-patch-core/src/patch/jvm_jar.rs' + - 'crates/socket-patch-core/src/patch/jar_members.rs' + - 'crates/socket-patch-core/src/patch/jar_manifest.rs' + - 'crates/socket-patch-core/src/vex/**' + - 'crates/socket-patch-cli/src/commands/vex*.rs' push: branches: [main] # The pull request paths, and what only a push to main covers: the @@ -120,13 +132,15 @@ on: - 'crates/socket-patch-core/src/crawlers/maven_selection.rs' - 'crates/socket-patch-core/src/crawlers/maven_scope.rs' - 'crates/socket-patch-core/src/crawlers/maven_crawler.rs' - - 'crates/socket-patch-core/src/vex/discover/maven.rs' - - 'crates/socket-patch-core/src/vex/discover/bazel_maven.rs' - 'crates/socket-patch-core/src/patch/redirect/upstream/maven.rs' - 'crates/socket-patch-core/src/patch/redirect/upstream/bazel_maven.rs' - 'crates/socket-patch-core/src/vendor/maven_repo.rs' - 'crates/socket-patch-core/src/vendor/refusals.rs' - 'crates/socket-patch-core/src/bazel/**' + - 'crates/socket-patch-core/src/patch/jvm_jar.rs' + - 'crates/socket-patch-core/src/patch/jar_members.rs' + - 'crates/socket-patch-core/src/patch/jar_manifest.rs' + - 'crates/socket-patch-core/src/vex/**' - 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs' - 'crates/socket-patch-core/src/patch/redirect/**' - 'crates/socket-patch-core/src/hosted/**' diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 346a92ba3..49c7f70b0 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -746,7 +746,7 @@ Every VEX statement's impact string records which patch-application mode persist **Unverified statements (`--no-verify`).** `--no-verify` / `--vex-no-verify` (and an ambient `SOCKET_VEX_NO_VERIFY`) skip every on-disk hash check, so a statement emitted under it rests on the patch records and the wiring gates alone — for an agent-mode patch on nothing but the manifest, even when the installed files are unpatched or absent. Such a run is never silent. Each purl it attests ends its impact-statement part with `; not verified on disk`, appended AFTER the marker, so the three marker strings above stay intact as prefixes (`Patched via Socket patch (vendored); not verified on disk`), and the run raises the `vex_not_verified` warning (stderr in human mode, `warnings[]` / `vex.warnings` under `--json`) naming how many statements carry the suffix. The purls an in-run `scan --mode hosted --vex` confirmed are attested from that run's own evidence in either mode and never carry it. A default (verifying) run's document is unchanged: it never contains the suffix. -**The installed tree (`--no-bazel`).** `--no-bazel` / `--vex-no-bazel` (and an ambient `SOCKET_VEX_NO_BAZEL`, read like `SOCKET_VEX_NO_VERIFY`: `1` or `true` sets it, an empty value does not) say that the document describes what the package manager installed, for a repository that ships that tree while a Bazel hub reads the same lock for something else, such as its tests. The Bazel workspace is then not read: no package is attested as `affected` for `vex_bazel_not_in_build`, and the document, the events, the warnings, the exit code and every printed line are those of the same checkout without its Bazel files. Without the option nothing changes, and outside a Bazel workspace the option changes nothing and is accepted silently. `--vex-no-bazel` is inert unless `--vex` is set. +**The installed tree (`--no-bazel`).** `--no-bazel` / `--vex-no-bazel` (and an ambient `SOCKET_VEX_NO_BAZEL`, read like `SOCKET_VEX_NO_VERIFY`: `1` or `true` sets it, an empty value does not) say that the document describes what the package manager installed, for a repository that ships that tree while a Bazel hub reads the same lock for something else, such as its tests. The Bazel workspace is then not read for the document: no package is attested as `affected` for `vex_bazel_not_in_build`, and the document, the events and warnings about it, the exit code and every line `vex` prints are those of the same checkout without its Bazel files. The option is about the document alone: what the host command itself does and says in a Bazel workspace stays (`apply` and an agent `scan` beside a pinned rules_jvm_external hub still warn `maven_bazel_hub_unpatched`). Without the option nothing changes, and outside a Bazel workspace the option changes nothing and is accepted silently. `--vex-no-bazel` is inert unless `--vex` is set. **Identifier overrides.** `--product` / `--vex-product` and `--doc-id` / `--vex-doc-id` are emitted with surrounding whitespace trimmed (a blank value means unset). @@ -2712,7 +2712,7 @@ command's integration tests check its output. | `vendor_prebuilt_stub_invalid` | `failed` | RubyGems: the server stub lacks required attributes or is otherwise invalid; no local stub fallback is permitted. | | `bazelignore_not_updated` | run-level `warnings[]` | Go vendored and agent mode, Cargo vendored mode under a rules_rust crate hub, and Maven vendored mode, in a Bazel workspace (additive): the `.bazelignore` at the Bazel root, which keeps Bazel and gazelle out of the module copies under `.socket/vendor/golang` and `.socket/go-patches` out of the crates under `.socket/vendor/cargo`, and out of the jars vendored for a rules_jvm_external hub under `.socket/vendor/maven`, was not updated. It could not be read or written (a symbolic link or a directory 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's patch or undo stands and its exit code is unchanged. See docs/ecosystems.md "Bazel workspaces: `.bazelignore`". | | `redirect_cargo_bazel_config_unread` | `warnings[]` (warning; human: `Warning: …`) | scan/get `--mode hosted` in a Bazel workspace (additive): a rules_rust crate_universe hub (`crate.from_cargo` in `MODULE.bazel` or a file it includes, or `crates_repository` in the `WORKSPACE` file) reads the project's `Cargo.lock` or one of its `Cargo.toml` files, or may read them (its `cargo_lockfile` or `manifests` is not written out as strings, or a use of the extension may hide a call), and its `cargo_config` is not one string that is the label of the cargo config hosted mode writes (`.cargo/config.toml`, or the project's `.cargo/config`). cargo-bazel runs cargo with the config its hub names and no other, so the registry of a pin would be unknown to it and `bazel build` would fail (`registry index was not found in any configuration`). The crate is not pinned: nothing is written for it, its event is `skipped` / `redirect_unconfirmed` (a vendored crate that the scan would take over stays vendored: `skipped` with this code, and `redirect_takeover_kept_vendored`), and status and exit code are unchanged. One warning per crate; the detail names the hub's file and line, the config to commit (created with a comment line in it where the project has none), the attribute to give the hub (`cargo_config = "//:.cargo/config.toml"`; the label is written in the package of the hub's own labels), after which the same command pins the crate, and `--mode vendored`. A hub that names that config is still in the way while the file is there and holds nothing: `rollback` deletes a cargo config it leaves empty, and the hub fails on the missing file; the detail asks for a comment line in it. For a use of the extension that may hide a call the detail names that line to rewrite and no attribute: the next run pins the crate or names the hub that is in the way. A `WORKSPACE` call (`crates_repository`, also under the name a `load` of that file gives it) counts only when no hub of the module files reads the project's Cargo files and the checkout names no Bazel 9 or later. The rewriter's own refusals answer first and keep their codes. A pin that is in place already stays `applied`, and no warning is given for it. Nothing is read or changed in no Bazel workspace, or where no such hub is seen to read the project's Cargo files. See docs/ecosystems.md "Bazel workspaces: Cargo". | -| `maven_bazel_hub_unpatched` | run-level `warnings[]` (human: `Warning: …`) | Maven in a Bazel workspace, agent and vendored mode (additive): a pinned rules_jvm_external hub of the root `MODULE.bazel` fetches the jar of a Maven patch of this run, and the block that hosted and vendored mode keep in `MODULE.bazel` holds no entry for that jar after it. The hub builds the jar its own lock (`maven_install.json`) names, so the patch does not reach the Bazel build. Agent mode (`apply`, and `get` / `scan` in that mode; only for a package it found installed; never under `--global`) patches no hub: one warning per purl; the detail names the lock, the hub, the file and line of its `maven.install`, and the hosted command that patches a pinned hub; the events, the exit code and every written file are those of the same run without the workspace. Vendored mode (`vendor`, and `get` / `scan` in that mode) patches a pinned hub (below), and gives this code only for a jar it could have written an entry for and vendored nothing for, because the package failed or was skipped for another reason (never in a dry run); a jar it cannot write an entry for gets the `maven_bazel_*` code of the reason instead. A `vendor` whose `--ecosystems` leaves Maven out gives none. A hosted run never gives it: see the `maven_bazel_*` rows below and docs/ecosystems.md "Bazel workspaces: Maven". | +| `maven_bazel_hub_unpatched` | run-level `warnings[]` (human: `Warning: …`) | Maven in a Bazel workspace, agent and vendored mode (additive): a pinned rules_jvm_external hub of the root `MODULE.bazel` fetches the jar of a Maven patch of this run, and the block that hosted and vendored mode keep in `MODULE.bazel` holds no entry for that jar after it. The hub builds the jar its own lock (`maven_install.json`) names, so the patch does not reach the Bazel build. Agent mode (`apply`, and `get` / `scan` in that mode; only for a package it found installed; never under `--global`) patches no hub: one warning per purl; the detail names the lock, the hub, the file and line of its `maven.install`, and the hosted command that patches a pinned hub; the events, the exit code and every written file are those of the same run without the workspace. A VEX document says `affected` of such a jar (`vex_bazel_not_in_build`), but where the checkout does not say that Bazel reads `MODULE.bazel` as it stands. Vendored mode (`vendor`, and `get` / `scan` in that mode) patches a pinned hub (below), and gives this code only for a jar it could have written an entry for and vendored nothing for, because the package failed or was skipped for another reason (never in a dry run); a jar it cannot write an entry for gets the `maven_bazel_*` code of the reason instead. A `vendor` whose `--ecosystems` leaves Maven out gives none. A hosted run never gives it: see the `maven_bazel_*` rows below and docs/ecosystems.md "Bazel workspaces: Maven". | | `maven_bazel_unreadable` | agent: run-level `warnings[]` (human: `Warning: …`); scan/get `--mode hosted`: `warnings[]` (warning); vendored: as `maven_bazel_not_at_root` | Maven in a Bazel workspace (additive): whether a rules_jvm_external hub builds the jar of a Maven patch could not be told, because something could not be read with certainty: a hub without `lock_file` in agent mode (hosted and vendored mode give `maven_bazel_not_pinned`), a `maven.artifact` or `maven.from_toml` tag that cannot be read, a lock that is missing, empty, not format 2 or 3, larger than 16 MiB (or past the 64 MiB of locks that are read for one workspace) or no JSON, a `WORKSPACE` file that calls `maven_install` or cannot be read while `.bazeliskrc` or `.bazelversion` at the Bazel root names no release of Bazel 9 or later, a `maven.install` whose `name` or `lock_file` is not a plain string, a construct of `MODULE.bazel` the reader does not follow, and, in agent mode, a `maven.override` of the artifact (hosted and vendored mode give `maven_bazel_no_effect`). Hosted and vendored mode also give it for a hub on an isolated `use_extension`, an rc file that cannot be read (also one that an import takes from outside the workspace by a relative path, and one with a line whose reading Bazel's own source calls an oversight: a quote that is never closed, a `\` with nothing after it), a `MODULE.bazel` that is a symbolic link, and a block of `MODULE.bazel` that was changed by hand (also in its first line alone), and then writes no entry for the hub. One warning per purl; the detail names the file, the line and the reason. | | `maven_bazel_hub_not_read` | scan run-level `warnings[]` (human: `Warning: …`); `get --mode hosted`: `warnings[]` | scan `--mode hosted` at the root of a Bazel workspace, with Maven in scope (additive): the scan takes the jars that the pinned rules_jvm_external hubs fetch for packages of the project (the lockfile supplement above), and something of the hubs could not be read with certainty, so it does not know every Maven artifact that Bazel builds through a hub. Given once per run, for the first thing that was not read, whatever the packages: a hub without `lock_file` (the detail then ends with how to pin it), or one of the reasons of `maven_bazel_unreadable` that concern reading the hubs (a tag or a lock that cannot be read, a `maven_install` of a `WORKSPACE` file that Bazel may read, a construct of `MODULE.bazel` the reader does not follow, a block that was changed by hand). `[:]: , so socket-patch does not know every Maven artifact that Bazel builds here through a rules_jvm_external hub: it looks for patches of the ones in the hub locks it read, and of no other`. No jar is guessed for what was not read; the jars of the hubs that were read are scanned, and a patch found for one of them gets no entry while the doubt stands (`maven_bazel_not_pinned` / `maven_bazel_unreadable` below). With no package at all a person reads `No packages found (see the warning).`. Exit code and `status` unchanged. A hosted `get` by advisory, which keeps the releases the project has by the same supplement, gives it too when the advisory has a patch of a Maven artifact. Only in a workspace that has to do with the ruleset: a call in `MODULE.bazel` or a file it includes names `rules_jvm_external`, the `WORKSPACE` file calls `maven_install`, or a `maven_install.json` lies at the Bazel root. In another Bazel workspace a file that cannot be read gives no such warning, and the run is the one without the workspace. | | `maven_bazel_not_at_root` | `warnings[]` (warning; human: `Warning: …`) | scan/get `--mode hosted` in a Bazel workspace (additive): a rules_jvm_external hub builds the jar of a Maven patch, and the project is below the root of its Bazel workspace, in whose `MODULE.bazel` the patch of a hub is written. Like the five codes below and `maven_bazel_unreadable` in hosted mode, it means that the run wrote no entry for the jar in the block of the root `MODULE.bazel`, because it could not show from the checkout that the entry works. One warning per purl, in the place of `redirect_maven_no_pom` for that purl; the detail says why and ends with what the block of the root holds for the jar, which is read for a project below the root too (the detail then names the file as "the MODULE.bazel at the root"). Nothing: "socket-patch wrote nothing to MODULE.bazel for it, so the Bazel build keeps the upstream jar". The event is the one of the same run without the hub: `skipped` / `redirect_unconfirmed` in a tree only Bazel builds, `applied` beside a `pom.xml` or Gradle pin that landed. The entry of the patch, from an earlier run: that entry stays, the event is `applied`, and the detail ends "socket-patch left the entry that MODULE.bazel holds for it as it is". The entry of another patch of the same jar: it stays too, the event is the one without the hub, and the detail ends "socket-patch wrote nothing to MODULE.bazel for it: the entry of patch remains there, and where the block is in effect the Bazel build takes that patch's jar". Exit code unchanged. With none of these codes the run writes the block: `redirect.rewrittenFiles` holds `MODULE.bazel`, the event is `applied`, and `rollback` / `remove` take the entry out. **Vendored mode** (`vendor`, and `scan` / `get --mode vendored`; additive) gives the same codes, but for `maven_bazel_download`, with the same detail, for a jar whose entry it does not write. In a project with no Maven, Gradle, sbt or scala-cli build the hub is the patch's only target: the event is `failed` with the code as its `errorCode` and the detail, without the purl in front, as its `error`, nothing is written and the run exits 1, as for any vendored refusal (before, such a project failed with `vendor_jvm_shape_unsupported`, which a jar that no hub builds still does). Beside such a build that was vendored, the code is a run-level `warnings[]` entry (human: `Warning: …`), the event is `applied` and the exit code is unchanged. With none of these codes vendored mode commits the patched jar under `.socket/vendor/maven//` (`REPO.bazel`, `file/BUILD.bazel`, `file/-.jar`, `.gitignore`), writes the entry that names that directory into the block, in the place of a hosted entry for the same jar, adds the directory's line to `.bazelignore`, and records the patch in the vendor ledger (in a project with no other JVM build: `ecosystem` `maven`, `flavor` `bazel`, the committed jar as `artifact`, no `wiring`); the event is `applied`. `vendor --check` then also fails (`vendor_check_failed`) when that jar is not, byte for byte, the vendored jar the ledger records (beside a Maven or Gradle build the hub's jar is a copy of it), when `REPO.bazel` / `file/BUILD.bazel` is not the file that was written, when the block no longer names the directory or was changed by hand, and when no pinned hub takes the jar at that version any more (the reason names `socket-patch rollback `) or an rc file switches the entry off. `vendor --revert` / `rollback` / `remove` take the entry, the directory and the line out, and a hosted run that puts its entry in the place of the vendored one removes the directory and the line. See docs/ecosystems.md "Bazel workspaces: Maven". | @@ -4178,7 +4178,7 @@ match it, withholds the statement: | `gradle_unpatched_copy` | `failed[].reason` | The purl the warning above (or `vex_gradle_derived_cache_check_failed`) withheld. | | `vex_gradle_derived_cache_check_failed` | run warning | The derived-cache check of an attested Gradle copy did not finish (it crashed), so a copy derived from the unpatched jar cannot be ruled out; no statement for that purl. | | `vex_gradle_lock_above_base` | run warning and `failed[].reason` | A hosted pin is wired, but a lock file records a release above its base, which that build resolves instead of the patch; no statement until it is re-locked or the patch is rolled back. | -| `vex_bazel_not_in_build` | run warning and `skipped` event of `vex` | In a Bazel workspace, `MODULE.bazel` or a file it includes hands the package to a hub that builds it from a lock (aspect_rules_js `npm.npm_translate_lock`, rules_python `pip.parse`, rules_ruby `ruby.bundle_fetch`, rules_rust `crate.from_cargo`, rules_csharp `nuget.from_lock`), and the patch is not in what the hub builds: it is in the installed copy while the project's own lock, which the call names, lists the package at that version without naming the patch (the lock in the project's directory, the lock of a pnpm workspace above it that names that directory among its importers, or a `packages.lock.json` below it), or it is a PyPI copy under `.socket/vendor` and the call names the file that points at it. The package is not omitted: each advisory of its patch gets a statement of the package's own with `status: "affected"`, the package as the one subcomponent of the product, and an `action_statement` that says what Bazel builds the package from and what to run (`scan --mode hosted` in the directory of that lock, with the attribute the call has to carry for a crate, `cargo_config`, and for a NuGet package, `nuget_config`, and for a crate `scan --mode vendored` as well; for a vendored copy `vendor --revert` first), and for an installed copy ends with the way out of a repository that ships the installed tree (``If what you ship is the installed tree and not what Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor).``), with no `justification` and no `impact_statement`; they come after the `not_affected` statement of an advisory that a patched package shares. The warning, one per package, starts `: the VEX document says affected.` and goes on with the action statement; no `vendored_tree_out_of_sync` warning is given for such a package. The document is always written for it, and the exit code is the one of a run without it: standalone `vex` exits 0 with the document, also when every statement is `affected`. It does not report the package as verified: each such statement is a `skipped` event with this `errorCode` and `details.status: "affected"`, counted in `summary.skipped` and not in `summary.verified`, and the envelope's `status` is `partialFailure`. A command run with `--vex` writes the document, keeps its exit code and status, and carries the warning in `vex.warnings` and the number of such statements in `vex.affected`. The one-line summary counts them: `Wrote OpenVEX document with 2 statements (1 affected) to `. `--no-verify` changes none of it. Not raised in a run with `--no-bazel` / `--vex-no-bazel` / `SOCKET_VEX_NO_BAZEL`, which reads no Bazel file (see "The installed tree" under "VEX provenance markers"), for a hosted pin, for a vendored gem or NuGet package (no test builds one under its hub), for a vendored crate (rules_rust builds it), in a `--global` run, for a patch whose record names no advisory (no run attests it), for the lock of another project (beside the project, below it unless it is a `packages.lock.json`, or above it without naming it), for a package that only the env document of a pnpm 11 lock lists, for a call that is not a statement of its own (in a comprehension, behind `and`, in a conditional, in the arguments of another call: it may never run), for a call that passes `*` or `**`, a `pip.parse` with `uv_lock` or a `crate.from_cargo` with a cargo-bazel lock, `lockfile` (it may take its packages from a file that is not read), for a crate the lock does not take from a registry, for a NuGet package when the `nuget_config` of any `nuget.from_lock` call names the patch, is not a plain string label of the main repository or cannot be read, or when such a call passes `*` or `**` (rules_csharp makes one hub of all the calls, so the pin of another project may be what it builds), or where the files are not read with certainty (a doubt of the reader, a `WORKSPACE` file that defines repositories, a label that is not a plain string, a lock that cannot be read, is over 64 MiB or is a link out of the Bazel root): the statement is then the `not_affected` one of a project without Bazel. See "Bazel workspaces: VEX" in `docs/ecosystems.md`. | +| `vex_bazel_not_in_build` | run warning and `skipped` event of `vex` | In a Bazel workspace, `MODULE.bazel` or a file it includes hands the package to a hub that builds it from a lock (aspect_rules_js `npm.npm_translate_lock`, rules_python `pip.parse`, rules_ruby `ruby.bundle_fetch`, rules_rust `crate.from_cargo`, rules_csharp `nuget.from_lock`, rules_jvm_external `maven.install`), and the patch is not in what the hub builds: it is in the installed copy while the project's own lock, which the call names, lists the package at that version without naming the patch (the lock in the project's directory, the lock of a pnpm workspace above it that names that directory among its importers, or a `packages.lock.json` below it), or it is a patch of a Maven artifact in any mode (in an installed jar, in a pin of a `pom.xml` or a Gradle file, in a copy vendored for such a build) while a pinned hub fetches the plain jar of that artifact at that version and the block that hosted and vendored mode keep in the root `MODULE.bazel` holds no entry of that patch for it (no file of a project decides what a hub fetches: the same from every project of the workspace, and the same after a hosted or vendored command that wrote no entry for the hub; a jar that the hub takes from the Maven local repository under `m2local` counts as fetched, and the lock's sha256 is not compared with the patch), or it is a PyPI copy under `.socket/vendor` and the call names the file that points at it. The package is not omitted: each advisory of its patch gets a statement of the package's own with `status: "affected"`, the package as the one subcomponent of the product, and an `action_statement` that says what Bazel builds the package from and what to run (`scan --mode hosted` in the directory of that lock, with the attribute the call has to carry for a crate, `cargo_config`, and for a NuGet package, `nuget_config`, and for a crate `scan --mode vendored` as well; for a Maven jar `get pkg:maven//@ --mode hosted` at the Bazel root, or with `--mode vendored`, the package by its coordinates alone, without the qualifiers of its purl; where hosted and vendored mode write no entry for the hub, in the place of the command ``Hosted and vendored mode write no entry for this hub (``): .`` with the `maven_bazel_*` code and the reason of the warning that the command gives (those of vendored mode: an option about downloads does not count); before either, when the block holds the entry of another patch for the jar, ``MODULE.bazel holds the entry of patch for this jar, and where the block is in effect the Bazel build takes that patch's jar.``; for a vendored PyPI copy `vendor --revert` first), and for an installed copy ends with the way out of a repository that ships the installed tree (``If what you ship is the installed tree and not what Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor).``; a Maven statement, which is one for every mode, says ``the patch is only in what Maven or Gradle builds from`` where the others say ``the patch is only in the installed copy``, and ends ``If what you ship is what Maven or Gradle builds and not what Bazel builds, pass …``), with no `justification` and no `impact_statement`; they come after the `not_affected` statement of an advisory that a patched package shares. The warning, one per package, starts `: the VEX document says affected.` and goes on with the action statement; no `vendored_tree_out_of_sync` warning is given for such a package. The document is always written for it, and the exit code is the one of a run without it: standalone `vex` exits 0 with the document, also when every statement is `affected`. It does not report the package as verified: each such statement is a `skipped` event with this `errorCode` and `details.status: "affected"`, counted in `summary.skipped` and not in `summary.verified`, and the envelope's `status` is `partialFailure`. A command run with `--vex` writes the document, keeps its exit code and status, and carries the warning in `vex.warnings` and the number of such statements in `vex.affected`. The one-line summary counts them: `Wrote OpenVEX document with 2 statements (1 affected) to `. `--no-verify` changes none of it. Not raised in a run with `--no-bazel` / `--vex-no-bazel` / `SOCKET_VEX_NO_BAZEL`, under which the Bazel workspace is not read for the document (see "The installed tree" under "VEX provenance markers": what the host command itself does and says in a Bazel workspace stays), for a hosted pin of another ecosystem than Maven, for a vendored gem or NuGet package (no test builds one under its hub), for a vendored crate (rules_rust builds it), in a `--global` run, for a patch whose record names no advisory (no run attests it), for the lock of another project (beside the project, below it unless it is a `packages.lock.json`, or above it without naming it), for a package that only the env document of a pnpm 11 lock lists, for a call that is not a statement of its own (in a comprehension, behind `and`, in a conditional, in the arguments of another call: it may never run), for a call that passes `*` or `**`, a `pip.parse` with `uv_lock` or a `crate.from_cargo` with a cargo-bazel lock, `lockfile` (it may take its packages from a file that is not read), for a crate the lock does not take from a registry, for a NuGet package when the `nuget_config` of any `nuget.from_lock` call names the patch, is not a plain string label of the main repository or cannot be read, or when such a call passes `*` or `**` (rules_csharp makes one hub of all the calls, so the pin of another project may be what it builds), for a Maven jar when the block holds the entry of its patch, or was changed by hand and still calls `override_repo`, when a `maven.override` names the artifact, when the hub that would fetch it or its lock cannot be read (what is not known of another hub, such as a second hub without `lock_file` or a `maven_install` of a `WORKSPACE` file, changes nothing), or when the checkout does not say that Bazel reads `MODULE.bazel` as it stands (`.bazeliskrc` or `.bazelversion` names a Bazel before 7.0, or neither names a release while a `WORKSPACE` file defines a repository; an rc file of the checkout names `enable_bzlmod` other than switched on while the checkout names no Bazel 9 or later, or `--ignore_dev_dependency` without switching it off, on whatever line, or cannot be read), or where the files are not read with certainty (a doubt of the reader, a `WORKSPACE` file that defines repositories, which for a Maven jar counts only while the checkout names no Bazel release, a label that is not a plain string, a lock that cannot be read, is over 64 MiB or is a link out of the Bazel root): the statement is then the `not_affected` one of a project without Bazel. See "Bazel workspaces: VEX" in `docs/ecosystems.md`. | | `vex_pnpm_bundled_copy` | run warning and `failed[].reason` | An npm pin is wired, but its pnpm lock names a package whose `bundledDependencies` may ship an unpatched copy of it (the lock does not record that copy's version); no statement while that package bundles it. `vendor --check` and `scan` still treat the wiring as live. | | `vex_deno_lock_copy` | run warning and `failed[].reason` | An npm pin is wired, but `deno.lock` locks the same `name@version`, which `deno install` installs without the wiring; no statement while it does. `vendor --check` and `scan` still treat the wiring as live. | | `vex_maven_reactor_root` | run warning and `failed[].reason` | A hosted Maven patch is wired in a reactor (`` / ``, a profile's included, or Maven 4 subproject discovery) one of whose modules may build the unpatched version: it resolves the patched version, or its selection cannot be told on the machine that runs `vex`, and no pin or declaration of the patch reaches it; or a declaration may still build the base version; or a pom of the reactor cannot be read. No statement; the note names the module (#261). `list`, `rollback` and `remove` still find the wiring. | diff --git a/crates/socket-patch-cli/src/commands/vex.rs b/crates/socket-patch-cli/src/commands/vex.rs index 74a908d22..436724d99 100644 --- a/crates/socket-patch-cli/src/commands/vex.rs +++ b/crates/socket-patch-cli/src/commands/vex.rs @@ -1902,17 +1902,13 @@ const BAZEL_NOT_IN_BUILD: &str = "vex_bazel_not_in_build"; const AFFECTED_REASON: &str = "attested as affected, not as patched: Bazel does not build the \ patched copy of this package; the run warning names what it builds from"; -/// The closing sentence of the action statement, and so of the run warning, -/// of a package that is affected because its patch is in the installed copy -/// alone. -const INSTALLED_TREE_SHIPS: &str = "If what you ship is the installed tree and not what Bazel \ - builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor)."; - /// In a Bazel workspace, a hub of aspect_rules_js, rules_python, -/// rules_ruby, rules_rust or rules_csharp builds a package from the lock a -/// workspace file names, never from the copy a package manager installed, -/// and rules_python builds nothing from a line that points at a copy under -/// `.socket/vendor` ([`socket_patch_core::bazel::built`]). A patch that is +/// rules_ruby, rules_rust, rules_csharp or rules_jvm_external builds a +/// package from the lock a workspace file names, never from the copy a +/// package manager installed (a jar of the Maven local repository only at +/// the lock's sha256), and rules_python builds nothing from a line +/// that points at a copy under `.socket/vendor` +/// ([`socket_patch_core::bazel::built`]). A patch that is /// only in such a copy is in no build of Bazel's, so its package is not /// attested as patched: it leaves `outcome.applied` and is returned with /// what to do about it, for statements that say `affected` @@ -1927,6 +1923,10 @@ const INSTALLED_TREE_SHIPS: &str = "If what you ship is the installed tree and n /// be read with certainty: the statement stays the one of a project /// without Bazel. So is a patch whose record names no advisory: no run /// writes a statement for it, and it is left out for that reason alone. +/// A Maven patch is asked about whichever mode recorded it: no file of a +/// project decides what a rules_jvm_external hub fetches, so a pin of a +/// `pom.xml` or a Gradle file, and a copy vendored for such a build, are +/// in the Bazel build only through an entry of the root `MODULE.bazel`. /// /// With `--no-bazel` / `--vex-no-bazel` (`params.no_bazel`) the user has /// said that the document is about the installed tree, which is what the @@ -1950,7 +1950,8 @@ async fn unbuilt_by_bazel( } let patched = |purl: &String| { let record = plan.view.patches.get(purl)?; - let judged = !plan.hosted.contains_key(purl) && !record.vulnerabilities.is_empty(); + let pinned = plan.hosted.contains_key(purl) && !purl.starts_with(MAVEN); + let judged = !pinned && !record.vulnerabilities.is_empty(); judged.then(|| Patched { purl: purl.clone(), uuid: record.uuid.clone(), @@ -1975,20 +1976,28 @@ async fn unbuilt_by_bazel( affected } +/// The start of a Maven purl. +const MAVEN: &str = "pkg:maven/"; + /// The action statement for `package`, whose patch Bazel does not build: /// what `call` builds the package from, and what to run so that the build /// holds the patch. For an installed copy that is the hub's own remedy /// ([`Unbuilt::remedy`]). Hosted and vendored mode write in the project -/// they run in, so it names the directory of the lock. +/// they run in, so it names the directory of the lock, and for a Maven jar +/// the Bazel root, where the patch of a hub is written, or what keeps +/// those modes from writing it. /// /// For an installed copy it ends with the way out of a repository that /// ships the installed tree and keeps the hub for something else: the /// option, in both spellings, since the statement is the same whichever -/// command wrote the document. Not for a committed PyPI copy: there the +/// command wrote the document. A Maven patch has the same statement in +/// every mode, a committed copy included, in words that hold for each: +/// the patch is in what Maven or Gradle builds from, and that build is +/// what such a repository ships. Not for a committed PyPI copy: there the /// build is broken or takes the registry's package, and the installed tree /// is not the patched one either. fn bazel_build_action(package: &Patched, call: &Unbuilt) -> String { - let Patched { uuid, pins, .. } = package; + let Patched { purl, uuid, pins } = package; let Unbuilt { call: tag, file, @@ -1996,11 +2005,20 @@ fn bazel_build_action(package: &Patched, call: &Unbuilt) -> String { lock, remedy, } = call; - match pins { + let maven = purl.starts_with(MAVEN); + let (only, ships) = match maven { + true => ( + "what Maven or Gradle builds from", + "what Maven or Gradle builds", + ), + false => ("the installed copy", "the installed tree"), + }; + match pins.as_ref().filter(|_| !maven) { None => format!( "Bazel builds this package from {lock} (`{tag}`, {file}:{line}), which does not \ - name Socket patch {uuid}: the patch is only in the installed copy. {remedy} \ - {INSTALLED_TREE_SHIPS}" + name Socket patch {uuid}: the patch is only in {only}. {remedy} If what you ship \ + is {ships} and not what Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, \ + apply and vendor)." ), Some(_) => format!( "Bazel builds no package from the line of {lock} that points at the copy of Socket \ diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs index f8f76acce..666062854 100644 --- a/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs @@ -1452,7 +1452,7 @@ impl Cell { /// Agent mode in this checkout, through `run`, for a package that its /// package manager keeps outside the checkout, where `installed` is a - /// file of that copy: `scan --mode agent --yes` gives the file the text + /// file of that copy: `scan --mode agent --yes` gives the file the bytes /// `patched`, from the record of the patch, and writes nothing into the /// checkout but its own `.socket` directory, and the `apply` of a later /// run passes. Then what the two say with `--vex`, and what `vex` and @@ -1462,7 +1462,7 @@ impl Cell { &self, run: impl Fn(&Cell, &[&str]) -> Ran, installed: &Path, - patched: &str, + patched: impl AsRef<[u8]>, claim: &Claim, ) -> Tree { let test = &self.test; @@ -1478,8 +1478,9 @@ impl Cell { let mut wrote = changed(&before, &left); wrote.retain(|file| !file.starts_with(".socket/")); assert_eq!(wrote, NOTHING, "{test}: {said}"); - let holds = std::fs::read_to_string(installed).unwrap_or_default(); - assert_eq!(holds, patched, "{test}: {}", installed.display()); + let holds = std::fs::read(installed).unwrap_or_default(); + let (at, shown) = (installed.display(), String::from_utf8_lossy(&holds)); + assert!(holds == patched.as_ref(), "{test}: {at} holds:\n{shown}"); let again = self.document("apply"); let apply = run(self, &[&["apply"][..], &with_vex(&again)].concat()); diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/maven.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/maven.rs index c1ab67684..1f02fbe68 100644 --- a/crates/socket-patch-cli/tests/e2e_bazel_build/maven.rs +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/maven.rs @@ -71,6 +71,15 @@ //! hosted command then takes the vendored entry, its directory and its //! ledger entry out again, and the checkout is the one the first hosted //! command left; `rollback` undoes a vendored patch to the byte. +//! - [`an_agent_patch_beside_a_pinned_hub`]: agent mode in a checkout that +//! Maven builds too. `scan --mode agent --yes` puts the patched jar in +//! the Maven local repository and patches no hub: the build prints the +//! upstream marker before and after, and the document of the run, of +//! `apply`, of `vex` and of `vex --no-verify` says that the artifact is +//! affected, with the lock and the `maven.install` Bazel builds it from +//! and the command that patches the hub. After that command, the hosted +//! one, the build prints the marker of the patched jar and `vex` says +//! not affected. //! //! A command that writes the block gives no warning. One that does not //! gives one, in the place of "no pom.xml", with the reason and the words @@ -117,6 +126,7 @@ //! Java embedded in Bazel. use std::io::Write as _; +use std::path::Path; use std::time::Instant; use base64::Engine as _; @@ -127,9 +137,10 @@ use sha2::{Digest, Sha256, Sha512}; use socket_patch_core::bazel::maven::{Lock, Pinned as Jar}; use socket_patch_core::hash::git_sha256::compute_git_sha256_from_bytes as git_sha256; -use crate::envelope::{event_for, event_triples}; +use crate::envelope::{codes_in, event_for, event_triples}; use crate::harness::{ - changed, rewrite, Cell, Claim, Fixture, How, Ran, StandIn, Tree, CONSUMER, NOTHING, + cell_without_bazel, changed, fixture_path, rewrite, with_vex, Cell, Claim, Fixture, How, Ran, + StandIn, Tree, CONSUMER, NOTHING, }; use crate::hosted_memory_common::{Patch, ORG}; @@ -431,15 +442,68 @@ struct Pinned { } impl Pinned { - /// Step 0 of every flow: the fixture's checkout for `test`, with a + /// Step 0 of every flow: the fixture's checkout for `test` + /// ([`Pinned::laid_out`]), pinned by the ruleset against its stand-in. + /// `None` when no Bazel runs here. + fn open(test: &str, maven: bool, prepare: &dyn Fn(&Cell, &mut String)) -> Option { + let flow = Pinned::laid_out(Cell::open(test, &FIXTURE)?, maven, prepare); + let (cell, repository) = (&flow.cell, flow.repository()); + + // The ruleset's own relock. The lock it writes holds the victim, + // which `MODULE.bazel` does not name, at the sha256 of this run's + // jar, in the format of the era. + let pin = cell.bazel_env("run", &[&format!("@{HUB}//:pin")], &[("REPIN", "1")]); + assert!(pin.ok(), "{test}: {}", pin.dump()); + let lock = std::fs::read_to_string(cell.path("maven_install.json")).expect("the lock"); + let raw: Value = serde_json::from_str(&lock).expect("the lock is JSON"); + // Nothing else is in it: no other Bazel module added to the hub, + // and its one repository is the stand-in. + let keys = |of: &str| { + let map = raw[of].as_object().expect("a map of the lock"); + map.keys().map(String::as_str).collect::>().join(" ") + }; + assert_eq!( + (keys("artifacts"), keys("repositories")), + ( + format!("{GROUP}:consumer {GROUP}:{VICTIM}"), + format!("{repository}/") + ), + "{test}: {raw:#}" + ); + let lock = Lock::parse(&lock).unwrap_or_else(|why| panic!("{test}: {why}:\n{lock}")); + let format = if cell.numbers() >= (9, 0, 0) { 3 } else { 2 }; + let jar = Jar { + version: VERSION.to_string(), + jar_sha256: Some(sha256_hex(&flow.upstream_jar())), + fetched: true, + }; + assert_eq!( + (lock.format, lock.plain_jar(GROUP, VICTIM)), + (format, Some(&jar)), + "{test}: {lock:?}" + ); + Some(flow) + } + + /// The repository of the hub: the stand-in's. + fn repository(&self) -> String { + format!("{}/{UPSTREAM}", self.standin.origin()) + } + + /// The upstream jar of the victim that the stand-in serves. + fn upstream_jar(&self) -> Vec { + victim(&format!("PRISTINE {}", self.run)) + } + + /// The checkout `cell` of the fixture before its hub is pinned, with a /// `pom.xml` that declares the victim when `maven` is set and with /// what `prepare` adds to its files and to the text of `MODULE.bazel`, - /// pinned by the ruleset against a stand-in that has the two - /// artifacts, the patched jar and the grant of its patch. `None` when - /// no Bazel runs here. - fn open(test: &str, maven: bool, prepare: &dyn Fn(&Cell, &mut String)) -> Option { + /// against a stand-in that has the two artifacts, the patched jar and + /// the grant of its patch. The lock is the empty file the ruleset's + /// documentation starts from. + fn laid_out(mut cell: Cell, maven: bool, prepare: &dyn Fn(&Cell, &mut String)) -> Pinned { let started = Instant::now(); - let mut cell = Cell::open(test, &FIXTURE)?; + let test = cell.test.clone(); // The cache of the Coursier that pins. Before 7.2 the ruleset // leaves the choice to Coursier, which without this takes the home // directory from the system, whatever `HOME` says. @@ -493,41 +557,8 @@ impl Pinned { std::fs::write(&file, bytes).expect("write the local repository"); } - // The ruleset's own relock. The lock it writes holds the victim, - // which `MODULE.bazel` does not name, at the sha256 of this run's - // jar, in the format of the era. - let pin = cell.bazel_env("run", &[&format!("@{HUB}//:pin")], &[("REPIN", "1")]); - assert!(pin.ok(), "{test}: {}", pin.dump()); - let lock = std::fs::read_to_string(cell.path("maven_install.json")).expect("the lock"); - let raw: Value = serde_json::from_str(&lock).expect("the lock is JSON"); - // Nothing else is in it: no other Bazel module added to the hub, - // and its one repository is the stand-in. - let keys = |of: &str| { - let map = raw[of].as_object().expect("a map of the lock"); - map.keys().map(String::as_str).collect::>().join(" ") - }; - assert_eq!( - (keys("artifacts"), keys("repositories")), - ( - format!("{GROUP}:consumer {GROUP}:{VICTIM}"), - format!("{repository}/") - ), - "{test}: {raw:#}" - ); - let lock = Lock::parse(&lock).unwrap_or_else(|why| panic!("{test}: {why}:\n{lock}")); - let format = if cell.numbers() >= (9, 0, 0) { 3 } else { 2 }; - let jar = Jar { - version: VERSION.to_string(), - jar_sha256: Some(sha256_hex(&upstream_jar)), - fetched: true, - }; - assert_eq!( - (lock.format, lock.plain_jar(GROUP, VICTIM)), - (format, Some(&jar)), - "{test}: {lock:?}" - ); let upstream = format!("OUT: PRISTINE {run}"); - Some(Pinned { + Pinned { cell, standin, tools, @@ -537,7 +568,7 @@ impl Pinned { upstream, upstream_route: format!("/{UPSTREAM}/{}", repo_path(VICTIM, VERSION, "jar")), started, - }) + } } /// Step 1: the build prints the upstream marker, from a jar Bazel @@ -560,22 +591,38 @@ impl Pinned { /// `standin`, with the directories of [`Pinned::tools`] in the place of /// the machine's. fn socket_patch_in(&self, cell: &Cell, standin: &StandIn, args: &[&str]) -> Ran { - let dir = |name: &str| self.tools.path().join(name).to_string_lossy().into_owned(); - let (m2, gradle, coursier) = (dir("m2"), dir("gradle"), dir("coursier")); - let env = [ - ("MAVEN_REPO_LOCAL", m2.as_str()), - ("GRADLE_USER_HOME", &gradle), - ("COURSIER_CACHE", &coursier), - ]; + let tools = self.tools_env(); + let env = tools.each_ref().map(|(name, dir)| (*name, dir.as_str())); cell.socket_patch(&standin.origin(), args, &env) } + /// The directories of [`Pinned::tools`], each as the variable that + /// puts it in the place of the machine's. + fn tools_env(&self) -> [(&'static str, String); 3] { + let dir = |name: &str| self.tools.path().join(name).to_string_lossy().into_owned(); + [ + ("MAVEN_REPO_LOCAL", dir("m2")), + ("GRADLE_USER_HOME", dir("gradle")), + ("COURSIER_CACHE", dir("coursier")), + ] + } + /// `socket-patch ` for a command of vendored mode, which downloads /// the patched jar from the URL the grant has for it on the patch host: /// that host is the stand-in here. fn vendored(&self, args: &[&str]) -> Ran { + self.serviced(&self.cell, args) + } + + /// [`Pinned::vendored`] in the checkout `cell`. Agent mode takes the + /// patched jar from that URL as well. + fn serviced(&self, cell: &Cell, args: &[&str]) -> Ran { let origin = self.standin.origin(); - self.socket_patch(&[args, &["--patch-server-url", &origin]].concat()) + self.socket_patch_in( + cell, + &self.standin, + &[args, &["--patch-server-url", &origin]].concat(), + ) } /// The command where it writes no entry for the hub, in a tree without @@ -1352,6 +1399,286 @@ fn the_users_own_override_of_the_artifact() { ); } +/// The warning of an agent run for an artifact that a pinned hub fetches: +/// the command patches no hub. +const HUB_UNPATCHED: &str = "maven_bazel_hub_unpatched"; +/// What an action statement says to run where hosted and vendored mode +/// write an entry for the hub: the package by its coordinates. +const RUN: &str = "Run `socket-patch get pkg:maven/dev.socket.fixture/victim@1.10.0 --mode \ + hosted` at the Bazel root to patch the hub, or with `--mode vendored` to \ + commit the patched jar."; +/// And what it says where they write none, before the code of their +/// warning and its reason. +const NO_ENTRY: &str = "Hosted and vendored mode write no entry for this hub"; + +impl Pinned { + /// Without a Bazel: the checkout of [`Pinned::open`] beside a `pom.xml`, + /// in a workspace copy that no Bazel has built in. No ruleset pins it, + /// so the lock is written here: the two artifacts under the stand-in's + /// repository, in the format the ruleset of Bazel 9.3.2 writes. And no + /// Bazel leaves its own lock, so what hosted and vendored mode read + /// before they write an entry is written here too: the Bazel release, + /// and the release of the ruleset that Bazel selected. + fn unbuilt() -> Pinned { + let cell = cell_without_bazel("9.3.2"); + let module = fixture_path(FIXTURE.dir).join("e9").join(MODULE); + cell.write(MODULE, std::fs::read(module).expect(MODULE)); + cell.write(".git/HEAD", "ref: refs/heads/main\n"); + let flow = Pinned::laid_out(cell, true, &|_, _| {}); + let sums = |jar: &[u8]| json!({ "jar": sha256_hex(jar) }); + let keys = [format!("{GROUP}:consumer"), format!("{GROUP}:{VICTIM}")]; + let lock = json!({ + "artifacts": { + &keys[0]: { "shasums": sums(&jar(&[])), "version": "2.0" }, + &keys[1]: { "shasums": sums(&flow.upstream_jar()), "version": VERSION }, + }, + "repositories": { format!("{}/", flow.repository()): keys }, + "version": "3", + }); + flow.cell.write("maven_install.json", format!("{lock:#}\n")); + let selected = "https://bcr.bazel.build/modules/rules_jvm_external/7.2/source.json"; + let lock = json!({ "lockFileVersion": 13, "registryFileHashes": { selected: "0" } }); + flow.cell.write("MODULE.bazel.lock", format!("{lock:#}\n")); + flow.cell.write(".bazelversion", "9.3.2\n"); + flow + } + + /// What every document of a patch that the hub does not take says, as + /// its action statement and in the warning of its run: the lock of the + /// hub, the call and its line, `remedy`, which is the command of the + /// two modes that patch a pinned hub ([`RUN`]) or why they write no + /// entry for this one, and the option for a repository that ships what + /// Maven or Gradle builds. + fn affected(&self, remedy: &str) -> Vec { + let module = std::fs::read_to_string(self.cell.path(MODULE)).expect(MODULE); + let call = module.lines().position(|line| line == "maven.install("); + vec![format!( + "Bazel builds this package from maven_install.json (`maven.install`, {MODULE}:{}), \ + which does not name Socket patch {}: the patch is only in what Maven or Gradle \ + builds from. {remedy} If what you ship is what Maven or Gradle builds and not what \ + Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor).", + call.expect("the hub") + 1, + self.patch.uuid + )] + } + + /// Agent mode in the checkout ([`Cell::patches_outside`]). The victim's + /// jar is in the Maven local repository, as a Maven build of the + /// checkout's `pom.xml` leaves it: `scan --mode agent --yes` puts the + /// jar of the patch service in its place and writes nothing into the + /// checkout but its `.socket` directory, and each of the four + /// documents says `claim`. A later `apply` warns that it patches no hub + /// ([`HUB_UNPATCHED`]). Gives the files of the checkout. + fn patches_the_installed_jar(&self, claim: &Claim) -> Tree { + let test = &self.cell.test; + let installed = self.tools.path().join("m2"); + let installed = installed.join(repo_path(VICTIM, VERSION, "jar")); + let patched = victim(&format!("PATCHED {}", self.patch.uuid)); + let run = |cell: &Cell, args: &[&str]| self.serviced(cell, args); + let left = self.cell.patches_outside(run, &installed, &patched, claim); + let apply = self.serviced(&self.cell, &["apply"]); + let warned = codes_in(&apply.envelope["warnings"]); + assert_eq!(warned, [HUB_UNPATCHED], "{test}: {}", apply.dump); + left + } +} + +/// Agent mode beside a pinned hub that fetches the artifact. +/// +/// - (a) `bazel build` and `bazel run //:consumer` print the upstream +/// marker. +/// - (b) `scan --mode agent --yes` patches the jar in the Maven local +/// repository and warns that it patches no hub, and `apply` passes +/// ([`Pinned::patches_the_installed_jar`]). Both, `vex` and +/// `vex --no-verify` exit 0 and write a document that says the artifact +/// is affected, with what to do about it ([`Pinned::affected`]). +/// - (c) The same two Bazel commands print the upstream marker again, as +/// the documents said, and change no file: the hub fetches the jar its +/// lock pins, at the lock's sha256, and no build reads the Maven local +/// repository. +/// - (d) The hosted command the documents name pins the Maven build and +/// the hub. The same build prints the marker of the patched jar, and +/// `vex` says not affected, through the hosted patch. +#[test] +#[ignore = "real Bazel; run with --ignored"] +fn an_agent_patch_beside_a_pinned_hub() { + let test = "maven::an_agent_patch_beside_a_pinned_hub"; + let Some(flow) = Pinned::open(test, true, &|_, _| {}) else { + return; + }; + // (a) The upstream build, with the jar from the stand-in. + flow.builds_upstream(); + flow.names_its_bazel(); + + // (b) Agent mode patches the installed jar, and every document says + // the artifact is affected. + let names = flow.affected(RUN); + let claim = Claim::unbuilt(PURL, &flow.patch.uuid, &names); + let patched = flow.patches_the_installed_jar(&claim); + + // (c) The build is the one of (a): Bazel never reads the patched jar. + flow.cell.builds(&flow.upstream); + assert_eq!(changed(&patched, &flow.cell.tree()), NOTHING, "{test}"); + + // (d) What the documents say to run: the build is patched, and the + // document says so. + let pinned = flow.pins(&patched, &PINNED); + flow.builds_patched(&pinned); + let run = |cell: &Cell, args: &[&str]| flow.socket_patch_in(cell, &flow.standin, args); + let hosted = Claim::new(PURL, &flow.patch.uuid, How::Hosted); + flow.cell.stands_alone(run, &hosted, true); + println!( + "{test}: Bazel {} built {:?} before and after `scan --mode agent` patched {VICTIM}-\ + {VERSION}.jar in the Maven local repository, and every form of `vex` said the artifact \ + is affected, in the words {names:?}; after `get --mode hosted` it built {:?} and `vex` \ + said not affected; {:?}", + flow.cell.release(), + flow.upstream, + flow.patch.marker, + flow.started.elapsed() + ); +} + +/// A second hub, which has no lock: what is not known of it leaves the hub +/// that fetches the jar as it is. +const MORE: &str = "maven.artifact(\n name = \"more\",\n artifact = \"gson\",\n group = \ + \"com.google.code.gson\",\n version = \"2.8.9\",\n)\n"; + +/// Without a Bazel: step (b) of [`an_agent_patch_beside_a_pinned_hub`] in a +/// checkout whose hub is pinned ([`Pinned::unbuilt`]). Then each of the two +/// modes the document names, in a checkout of its own: the command pins or +/// vendors the artifact for the Maven build and for the hub, with no +/// warning about the hub, and `vex` says not affected, through that mode. +/// +/// And each where the command writes no entry: in a checkout that names a +/// Bazel without `override_repo`, and beside a second hub without a lock, +/// where `apply` warns that the hub stays unpatched all the same. There +/// the documents of agent mode say affected too, and name the code and +/// the reason of the command's warning in the place of the command. The +/// command then pins or vendors for the Maven build alone and gives that +/// warning, and `vex` still says affected, in the same words: the hub +/// fetches the upstream jar as before, whichever mode holds the patch. +#[test] +fn an_installed_jar_is_affected_beside_a_hub_that_fetches_its_own() { + let modes = [("hosted", How::Hosted), ("vendored", How::Vendored)]; + let old = "the checkout names Bazel 7.3.0 (`.bazeliskrc` or `.bazelversion`), and an entry \ + needs `override_repo`, which Bazel has from 7.4.0"; + let lockless = "hub \"more\" has no `lock_file`, so Bazel resolves its jars when it builds \ + and none can be replaced: give the hub a `lock_file` and pin it (`REPIN=1 \ + bazel run @more//:pin`)"; + // The file that differs, with what is added to it or put in its place, + // and the code and the reason with which the command then declines. + let checkouts = [ + (MODULE, "", None), + ( + ".bazelversion", + "7.3.0\n", + Some(("maven_bazel_release", old)), + ), + (MODULE, MORE, Some(("maven_bazel_not_pinned", lockless))), + ]; + let each = + |(mode, how)| checkouts.map(|(file, text, declined)| (mode, how, file, text, declined)); + for (mode, how, file, text, declined) in modes.into_iter().flat_map(each) { + let flow = Pinned::unbuilt(); + let module = std::fs::read_to_string(flow.cell.path(MODULE)).expect(MODULE); + match file { + MODULE => flow.cell.write(MODULE, format!("{module}{text}")), + _ => flow.cell.write(file, text), + } + // The reason names the place of a tag: the one added last. + let at = format!("{MODULE}:{}: ", module.lines().count() + 1); + let remedy = declined.map(|(code, why)| { + let at = if why.starts_with("hub ") { &at } else { "" }; + format!("{NO_ENTRY} (`{code}`): {at}{why}.") + }); + let names = flow.affected(remedy.as_deref().unwrap_or(RUN)); + let claim = Claim::unbuilt(PURL, &flow.patch.uuid, &names); + flow.patches_the_installed_jar(&claim); + + let get = flow.vendored(&["get", PURL, "--mode", mode, "--yes"]); + assert_eq!(get.envelope["summary"]["applied"], 1, "{}", get.dump); + let mut warned = codes_in(&get.envelope["warnings"]); + warned.retain(|code| code.starts_with("maven_bazel_")); + let module = std::fs::read_to_string(flow.cell.path(MODULE)).expect(MODULE); + let entry = module.contains(&format!("Socket patch {}", flow.patch.uuid)); + let code = declined.map_or("", |(code, _)| code); + assert_eq!( + (warned.join(" "), entry), + (code.to_string(), declined.is_none()), + "{}", + get.dump + ); + let run = |cell: &Cell, args: &[&str]| flow.socket_patch_in(cell, &flow.standin, args); + let patched = Claim::new(PURL, &flow.patch.uuid, how); + let says = if declined.is_some() { &claim } else { &patched }; + flow.cell.stands_alone(run, says, true); + } +} + +/// Without a Bazel: what stays the statement of a project without Bazel +/// while the patch is in the installed jar alone +/// ([`Cell::keeps_its_statement`]). The hubs of which nothing is said: one +/// that has no lock, one with a `maven.override` of the artifact, and one +/// whose call may not run. So is the checkout that does not say that Bazel +/// reads `MODULE.bazel` as it stands: an rc file names the option that +/// switches Bzlmod off, for one `--config`; no release is named beside a +/// `WORKSPACE` file that defines a repository; the release named is one +/// before 7.0. And what a person reads of an `apply` beside the hub that +/// fetches the jar: the warning and the count of the statement, and +/// nothing under `--silent`. +#[test] +fn an_installed_jar_is_attested_where_no_hub_is_shown_to_fetch_its_own() { + let flow = Pinned::unbuilt(); + let names = flow.affected(RUN); + let claim = Claim::unbuilt(PURL, &flow.patch.uuid, &names); + flow.patches_the_installed_jar(&claim); + + let (origin, tools) = (flow.standin.origin(), flow.tools_env()); + let told = tools.each_ref().map(|(name, dir)| (*name, dir.as_str())); + let document = flow.cell.document("person"); + let warned = format!( + "Warning: {PURL}: the VEX document says affected. {}", + names[0] + ); + let loud = [&["apply"][..], &with_vex(&document)].concat(); + let silent = [&loud[..], &["--silent"]].concat(); + for apply in [loud, silent] { + flow.cell + .tells_a_person(&origin, &apply, &told, &document, &claim, &warned); + } + + let run = |cell: &Cell, args: &[&str]| flow.socket_patch_in(cell, &flow.standin, args); + let installed = Claim::new(PURL, &flow.patch.uuid, How::Agent); + let repository = "http_archive(name = \"zlib\", urls = [])\n"; + for (file, text, bazel) in [ + (".bazelrc", "build:legacy --noenable_bzlmod\n", "7.7.2\n"), + ("WORKSPACE", repository, "latest\n"), + ("WORKSPACE", "", "6.5.0\n"), + ] { + flow.cell.write(file, text); + flow.cell.write(".bazelversion", bazel); + flow.cell.stands_alone(run, &installed, true); + std::fs::remove_file(flow.cell.path(file)).expect(file); + } + flow.cell.write(".bazelversion", "9.3.2\n"); + + let module = std::fs::read_to_string(flow.cell.path(MODULE)).expect(MODULE); + let lock = " lock_file = \"//:maven_install.json\",\n"; + let own = format!( + "maven.override(\n name = \"{HUB}\",\n coordinates = \"{GROUP}:{VICTIM}\",\n \ + target = \"//own:victim\",\n)\n" + ); + let undecided = [ + module.replace(lock, ""), + format!("{module}{own}"), + module.replace("maven.install(", "ENABLED and maven.install("), + ]; + let gives = |args: &[&str], document: &Path| flow.cell.gives(&origin, args, &told, document); + flow.cell + .keeps_its_statement(run, gives, &installed, &undecided); +} + /// Without a Bazel: the fixture's directories and the legs of its workflow /// are those of the table, each era's `MODULE.bazel` has a hub of its own /// name with the one repository a flow replaces, and a jar made here holds diff --git a/crates/socket-patch-core/src/bazel/built.rs b/crates/socket-patch-core/src/bazel/built.rs index f569ec39d..c66f66927 100644 --- a/crates/socket-patch-core/src/bazel/built.rs +++ b/crates/socket-patch-core/src/bazel/built.rs @@ -19,6 +19,19 @@ //! rules_ruby does with a vendored gem and rules_csharp with a vendored //! package no test here builds, so neither is judged. //! +//! A sixth, rules_jvm_external, takes each jar that the lock of a hub pins +//! (`maven.install`) at the lock's sha256: from a repository of the lock, +//! or under `m2local` from the Maven local repository, where a jar that +//! agent mode patched no longer has that sha256. It reads no `pom.xml` +//! and no Gradle file either. Its hubs are not in the table below: +//! [`super::maven`] reads them for hosted and vendored mode, which patch a +//! hub through a block of the root `MODULE.bazel` and not through its +//! lock, and the same reader answers here ([`super::maven::upstream`]): +//! for a Maven patch of any mode, whatever project of the workspace it is +//! in, when a pinned hub that was read fetches the jar and the block holds +//! no entry of that patch for it. What makes that uncertain is that +//! reader's to say, and the next paragraph is about the table alone. +//! //! [`unbuilt`] names, for a patched package, the call that builds it from a //! file without its patch. It says so only when the files of the workspace //! show it: the reader's answer is certain ([`Workspace::doubts`] and the @@ -67,7 +80,7 @@ //! `local_package_dirs` are not read. Whether a hub takes a hosted pin of //! the project itself is not judged here: [`super::cargo`] and //! [`super::nuget`] say that to hosted mode, and the caller hands over no -//! package that has one. +//! package that has one, but a Maven jar. //! //! Not looked at: what a `--override_repository` or an rc file changes, //! BUILD files, and hubs of other rulesets. @@ -106,7 +119,9 @@ pub struct Patched { /// `None`: the patch is in the copy a package manager installed. /// `Some`: it is a copy committed under `.socket/vendor`, and these /// files of the project point at it, each by its path from the project - /// with `/` between its parts. + /// with `/` between its parts. They decide nothing for a Maven jar, + /// which is asked about in every mode: no file of a project says what + /// a rules_jvm_external hub fetches. pub pins: Option>, } @@ -123,8 +138,9 @@ pub struct Unbuilt { /// the file that points at it. pub lock: String, /// What to run so that the build holds the patch of an installed copy - /// ([`Hub::remedy`]). - pub remedy: &'static str, + /// ([`Hub::remedy`]). For a Maven jar, the command that patches the + /// hub, or why none does ([`super::maven::upstream`]). + pub remedy: String, } /// The tag of a module extension that names the files a ruleset builds the @@ -309,30 +325,57 @@ fn ecosystem(purl: &str) -> Option { /// Nothing is read for packages no hub is known for, nor outside a Bazel /// workspace ([`super::bazel_root`], whose bounds these are). pub async fn unbuilt(project: &Path, packages: Vec) -> Vec> { - let hub = |package: &Patched| { - let of = ecosystem(&package.purl); - HUBS.iter().any(|hub| of.as_deref() == Some(hub.ecosystem)) - }; - if !packages.iter().any(hub) { + let of = |package: &Patched| ecosystem(&package.purl); + let known = |of: Option| HUBS.iter().any(|hub| of.as_deref() == Some(hub.ecosystem)); + let maven = |package: &Patched| of(package).as_deref() == Some("maven"); + // The table's reader is not asked for a run of Maven jars alone. + let table = packages.iter().any(|package| known(of(package))); + if !table && !packages.iter().any(maven) { return vec![None; packages.len()]; } let project = project.to_path_buf(); run_blocking(move || { + let mut found = vec![None; packages.len()]; let Some(root) = super::bazel_root(&project) else { - return vec![None; packages.len()]; + return found; }; - let workspace = Workspace::read(&disk(&root.dir)); - let read = |file: &str| lock_text(&root.dir, file); - // Bazel has `/` between the parts of a path on every system. - let parts = root.project.components(); - let parts: Vec<_> = parts - .map(|part| part.as_os_str().to_string_lossy()) - .collect(); - judge(&workspace, &parts.join("/"), &read, &packages) + if table { + let workspace = Workspace::read(&disk(&root.dir)); + let read = |file: &str| lock_text(&root.dir, file); + // Bazel has `/` between the parts of a path on every system. + let parts = root.project.components(); + let parts: Vec<_> = parts + .map(|part| part.as_os_str().to_string_lossy()) + .collect(); + found = judge(&workspace, &parts.join("/"), &read, &packages); + } + jars(&root.dir, &packages, &mut found); + found }) .await } +/// [`unbuilt`] for the Maven jars among `packages`, in the workspace at +/// `root`, whichever mode patched them: into `found`, the `maven.install` +/// of the pinned hub that fetches the upstream jar, and what +/// [`super::maven::upstream`] says to do about it. A flow builds the hub +/// patched on real Bazel after the command it names, in hosted and in +/// vendored mode. +fn jars(root: &Path, packages: &[Patched], found: &mut [Option]) { + let patches: Vec<_> = packages.iter().map(|p| (&*p.purl, &*p.uuid)).collect(); + let hubs = super::maven::upstream(root, &patches); + for (found, hub) in found.iter_mut().zip(hubs) { + let Some((hub, remedy)) = hub else { continue }; + *found = Some(Unbuilt { + call: super::maven::INSTALL.to_string(), + file: hub.file, + line: hub.line, + lock: hub.lock.unwrap_or_default(), + remedy, + }); + } +} + /// The most of a lock that is read: the lock of a large pnpm workspace has /// some tens of MiB. A larger file decides nothing. const MAX_LOCK: u64 = 64 << 20; @@ -463,7 +506,7 @@ fn judge( file: tag.file.to_string(), line: tag.call.line, lock: lock.clone(), - remedy: hub.remedy, + remedy: hub.remedy.to_string(), }); } } @@ -1358,6 +1401,60 @@ replace = "swapped 3.0.0" assert_eq!(unbuilt(&root.join("api"), vendored).await, [None]); } + /// A Maven jar in a project of a workspace whose pinned hub fetches + /// that jar, patched in the installed copy or in a copy committed for + /// a Maven build: the `maven.install` and the lock of the hub, with + /// the command of the two modes that patch it, which names the jar. + /// Nothing once the block holds the entry of the patch. A `WORKSPACE` + /// file that defines another repository, beside which nothing is said + /// of a package of the table, changes nothing for the jar while the + /// checkout names a Bazel that reads `MODULE.bazel`. + #[tokio::test] + async fn a_maven_jar_is_not_what_a_pinned_hub_fetches() { + let [module, lock, patched] = super::super::maven::tests::sample(UUID); + let tmp = tempfile::tempdir().unwrap(); + let root = std::fs::canonicalize(tmp.path()).unwrap(); + std::fs::create_dir_all(root.join(".git")).unwrap(); + std::fs::create_dir_all(root.join("services/billing")).unwrap(); + let selected = "https://bcr.bazel.build/modules/rules_jvm_external/7.2/source.json"; + let selected = format!( + "{{\"lockFileVersion\": 13, \"registryFileHashes\": {{\"{selected}\": \"0\"}}}}" + ); + for (file, text) in [ + ("MODULE.bazel", module.as_str()), + ("maven_install.json", &lock), + ("MODULE.bazel.lock", &selected), + (".bazelversion", "7.7.2\n"), + ("WORKSPACE", "http_archive(name = \"zlib\", urls = [])\n"), + ] { + std::fs::write(root.join(file), text).unwrap(); + } + let netty = "pkg:maven/io.netty/netty-tcnative-classes@2.0.61.Final"; + let package = |pins: Option>| Patched { + purl: netty.to_string(), + uuid: UUID.to_string(), + pins, + }; + let packages = vec![package(None), package(Some(vec!["pom.xml".to_string()]))]; + let fetched = Unbuilt { + call: "maven.install".to_string(), + file: "MODULE.bazel".to_string(), + line: 4, + lock: "maven_install.json".to_string(), + remedy: format!( + "Run `socket-patch get {netty} --mode hosted` at the Bazel root to patch the \ + hub, or with `--mode vendored` to commit the patched jar." + ), + }; + for project in ["", "services/billing"] { + let found = unbuilt(&root.join(project), packages.clone()).await; + let jars = [Some(fetched.clone()), Some(fetched.clone())]; + assert_eq!(found, jars, "{project}"); + } + std::fs::write(root.join("MODULE.bazel"), patched).unwrap(); + assert_eq!(unbuilt(&root, packages).await, [None, None]); + } + /// On disk a lock is read as a file of the workspace is: through a /// link that stays inside the Bazel root and not through one that /// leads out of it, not past its bound, and as the run left a file it diff --git a/crates/socket-patch-core/src/bazel/maven.rs b/crates/socket-patch-core/src/bazel/maven.rs index 73eb1d69d..6a26f4974 100644 --- a/crates/socket-patch-core/src/bazel/maven.rs +++ b/crates/socket-patch-core/src/bazel/maven.rs @@ -143,6 +143,9 @@ const MAX_LOCKS: usize = 4 * MAX_LOCK; /// `override` and `amend_artifact`, make no hub. const MAKE_A_HUB: [&str; 3] = ["install", "artifact", "from_toml"]; +/// The tag that pins a hub, as a warning and an action statement name it. +pub const INSTALL: &str = "maven.install"; + /// A hub of the root module. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Hub { @@ -487,6 +490,9 @@ pub struct Hubs { /// or a `maven_install.json` lies at the root. In another workspace a /// doubt is about no hub ([`hub_jars`]). ruleset: bool, + /// Whether a `WORKSPACE` file of the root defines anything + /// ([`Workspace::legacy`]), which is what a Bazel before 7.0 builds. + legacy: bool, } impl Hubs { @@ -600,7 +606,7 @@ impl Hubs { .files .iter() .any(|(_, file)| file.calls.iter().any(names)); - let calls = matches!(ws.legacy, Some(Ok(_))); + let (calls, defined) = (matches!(ws.legacy, Some(Ok(_))), ws.legacy.is_some()); let before_9 = || declared_release(view).is_none_or(|release| release < (9, 0, 0)); let legacy = match ws.legacy.filter(|_| before_9()) { Some(Ok((file, read))) => { @@ -634,6 +640,7 @@ impl Hubs { edited, asked, ruleset, + legacy: defined, } } @@ -674,6 +681,16 @@ impl Hubs { (built, _) => built, } } + + /// The id of the patch whose entry the block holds for the jar of + /// `group:artifact` at `version`: where the block is in effect, Bazel + /// takes that entry's jar in the place of a hub's. + fn held(&self, group: &str, artifact: &str, version: &str) -> Option<&str> { + let jar = |patch: &&Patch| { + (&*patch.group, &*patch.artifact, &*patch.version) == (group, artifact, version) + }; + self.block.iter().find(jar).map(|patch| &*patch.uuid) + } } /// One patch in the block: the jar that takes the place of the one a hub @@ -1364,10 +1381,7 @@ fn verdicts( all.clone().map(|()| label) }), }; - let jar = |patch: &&Patch| { - (&patch.group, &patch.artifact, &patch.version) == (group, artifact, version) - }; - let held = hubs.block.iter().find(jar).map(|patch| patch.uuid.clone()); + let held = hubs.held(group, artifact, version).map(str::to_string); site.push((uuid.clone(), entry, held)); } site @@ -1673,26 +1687,23 @@ fn notices_blocking( let hubs = Hubs::read(&disk(&root.dir), &disk_up_to(&root.dir, MAX_LOCK)); let mut said = Vec::new(); for (purl, (group, artifact, version)) in jars { - let in_block = |patch: &Patch| { - (&*patch.group, &*patch.artifact, &*patch.version) == (&*group, &*artifact, &*version) - }; let (code, detail) = match hubs.built(&group, &artifact, &version) { Built::No => continue, // Bazel takes the jar of the block's entry. - Built::Yes(_) if hubs.block.iter().any(in_block) => continue, + Built::Yes(_) if hubs.held(&group, &artifact, &version).is_some() => continue, Built::Yes(by) => { let from = |(hub, _): &(&Hub, &Pinned)| { let (lock, name) = (hub.lock.as_deref().unwrap_or_default(), &hub.name); let (file, line) = (&hub.file, hub.line); - format!("{lock} (hub \"{name}\", `maven.install` at {file}:{line})") + format!("{lock} (hub \"{name}\", `{INSTALL}` at {file}:{line})") }; let from = by.iter().map(from).collect::>().join(" and "); let detail = format!( "Bazel takes this artifact from {from}, which pins the upstream jar, and \ this command does not patch a rules_jvm_external hub: the Bazel build \ - stays unpatched whatever it did for another build of the checkout. \ - `socket-patch get {purl} --mode hosted` at the Bazel root patches a \ - pinned hub" + stays unpatched whatever it did for another build of the checkout. {} \ + patches a pinned hub", + command(&group, &artifact, &version) ); (HUB_UNPATCHED, detail) } @@ -1711,6 +1722,99 @@ fn notices_blocking( said } +/// The command of hosted mode that patches a pinned hub, as a warning and +/// an action statement name it: by the coordinates of the jar, without +/// what a purl may carry after them (`?type=jar`), which a shell reads. +fn command(group: &str, artifact: &str, version: &str) -> String { + let purl = build_maven_purl(group, artifact, version); + format!("`socket-patch get {purl} --mode hosted` at the Bazel root") +} + +/// For each of `patches`, the purl of a Maven jar and the id of its patch, +/// whichever mode recorded it (the jar a build tool installed, a pin or a +/// vendored copy of a Maven or Gradle build): the pinned hub of the +/// workspace at the Bazel root `root` that fetches the plain jar of that +/// artifact at that version, the first of them, and what to do about it. +/// Bazel then takes the jar at the sha256 the lock of that hub pins: from +/// a repository, or under `m2local` from the Maven local repository, where +/// a patched jar is not one of that sha256. The lock's sha256 is not +/// compared with the patch: a lock that pins the patched jar's bytes is +/// read as any other. +/// +/// It is said only when it is certain of that hub, whatever is not known +/// of another one: the hub and its lock were read and no `maven.override` +/// names the artifact ([`Hubs::built`]), and the block holds no entry of +/// this patch for the jar. A block that was changed by hand and still +/// overrides a repository may hold one: the reader does not follow such +/// a call, so no hub is read beside it. One that overrides nothing holds +/// no entry, and no mode writes to it. `None` +/// otherwise, and where the checkout does not say that Bazel reads +/// `MODULE.bazel` as it stands: it names a Bazel before 7.0, or none +/// while a `WORKSPACE` file defines something, or an rc file names an +/// option that switches Bzlmod off or drops tags of the root (on whatever +/// line: it may hold for one `--config` alone), or cannot be read. `None` +/// too for what is no plain jar of a Maven artifact; without one among +/// `patches` nothing is read. +/// +/// What to do is the command that writes an entry for the jar at the +/// root, or why hosted and vendored mode write none there, with the code +/// and in the words of their warning ([`verdicts`]). The entry of another +/// patch is named first: Bazel takes that one's jar until the command has +/// put this one's in its place. +pub fn upstream(root: &Path, patches: &[(&str, &str)]) -> Vec> { + let jar = |purl| parse_maven_purl(purl).filter(|_| plain(purl)); + let jars: Vec<_> = patches.iter().map(|&(purl, _)| jar(purl)).collect(); + let none = || vec![None; patches.len()]; + if jars.iter().all(Option::is_none) { + return none(); + } + let (view, locks) = (disk(root), disk_up_to(root, MAX_LOCK)); + let hubs = Hubs::read(&view, &locks); + let release = declared_release(&view); + let read = release.map_or(!hubs.legacy, |release| release >= (7, 0, 0)); + let off = || rc(&view, release).iter().any(|(code, _)| *code != DOWNLOAD); + if hubs.pinned.is_empty() || !read || off() { + return none(); + } + // What holds for every jar is asked once, as for a run that writes. + let mut workspace = None; + let fetched = jars.into_iter().zip(patches).map(|(jar, (_, uuid))| { + let (group, artifact, version) = jar?; + let Built::Yes(by) = hubs.built(&group, &artifact, &version) else { + return None; + }; + let held = hubs.held(&group, &artifact, &version); + if held == Some(*uuid) { + return None; + } + let entry = match hubs.sure(&group, &artifact, &version) { + Built::Unknown(doubt, kind) => Err(unknown(doubt, kind)), + _ => hubs.label(&group, &artifact, &version).and_then(|_| { + let all = || settings(root, &view, &locks, &hubs.asked, true); + workspace.get_or_insert_with(all).clone() + }), + }; + let other = held.map_or_else(String::new, |held| { + format!( + "{FILE} holds the entry of patch {held} for this jar, and where the block is \ + in effect the Bazel build takes that patch's jar. " + ) + }); + let remedy = match entry { + Ok(()) => format!( + "{other}Run {} to patch the hub, or with `--mode vendored` to commit the \ + patched jar.", + command(&group, &artifact, &version) + ), + Err((code, why)) => format!( + "{other}Hosted and vendored mode write no entry for this hub (`{code}`): {why}." + ), + }; + Some((by[0].0.clone(), remedy)) + }); + fetched.collect() +} + #[cfg(test)] pub(crate) mod tests { use super::*; @@ -2545,6 +2649,157 @@ jvm.artifact(group = "com.example", artifact = "a", version = "1") assert_eq!(notices_blocking(broken.path(), Vec::new()), []); } + /// [`upstream`] names the hub of a patched jar only where that is + /// certain of the hub: a pinned hub that was read fetches the plain jar + /// at that version, the block was not changed by hand and holds no + /// entry of the patch, and nothing of the checkout says that Bazel may + /// pass over `MODULE.bazel`. With the hub it says what to do: the + /// command that writes the entry, or why no mode writes one. + #[test] + fn an_installed_jar_is_upstream_where_a_pinned_hub_surely_fetches_it() { + let uuid = "7a11e000-0000-4000-8000-0000000000b1"; + let [_, lock, patched] = sample(uuid); + // In a checkout where a run may write, with `files`: the hub of + // `purl`, patched by `uuid`, and what to do. + let said = |files: &[(&str, &str)], purl: &str| { + let dir = checkout(files); + let patches = [("pkg:npm/left-pad@1.3.0", uuid), (purl, uuid)]; + let mut found = upstream(dir.path(), &patches); + assert_eq!((found.len(), &found[0]), (2, &None), "{purl}"); + let (hub, remedy) = found.remove(1).unzip(); + let hub = hub.map(|hub: Hub| { + let lock = hub.lock.unwrap_or_default(); + format!("{} {}:{} {lock}", hub.name, hub.file, hub.line) + }); + (hub.unwrap_or_default(), remedy.unwrap_or_default()) + }; + let hub = "maven MODULE.bazel:4 maven_install.json"; + let run = format!( + "Run `socket-patch get {NETTY_PURL} --mode hosted` at the Bazel root to patch the \ + hub, or with `--mode vendored` to commit the patched jar." + ); + let fetched = (hub.to_string(), run.clone()); + let nothing = (String::new(), String::new()); + // The hub, where hosted and vendored mode write no entry for it: + // the code of their warning, and how its reason ends. + let declined = |files: &[(&str, &str)], code: &str, ends: &str| { + let (found, remedy) = said(files, NETTY_PURL); + let begins = + format!("Hosted and vendored mode write no entry for this hub (`{code}`): "); + let whole = remedy.starts_with(&begins) && remedy.ends_with(&format!("{ends}.")); + assert!(found == hub && whole, "{files:?}: {found:?} {remedy:?}"); + }; + assert_eq!(said(&[], NETTY_PURL), fetched); + // The command names the jar by its coordinates: nothing of what a + // purl carries after them, which a shell would read, is in it. + let qualified = format!("{NETTY_PURL}?type=jar&repository_url=x"); + assert_eq!(said(&[], &qualified), fetched); + // The warning of an agent run names the same command. + let warned = notices_blocking(checkout(&[]).path(), vec![qualified.clone()]); + let command = run.split(" to patch").next().expect("the command"); + let command = command.trim_start_matches("Run "); + let names = |(_, detail): &(_, String)| detail.contains(&format!("{command} patches")); + assert!(warned.len() == 1 && names(&warned[0]), "{warned:?}"); + // Not another version, a classifier or a packaging of the jar. + let others = [ + NETTY_PURL.replace("61", "62"), + format!("{NETTY_PURL}?classifier=sources"), + format!("{NETTY_PURL}?type=aar"), + ]; + for purl in others { + assert_eq!(said(&[], &purl), nothing, "{purl}"); + } + // Also a jar that no repository of the lock lists: under `m2local` + // the hub takes it from the Maven local repository, at that sha256, + // and no entry can take its place. + let local = lock.replace("\"repositories\"", "\"m2local\": true, \"mirrors\""); + let local = [("maven_install.json", local.as_str())]; + declined( + &local, + ARTIFACT, + "there is no repository for an entry to replace", + ); + // The block's entry of the patch is the jar Bazel takes, and a + // block that was changed by hand may hold one while it overrides + // a repository: what is left of one that overrides nothing holds + // none, and no mode writes to it. The entry of another + // patch is not this one's: it is named, and the command replaces + // it. An entry for another version is the entry of another jar. + fn module(text: &str) -> [(&str, &str); 1] { + [("MODULE.bazel", text)] + } + assert_eq!(said(&module(&patched), NETTY_PURL), nothing); + let by_hand = patched.replace("# Do not change", "# Do change"); + assert_eq!(said(&module(&by_hand), NETTY_PURL), nothing); + let emptied = format!("{PINNED}\n{BEGIN}\n{END}\n"); + let by_hand = "the block that socket-patch keeps there was changed by hand"; + declined(&module(&emptied), UNREADABLE, by_hand); + let another = patch("netty-tcnative-classes", OTHER_UUID); + let another = with_patch(PINNED, LABEL, another).expect("a block"); + let names = format!( + "MODULE.bazel holds the entry of patch {OTHER_UUID} for this jar, and where the \ + block is in effect the Bazel build takes that patch's jar. {run}" + ); + let replaced = (hub.to_string(), names); + assert_eq!(said(&module(&another), NETTY_PURL), replaced); + let mut earlier = patch("netty-tcnative-classes", uuid); + earlier.version = "2.0.60.Final".to_string(); + let earlier = with_patch(PINNED, LABEL, earlier).expect("a block"); + assert_eq!(said(&module(&earlier), NETTY_PURL), fetched); + // What is not known of another hub leaves this one as it is, and + // says why no entry is written: a hub without a lock, and a + // `maven_install` of a `WORKSPACE` file that Bazel 7 reads. An + // override of the jar is about this hub. + let lockless = "maven.artifact(name = 'more', artifact = 'a', group = 'g', version = '1')"; + let over = "maven.override(coordinates = 'io.netty:netty-tcnative-classes', target = ':a')"; + let [lockless, over] = [lockless, over].map(|more| format!("{PINNED}{more}\n")); + declined( + &module(&lockless), + NOT_PINNED, + "(`REPIN=1 bazel run @more//:pin`)", + ); + assert_eq!(said(&module(&over), NETTY_PURL), nothing); + let (bazel_6, bazel_7) = ((".bazelversion", "6.5.0\n"), (".bazelversion", "7.7.2\n")); + let bazel_9 = (".bazelversion", "9.3.2\n"); + let legacy = ("WORKSPACE", "maven_install(artifacts = [])\n"); + let not_read = "WORKSPACE:1: a `maven_install` of a WORKSPACE file is not read"; + declined(&[legacy, bazel_7], UNREADABLE, not_read); + assert_eq!(said(&[legacy, bazel_9], NETTY_PURL), fetched); + // Nothing where the checkout does not say that Bazel reads + // `MODULE.bazel`: it names a Bazel before 7.0, or none beside a + // `WORKSPACE` file that defines anything. + let other = ("WORKSPACE", "http_archive(name = 'zlib', urls = [])\n"); + assert_eq!(said(&[bazel_6], NETTY_PURL), nothing); + for file in [legacy, other] { + assert_eq!(said(&[file], NETTY_PURL), nothing, "{file:?}"); + } + assert_eq!(said(&[other, bazel_7], NETTY_PURL), fetched); + // Nor where an rc file names an option that switches Bzlmod off or + // drops tags of the root, on whatever line, or cannot be read: + // with it the hub, and an entry of the block, may be out of the + // build. An option about downloads leaves the hub as it is. + let unread = [ + (".bazelrc", "import %workspace%/ci.bazelrc\n"), + ("ci.bazelrc/a", ""), + ]; + let rc = |text: &'static str| [(".bazelrc", text), bazel_7]; + for files in [ + &rc("build:legacy --noenable_bzlmod\n")[..], + &rc("common --ignore_dev_dependency\n"), + &unread, + ] { + assert_eq!(said(files, NETTY_PURL), nothing, "{files:?}"); + } + let offline = rc("build --repository_disable_download\n"); + assert_eq!(said(&offline, NETTY_PURL), fetched); + // A Bazel without `override_repo`, and a hub on a usage that no + // entry reaches: the hub fetches the jar, and no mode patches it. + let old = [(".bazelversion", "7.3.0\n")]; + declined(&old, RELEASE, "which Bazel has from 7.4.0"); + let isolated = PINNED.replace("\"maven\")", "\"maven\", isolate = True)"); + declined(&module(&isolated), UNREADABLE, "reaches"); + } + /// What the warning for a hub that was not read says after where and /// why. const NOT_READ: &str = "so socket-patch does not know every Maven artifact that Bazel builds \ diff --git a/docs/ecosystems.md b/docs/ecosystems.md index fc0ed2637..22b6b43e2 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -2157,7 +2157,7 @@ workspace and that Bazel does not build keeps the matrix at the top. | Cargo: rules_rust `crate.from_cargo` over `Cargo.lock` | Supported with `cargo_config` on the hub | Supported | Not supported | Supported | | NuGet: rules_csharp `nuget.from_lock` over `packages.lock.json` | Supported with `nuget_config` on the tag | Not supported | Not supported | Supported; for a hosted patch, with no restored copy on the machine | | Composer: a rule of your own that runs `composer install` from `composer.lock` | Supported | Not supported | Not supported | Supported with a hosted patch | -| Maven: rules_jvm_external `maven.install` with a `lock_file` | Supported with Bazel 7.4.0 or later | Supported with Bazel 7.4.0 or later | Not supported | Supported with a hosted or vendored patch | +| Maven: rules_jvm_external `maven.install` with a `lock_file` | Supported with Bazel 7.4.0 or later | Supported with Bazel 7.4.0 or later | Not supported | Supported | These words are the table's own, not the [v5 support tiers](#v5-support-tiers). In particular "Not supported" does not mean that the command refuses: in several cells it runs, @@ -2178,14 +2178,15 @@ exits 0 and leaves a build that fails (the known problems below). What a cell sa [Bazel workspaces: VEX](#bazel-workspaces-vex) has the cases and the limits; a vendored gem is one of the limits. The NuGet cell names another: of a hosted patch `vex` writes no document on a machine whose NuGet folder still holds the unpatched package (the last list - below). + below). For Maven, + [Bazel workspaces: Maven](#bazel-workspaces-maven) says what `vex` checks of the hub before + it attests an entry, and that it says `affected` of a patch that only a Maven or Gradle + build holds while a pinned hub fetches the jar. - **Supported with a condition**, for VEX: under the condition the cell names, the document says `not_affected` of a package the Bazel build takes patched, and a test holds it to - that (for a hosted Maven patch a test without Bazel). It does not say `affected` of a + that. It does not say `affected` of a package whose patch is in no build of Bazel's, one that is only in an installed copy, say: - of that package it still says `not_affected`. Do not publish that document. For Maven, - [Bazel workspaces: Maven](#bazel-workspaces-maven) says what `vex` checks of the hub before - it attests an entry. + of that package it still says `not_affected`. Do not publish that document. **Agent mode is not supported for Bazel builds, in any ecosystem.** Agent mode is made for the copies a package manager installed: `socket-patch apply` patches them after each install. @@ -2783,6 +2784,21 @@ artifact: - Nothing is said for a jar the block patches, under `--global`, and by a `vendor` whose `--ecosystems` leaves Maven out. +`socket-patch vex` and `--vex` say `affected` of an artifact whose patch the hub does not +take, whichever mode holds it: an installed jar that agent mode patched, a pin of a +`pom.xml` or a Gradle file, a copy vendored for such a build, while a pinned hub fetches +the jar at that version and the block holds no entry of that patch for it. The statement +names the lock, the `maven.install` and its line, and the command of hosted and of vendored +mode that patches the hub or, where those modes write no entry, the code of the table above +with its reason ([Bazel workspaces: VEX](#bazel-workspaces-vex)). So the document after a +command that gave such a code still says `affected`. It is about the hub that fetches the +jar, also beside another hub that was not read, where the first warning above is given. +It stays `not_affected`, as without Bazel, where that hub is in doubt (an override of the +artifact, a block that was changed by hand and still overrides a repository) and where the +checkout does not say that Bazel reads `MODULE.bazel` as it stands (a Bazel before 7.0, a +`WORKSPACE` file beside no named release, an rc file that names `enable_bzlmod` or +`--ignore_dev_dependency`). + The Bazel release is the one that `.bazeliskrc` (`USE_BAZEL_VERSION`) or else `.bazelversion` at the Bazel root names, read as bazelisk reads them: the last `USE_BAZEL_VERSION` line counts, without the blanks around the name and the value, and an empty one leaves it to the first line @@ -2836,8 +2852,10 @@ Limits: that only Bazel builds that is `package_not_installed`. A vendored `get` of a jar that no hub builds there fails with `vendor_jvm_shape_unsupported`, as before. So does a `vendor` that is refused before it vendors anything (a ledger that cannot be read, or one that git ignores). -- `socket-patch vex` looks at the hub only for the block's entries: a patch that only the - `pom.xml` pin or a patched jar attests is attested, although the Bazel build does not have it. +- `socket-patch vex` holds every Maven patch to the hubs (above), and a `maven_install.json` + names no project. So a project of the workspace that Maven or Gradle builds and Bazel + does not gets `affected` too, beside a hub that pins the same artifact at the same + version. `--no-bazel` is for the repository that ships what Maven or Gradle builds. ## Bazel workspaces: Cargo @@ -4279,7 +4297,7 @@ What is read, and what is not: `vex` and `--vex` read what socket-patch wrote and what the project's own package manager installed. In a Bazel workspace (the Bazel root of the section on `.bazelignore` above) they -also read `MODULE.bazel` and the files it includes, for the calls of five rulesets that hand +also read `MODULE.bazel` and the files it includes, for the calls of six rulesets that hand a lock to Bazel: | Call | Files it names | @@ -4289,11 +4307,15 @@ a lock to Bazel: | rules_ruby `ruby.bundle_fetch` | `gemfile_lock`, `gemfile` | | rules_rust `crate.from_cargo` | `cargo_lockfile` | | rules_csharp `nuget.from_lock` | `packages_lock_json` | +| rules_jvm_external `maven.install` | `lock_file` | Each of them builds a package from what those files name, in a repository of Bazel's own, and never from `node_modules`, a virtualenv, a gem home, a cargo home or NuGet's packages -folder. So two kinds of patch are in no build of Bazel's, and the document says of their -package that it is `affected`, not that it is patched: +folder. A rules_jvm_external hub takes a jar from the Maven local repository only when its +lock has `m2local` and lists the jar under no repository, and then at the lock's sha256 +too, so not the jar as agent mode patched it. So three kinds of patch are in no build of +Bazel's, and the document says of their package that it is `affected`, not that it is +patched: - **A patch in the installed copy** (agent mode) of an npm, PyPI, RubyGems, Cargo or NuGet package, when such a call names the project's own lock and that lock lists the package at @@ -4313,6 +4335,42 @@ package that it is `affected`, not that it is patched: 0.56.0 and for rules_csharp 0.5.0: the build prints the unpatched package before `scan --mode agent` and after it; for rules_rust and rules_csharp also on a second machine that has built nothing and has the patched cargo home or NuGet folder. +- **A patch of a Maven artifact, in any mode**, when a pinned hub (the `maven.install` tags + of one name, with a `lock_file`) fetches the plain jar of that artifact at that version, + and the block that hosted and vendored mode keep in the root `MODULE.bazel` holds no + entry of that patch for the jar. The patch is then in an installed jar (agent mode), in a + pin of a `pom.xml` or a Gradle file (hosted mode) or in a copy vendored for such a build, + and in none of them for Bazel: the lock of a hub is no project's own, and no `pom.xml` or + Gradle file decides what a hub fetches. So the statement is the same from every project + of the workspace, whichever build tool installed the jar and whichever mode holds the + patch, and it is still the statement after a hosted or vendored command that wrote no + entry for the hub (the `maven_bazel_*` warnings). + The hubs are read as [Bazel workspaces: Maven](#bazel-workspaces-maven) says, and the + statement is about the hub that fetches the jar, whatever is not known of another one: + beside a second hub without a `lock_file`, another hub's lock that cannot be read or a + `maven_install` of a `WORKSPACE` file, it is `affected`, as `apply` warns + `maven_bazel_hub_unpatched` there. It stays `not_affected` where that hub itself is in + doubt: its tags or its lock cannot be read, a `maven.override` names the artifact, or the + block was changed by hand and still overrides a repository, so that it may hold an entry + (what is left of a block that overrides nothing holds none: the statement is `affected`, + and says that no mode writes to that block). It stays so too where the checkout does + not say that Bazel reads `MODULE.bazel` as it stands: `.bazeliskrc` or `.bazelversion` + names a Bazel before 7.0; neither names a release while a `WORKSPACE` file defines a + repository; or an rc file of the checkout names `enable_bzlmod` other than switched on + (while the checkout names no Bazel 9 or later) or `--ignore_dev_dependency` without + switching it off, or cannot be read. Such an option counts on whatever line, so it may + hold for one `--config` alone, and where it holds the hub, like an entry of the block, + may be out of the build: neither is certain. + The entry of another patch for the same jar is not this patch's: the statement is + `affected`, and names that entry. + A jar that a hub takes from the Maven local repository under `m2local` counts as fetched: + the hub holds that file to the lock's sha256, which the patched jar does not have (read + from the ruleset's source at 7.2 and 6.5; no test builds it). The sha256 of the lock is + not compared with the patch: a lock that pins the very bytes of the patched jar would + still read as `affected`. + Measured on Bazel 9.3.2 and 7.7.2 for rules_jvm_external 7.2 and 6.5, with a lock that + lists the jar under a repository: the build prints the upstream jar before + `scan --mode agent` and after it. - **A PyPI patch committed under `.socket/vendor`** (vendored mode), when such a call names the file that points at the copy. rules_python 2.5.0-rc4 takes the path of the line for the name of a project, and the build of everything the hub serves fails (same three @@ -4362,6 +4420,48 @@ A test without Bazel runs each of those scans after agent mode, and the document `not_affected`; the patched build after a hosted or a vendored scan is tested on real Bazel from a checkout that agent mode has not touched. +For a Maven artifact the statement is one for every mode, so it says where the patch is and +what a repository beside the hub ships in words that hold for each: + +``` +Bazel builds this package from maven_install.json (`maven.install`, MODULE.bazel:22), which does not name Socket patch 7a11e000-...: the patch is only in what Maven or Gradle builds from. Run `socket-patch get pkg:maven//@ --mode hosted` at the Bazel root to patch the hub, or with `--mode vendored` to commit the patched jar. If what you ship is what Maven or Gradle builds and not what Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor). +``` + +The command is the one of the two modes that patch a pinned hub. It runs at the Bazel root, +where the patch of a hub is written, and it names the package by its group, artifact and +version alone: nothing of what a purl can carry after them (`?type=jar`), which a shell +would read. + +That command writes its entry only where it can show from the checkout that the entry +works: Bazel 7.4.0 or later, a known release of the ruleset, a jar that a repository of the +lock lists, a hub on a `use_extension` that is not isolated, no hub of the workspace that +was not read (the table of codes in [Bazel workspaces: Maven](#bazel-workspaces-maven)). +The statement is written by the same reader, so where the command would decline it names no +command. It says why, with the code and the reason of the warning that the command would +give: + +``` +Hosted and vendored mode write no entry for this hub (`maven_bazel_release`): the checkout names Bazel 7.3.0 (`.bazeliskrc` or `.bazelversion`), and an entry needs `override_repo`, which Bazel has from 7.4.0. +``` + +The reasons are those of vendored mode. Where only the hosted command declines, because an +rc file names an option about downloads (`maven_bazel_download`), the statement names the +command, which writes the entry with `--mode vendored`. When the block holds the entry of +another patch for the jar, the statement says so first, in the words of that warning +("MODULE.bazel holds the entry of patch for this jar, and where the block is in effect +the Bazel build takes that patch's jar"); the command puts its entry in that one's place. + +A command that declines still pins or vendors the patch for a `pom.xml` or a Gradle build +beside the hub, and exits 0. The next document says `affected` all the same: the patch is +judged against the hub whichever mode holds it. + +A test without Bazel runs each of the two after agent mode. Where the command writes its +entry it gives no such warning, and `vex` then says `not_affected`. In a checkout that names +Bazel 7.3.0, and beside a second hub without a lock, the documents of agent mode name the +code and the reason in the place of the command, the command gives that warning, and `vex` +says `affected` after it in the same words. The hosted command is run on real Bazel after +agent mode as well: the build prints the patched jar. + A run tells a person the same in a warning (`vex_bazel_not_in_build`, one per package): ``` @@ -4386,14 +4486,17 @@ lock. A repository that ships what its package manager installed (a container im with `pnpm install` and `socket-patch apply`, say) and keeps the hub for something else, such as its tests, says so with `--no-bazel` on `vex`, with `--vex-no-bazel` on `scan`, `apply` and `vendor`, or with `SOCKET_VEX_NO_BAZEL=1` in the environment. The Bazel workspace is then not -read: the document describes the installed tree, and it, the warnings, the exit code and -every line of the run are those of the same checkout without its Bazel files. The option -changes nothing outside a Bazel workspace. +read for the document: it describes the installed tree, and it, the warnings about it, the +exit code and every line that `vex` prints are those of the same checkout without its Bazel +files. The option is about the document alone. What `scan`, `apply` and `vendor` themselves +do and say in a Bazel workspace stays: an agent run beside a pinned rules_jvm_external hub +still warns `maven_bazel_hub_unpatched`. The option changes nothing outside a Bazel +workspace. Nothing changes where the files do not show such a call: outside a Bazel workspace, in a -`--global` run, for a hosted pin, for a project whose own lock no call names, and wherever -the workspace is not read with certainty: the statement is then `not_affected`, as without -Bazel. A lock elsewhere that lists the same package is the +`--global` run, for a hosted pin (but a Maven one, above), for a project whose own lock no +call names, and wherever the workspace is not read with certainty: the statement is then +`not_affected`, as without Bazel. A lock elsewhere that lists the same package is the lock of another project and decides nothing for this one: in a directory beside the project, below it (but for a `packages.lock.json`, see above), or above it without naming it among the importers of a pnpm workspace (a pnpm app @@ -4417,6 +4520,22 @@ is not read at all. In each of these the document is the one of a project without Bazel. Flags outside the committed files (`--override_repository`, an rc file) and BUILD files are not read either. +A Maven artifact differs in one of these, the `WORKSPACE` file. Its hubs are read as +[Bazel workspaces: Maven](#bazel-workspaces-maven) says, by the reader hosted mode writes +its entries by. While `.bazeliskrc` or `.bazelversion` names a Bazel release from 7.0 on, +such a file changes nothing for a jar that a pinned hub of `MODULE.bazel` fetches: that +Bazel reads `MODULE.bazel` whatever the file holds, and a `maven_install` there is another +hub, which no mode patches either. That is so for a `maven_install` that the file calls +through a function of a `.bzl` file as well, which is not seen at all: it cannot put the +patch into the hub that was read. Where only such a hub fetches the jar, nothing is said. +While neither file names a release (also `latest`, a range, a release candidate), a +`WORKSPACE` file that defines a repository leaves the statement as without Bazel, as for +the other ecosystems: an older Bazel builds from that file alone. In the other cases the +document of a Maven artifact is the one of a project without Bazel as well (a lock of a +hub is read up to 16 MiB): when no pinned hub that was read fetches the jar at that +version, when the block holds the entry of the patch, and in the doubts that the list of +the artifact above names. + Three more things change no statement. A call counts only when it is a statement of its own, which Bazel runs once: one in a comprehension, behind an `and`, in a conditional or in the arguments of another call may never run. Of a pnpm lock the project documents are read: a @@ -4424,6 +4543,12 @@ package that only the document pnpm 11 keeps for its own configuration lists is rules_js links. And a patch whose record names no advisory gets no statement from any run, so it is not looked at here: a run over such a patch alone ends as it does without Bazel. +Only a patch that the run found in place is looked at. Where the installed copy on the +machine is missing or is not the patched one, `vex` ends as it does without Bazel: no +statement for that package, and exit 1 (`no_applicable_patches`) when it was the only one, +although the hub builds the upstream package all the same. `vex --no-verify`, which attests +the record, says `affected` there. Measured by hand, without Bazel, for a Maven jar. + The document and the build are tested to agree on real Bazel 9.3.2, 8.8.2 and 7.7.2 for four shapes, all under Bzlmod: Go through gazelle's `go_deps.from_file(go_mod = "//:go.mod")` with @@ -4453,6 +4578,14 @@ NuGet a flow of agent mode holds the documents to the unpatched build as well: t [Bazel workspaces: what is supported](#bazel-workspaces-what-is-supported) says what the document does not show. +For Maven, through rules_jvm_external's `maven.install` with a `lock_file`, a flow of agent +mode beside a `pom.xml` holds the documents to the unpatched build on Bazel 9.3.2 and 7.7.2: +the run (`--vex`), `apply --vex`, `vex` and `vex --no-verify` each write one `affected` +statement. After the hosted command the build is the patched one, and `vex` writes one +`not_affected` statement, as it does after the vendored command in a checkout that only +Bazel builds. `vex --no-bazel`, and the `affected` statement of a hosted pin and of a +vendored copy whose command wrote no entry for the hub, are tested without Bazel. + For no other Bazel shape do these pages say that the document and the build agree, and in these the document is still wrong (`not_affected` while Bazel builds the unpatched package, or while the build is broken): @@ -4473,6 +4606,13 @@ the build is broken): file at the root defines something, or the workspace has no `MODULE.bazel`. Measured for rules_js, rules_rust and rules_csharp, which build from the lock and never read the patched copy. +- **A Maven patch beside rules_jvm_external**, in any mode, wherever the statement does not + say `affected` above: the hub that fetches the jar is not read with certainty; the + checkout does not say that Bazel reads `MODULE.bazel` as it stands (an rc file that names + `enable_bzlmod` or `--ignore_dev_dependency`, a `WORKSPACE` file beside no named + release); or the jar reaches Bazel in a way that + [Bazel workspaces: Maven](#bazel-workspaces-maven) lists as not seen (a hub of another + Bazel module, a `maven_install` inside a function of a `.bzl` file). - **Vendored mode for PyPI**, where the workspace is not read with certainty: a `WORKSPACE` file that defines something, no `MODULE.bazel`, a call that is not a statement of its own, a file that is not given as a plain string label. And where the line that diff --git a/scripts/loc-budget.json b/scripts/loc-budget.json index ea94cfb34..305019eaa 100644 --- a/scripts/loc-budget.json +++ b/scripts/loc-budget.json @@ -4,7 +4,7 @@ "paths": [ "crates/socket-patch-core/src/bazel/" ], - "max": 4006 + "max": 4084 }, "lock-readers": { "paths": [