diff --git a/.github/workflows/bazel-composer.yml b/.github/workflows/bazel-composer.yml index b45a25a7f..68e8bb7d2 100644 --- a/.github/workflows/bazel-composer.yml +++ b/.github/workflows/bazel-composer.yml @@ -3,7 +3,8 @@ name: Bazel patch compatibility (Composer) # The Composer flows of the real-Bazel tests (`composer::` in # `e2e_bazel_build`), on each Bazel release below: hosted mode where a # repository rule runs `composer install` from the committed lock, hosted -# mode where a genrule does, and agent mode on a committed `vendor/` tree. +# mode where a genrule does, agent mode on a committed `vendor/` tree, and +# vendored mode where the repository rule installs and on the committed tree. # No Bazel ruleset builds PHP or takes Composer packages, so the three # shapes are the fixture's own, as a PHP project under Bazel writes them by # hand. bazel-compatibility.yml holds the legs and what every ecosystem @@ -46,13 +47,15 @@ on: # with `composer` in its name. The flows run through most of them: the # lock's model and its hosted rewrite (formats/composer), the crawler # that finds an installed package, the lock inventory, the discovery of - # a hosted pin for `vex`, the upstream restore of `rollback`, and the - # hints a command prints. The vendored backend (vendor/composer_lock) - # matches the patterns too, and no flow runs vendored mode. One more - # file is here: patch/user_patches.rs, the inspection of a project's own - # patches, whose Composer part every scan of a Composer project runs. - # That file also holds the npm, Cargo and Go parts, so a change to - # those starts the Composer legs too. + # a hosted pin for `vex`, the upstream restore of `rollback`, the hints + # a command prints, and the vendored backend (vendor/composer_lock), with + # the refusal of vendored mode in a Bazel workspace. Four more are here. + # patch/user_patches.rs is the inspection of a project's own patches, + # whose Composer part every scan of a Composer project runs; it also + # holds the npm, Cargo and Go parts, so a change to those starts the + # Composer legs too. bazel/mod.rs finds the Bazel root that refusal asks + # for, on the walk of utils/repo_root.rs, and vendor/refusals.rs hands + # the refusal to every command. # # What it does not cover: shared engine code (patch/redirect/mod.rs, # hosted/, the apply and rollback engines of agent mode, vex/, @@ -72,6 +75,9 @@ on: - 'crates/*/src/**/*composer*' - 'crates/*/src/**/*composer*/**' - 'crates/socket-patch-core/src/patch/user_patches.rs' + - 'crates/socket-patch-core/src/bazel/mod.rs' + - 'crates/socket-patch-core/src/utils/repo_root.rs' + - 'crates/socket-patch-core/src/vendor/refusals.rs' push: branches: [main] # The pull request paths, and what only a push to main covers: the @@ -91,6 +97,9 @@ on: - 'crates/*/src/**/*composer*' - 'crates/*/src/**/*composer*/**' - 'crates/socket-patch-core/src/patch/user_patches.rs' + - 'crates/socket-patch-core/src/bazel/mod.rs' + - 'crates/socket-patch-core/src/utils/repo_root.rs' + - 'crates/socket-patch-core/src/vendor/refusals.rs' - 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs' - 'crates/socket-patch-core/src/patch/redirect/**' - 'crates/socket-patch-core/src/patch/apply.rs' diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index a19a36417..35e78eefa 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -121,6 +121,7 @@ Every subcommand accepts the same set of "global" flags via a single shared `Glo | `--no-vlt-install-cleanup` | — | `SOCKET_NO_VLT_INSTALL_CLEANUP` | `false` | bool | Opt out of hosted mode's warm-tree heal for vlt: stale installed copies (`node_modules/.vlt-lock.json` and the stale `node_modules/.vlt/` entries) are left in place after `vlt-lock.json` is repointed (`scan`/`get --mode hosted`) or restored (`rollback`/`remove`), and the `redirect_vlt_reinstall_required` advisory tells you to run `vlt ci` instead. Stale copies of optional dependencies are always left in place (see `redirect_vlt_reinstall_required`). Other subcommands accept it silently | | `--no-gem-install-refresh` | — | `SOCKET_NO_GEM_INSTALL_REFRESH` | `false` | bool | Opt out of hosted mode's gem stale-install refresh: the gems Bundler already holds for the project (an installed gem under the project and its cached `.gem`, an archive in `vendor/cache`) are left as they are after `Gemfile.lock` is repointed (`scan`/`get --mode hosted`) or restored (`rollback`/`remove`, of a hosted pin or of a vendored gem; `vendor --revert`), and the `redirect_gem_stale_install` / `upstream_gem_stale_install` warning names the files to remove by hand instead (see the "Gem stale-install guard" section). Other subcommands accept it silently | | `--gem-refresh-shared` | — | `SOCKET_GEM_REFRESH_SHARED` | `false` | bool | Extend the gem stale-install refresh to gem homes OUTSIDE the project (the system gems, `~/.gem`, an rbenv or asdf home) and to project bundle or cache paths that resolve outside the project directory. Off by default: another project on the machine may pin the other build of the same gem version, and its `bundle pristine` then fails on the replaced archive's checksum (see the "Gem stale-install guard" section). Other subcommands accept it silently | +| `--allow-vendored-in-bazel` | — | `SOCKET_ALLOW_VENDORED_IN_BAZEL` | `false` | bool | Let vendored mode write for a package although the project is in a Bazel workspace, where the build may not find the vendored copy. Vendored mode declines such a package by default; the one refusal it lifts today is `vendor_composer_bazel_workspace`: the lock then names the patched copy by a path relative to the directory Composer runs in, and a Bazel rule that runs `composer install` anywhere else fails on it. The switch is not specific to Composer: any refusal of vendored mode that rests on the Bazel workspace reads this one. For a project whose build does find the copy. Read by `vendor`, `scan --mode vendored` and `get --mode vendored`; other subcommands accept it silently. See docs/ecosystems.md "Bazel workspaces: Composer" | **`--download-mode` removed (v5.0, MAJOR).** The global `--download-mode` flag and its `SOCKET_DOWNLOAD_MODE` binding are gone, with no alias: passing the flag is a usage error (exit 2) and the env var is ignored. Patch content is always fetched as per-file blobs (`.socket/blobs/`); `.socket/diffs/` archives are obsolete, never read, and removed by the cleanup sweeps. @@ -1669,7 +1670,7 @@ to **six flavors**. | npm / vlt (`vlt-lock.json`, lockfileVersion 0 or 1 — A0 locks without a version and every other version refuse `vendor_lockfile_version_unsupported`; flavor `vlt`) | patched package **directory** `.socket/vendor/npm//[@scope/]-/node_modules//` (the extra `node_modules/` level lets a package `require()` its own name), its `package.json` without `devDependencies`, plus `/.gitignore` (re-includes the payload against the project's ignores, ignores vlt's links inside it) and `/.gitattributes` (the same `* -text -filter -ident -working-tree-encoding` as the tarball flavors) | direct dependencies of the root or a workspace member only: the lock node becomes a `file` node for the directory, its importer edges and outgoing edges are re-keyed, and each importer's `package.json` spec becomes `file:`; every moved entry lands where vlt's serializer puts it. A node whose only extra is one peer context (`ṗ:N`, `peer.N`, `peer.<16 hex>`: from vlt 1.0.8 a root dependency with resolved peers, from rc.15 a workspace member's) becomes a `file` node without the extra, as vlt writes `file:` dependencies, keeping its peer edges; revert restores the extra-bearing DepID. Refused before any write: transitive targets (`vendor_vlt_transitive_unsupported`), two or more instances of one `name@version` or a modifier extra, importer `peer` edges, foreign registries, a git, remote-tarball or local-directory node of the same package name (vlt records no version for it), a package `vlt build` would build in place (`vendor_vlt_build_scripts_unsupported`), a name declared in several dependency fields (`vendor_lock_entry_unsupported`), a spec that disagrees with the lock (`vendor_vlt_lock_out_of_sync`), a payload git would ignore (`vendor_artifact_gitignored`), a purl vendored under another flavor (`vendor_flavor_changed`); era-A locks warn `vendor_vlt_legacy_lockfile`; an optional dependency (or any dependency node_modules still links to its installed upstream copy) gets `vendor_vlt_reinstall_required` | fresh checkout, `vlt ci` with cold caches: the patched bytes load and `vlt-lock.json` stays byte-identical, also through a warm and a cold `vlt install --frozen-lockfile` (checked on 1.2.0, 1.0.10, 1.0.4, 1.0.0-rc.32 and 1.0.0-rc.14, and on every release by `docs/testing/vlt-compatibility.md`); no-op installs, `vlt install `, `uninstall` and `vlt update` keep the direct dependency vendored. `vendor --revert` restores the registry node, edges and specs, keeping what vlt re-laid since, and refuses on drift | | cargo | crate dir `-/` (no `.cargo-checksum.json`) | (v5.0) `[patch.crates-io]` path entry in the **workspace-root `Cargo.toml`** (the manifest beside the `Cargo.lock` it detaches — never `.cargo/config*`) **+** Cargo.lock surgery (the `[[package]]` entry's `source`/`checksum` removed and its `version` set to the copy's TAGGED version `+socket.` — `+.socket.` when the version already has build metadata — with every lock reference that spells the old version rewritten, formats v1–v4; the copy's own `Cargo.toml` version carries the same tag, so the patched crate sees it in `CARGO_PKG_VERSION`; revert restores the lock byte for byte). Key: always the Socket-owned `-socket-` with `package = ""` (the full uuid hex when that key is taken), never the bare crate name — cargo lets a config-file `[patch]` item (project, ancestor directory or `$CARGO_HOME`) replace the manifest item with the same key whatever its version, so keys any of those configs use are avoided and a re-run moves an entry off a now-shadowed key; two versions of one crate are wired side by side. Pre-v5 wiring in `.cargo/config.toml` / `.cargo/config` is moved into `Cargo.toml` by a re-run (`vendor`, `scan`/`get --mode vendored`) or `repair` (`cargo_wiring_migrated` note; the ledger's `cargo_patch_entry` record then names `Cargo.toml`); a detached lock entry left unwired by the pre-v5 multi-version overwrite is re-wired the same way (`cargo_wiring_restored`); every revert removes both spellings | `cargo build --locked --offline` on a fresh checkout — single-version manifest `[patch]` also builds with no network on cargo older than 1.56 (the old config-file wiring's floor); two vendored versions of ONE crate need cargo 1.45 or newer (`--offline` from an empty CARGO_HOME is enough there); older cargo fails closed whatever the index state, and a project that does not pin cargo ≥ 1.45 (`rust-version`, a `rust-version.workspace = true` inheritor's `[workspace.package]` value, or toolchain file) gets the `cargo_multi_version_old_cargo` warning. Note: path deps build **without** `--cap-lints allow` | | golang | module dir `@/` | `go.mod` `replace => ./.socket/vendor/golang//@` | `go build` with `GOPROXY=off` + empty `GOMODCACHE` (directory replaces bypass go.sum entirely; survives `go mod tidy`) | -| composer | package dir `/@/`; the copy's `.gitignore` / `.hgignore` are emptied and its `.gitattributes` `export-ignore` rules dropped, because Composer's path mirror skips the files they match (`vendor_composer_mirror_filters_neutralized`; a patch that rewrites one of them is refused `vendor_composer_mirror_filter_conflict`). Re-runs heal copies vendored before this | `composer.lock` only: entry's `dist` → `{type: "path", url, reference: ""}`, `source` removed, `transport-options: {symlink: false}` added. `content-hash` unaffected; `composer.json` untouched | `composer install` (from the lock alone, real copy not symlink, works under `--network none`). Composer 1 does not reinstall an already-installed package whose dist changed: remove `//` first (the hint names the project's resolved vendor dir: `COMPOSER_VENDOR_DIR`, else `config.vendor-dir`, else `vendor`). `composer update ` reverts it. See `docs/testing/composer-compatibility.md` | +| composer | package dir `/@/`; the copy's `.gitignore` / `.hgignore` are emptied and its `.gitattributes` `export-ignore` rules dropped, because Composer's path mirror skips the files they match (`vendor_composer_mirror_filters_neutralized`; a patch that rewrites one of them is refused `vendor_composer_mirror_filter_conflict`). Re-runs heal copies vendored before this | `composer.lock` only: entry's `dist` → `{type: "path", url, reference: ""}`, `source` removed, `transport-options: {symlink: false}` added. `content-hash` unaffected; `composer.json` untouched | `composer install` (from the lock alone, real copy not symlink, works under `--network none`). Composer 1 does not reinstall an already-installed package whose dist changed: remove `//` first (the hint names the project's resolved vendor dir: `COMPOSER_VENDOR_DIR`, else `config.vendor-dir`, else `vendor`). `composer update ` reverts it. See `docs/testing/composer-compatibility.md`. In a Bazel workspace the project is refused `vendor_composer_bazel_workspace` unless the run has `--allow-vendored-in-bazel` | | gem | gem dir `-/` + gemspec materialized from `specifications/`; for a gem locked as platform gems, one unpacked prebuilt platform gem per locked platform, `-//`, each with its gemspec, plus `/.gitignore` (`!*`: re-includes the prebuilt binaries against the project's ignores, such as a `*.so` or `*.bundle` rule) and the same `/.gitattributes` the npm tarballs get (so a project filter such as a Git LFS `*.so` rule does not change the committed bytes); an in-sync re-run and `repair` put the pair back when it is missing. A `.gitignore` a prebuilt platform gem ships inside its own tree would hide files below the uuid dir's `!*`, so each is emptied in the copy (`vendor_gem_gitignore_neutralized`; the ledger's inventory is of the copy as vendored; one the patch itself rewrites is kept). Refuses `vendor_artifact_gitignored` before any download, dry run included, when a rule ignores the uuid directory itself, and after the download when git would still drop a file | **Gemfile + Gemfile.lock pair**: the `gem` line gains `path:` (or a managed block for transitive deps, whose declaration is `require: false` so `Bundler.require` does not start loading the gem at boot; a block that 5.1 or earlier wrote without the option gains it when a patch update re-vendors the gem); the lock's spec block moves GEM→PATH and the DEPENDENCIES entry becomes ` (= )!`, in bundler's exact canonical form. A gem the lock pins as precompiled platform gems (one ` (-)` entry per platform and no plain ` ()`, as Bundler locks nokogiri) cannot be vendored as its own tree, because Bundler never builds a `path:` gem's extensions. It is vendored as the patch's prebuilt platform gems: the `path:` and the `PATH` `remote:` still name `-/`, which holds one unpacked tree per locked platform; EVERY platform block of the gem moves into the one `PATH` section; and Bundler loads the tree of the platform it runs on, with no build. Every locked platform needs a prebuild: while the patch service lists none for one of them the gem is skipped (`vendor_prebuilt_platform_pending`, no failure) with nothing written. A plain ` ()` entry beside the platform ones (the `ruby` platform, which no prebuilt tree serves), or a lock whose `BUNDLED WITH` is absent or older than Bundler 2.3 (the oldest the layout is verified on), is refused `platform_gem_unsupported` with nothing written. `vendor --check`, `vex` and `repair` cover every platform's tree with one rule, which a re-run's in-sync test applies too: a patched file the tree holds is the patched one; a patched file it lacks is no mismatch, even when it holds none of them (a platform gem need not ship what its extension was built from); and every file of the tree matches the ledger's file inventory, which is what vouches for such a tree once it is vendored (a file that is gone, changed or added is a mismatch, and the next run rebuilds the copy). Only an entry with no inventory needs a patched file in the tree: without one nothing vouches for it. On bundler 2.7+ (the lock's `BUNDLED WITH`), a `gem` line that follows a `gemspec` directive whose gemspec declares a development dependency on the same gem is refused `gemfile_declaration_not_editable` before any write: bundler merges the two requirements and then rejects even its own lock for a path gem under frozen mode, so the refusal names the remedy (declare the gem above `gemspec`, or use hosted mode). Hosted mode instead pins the merged requirement bundler writes: the gemspec's requirements plus `= `, in bundler's order (` (>= 0, = )!` for an unconstrained development dependency), and warns `redirect_gem_gemspec_dependency_unreadable` when the gemspec cannot be read (#985) | `bundle install` (normal **and** `BUNDLE_FROZEN=true`), byte-stable lock. Lock-only edits are a silent unpatch — hence the mandatory pair | | pypi / uv (uv.lock) | rebuilt wheel (canonical PEP 427 filename; RECORD regenerated) | `[tool.uv.sources] = {path}` in pyproject + surgical uv.lock rewrite; transitive deps via `[tool.uv] override-dependencies` | `uv sync --locked` / `--frozen --offline` (hash-verified, byte-stable lock) | | pypi / poetry (poetry.lock: legacy `[metadata.hashes]`, lock 1.0/1.1 `[metadata.files]`, 2.x `files`) | (rebuilt wheel) | lock-only: the target `[[package]]` gets `[package.source] type="file"` (+ `reference = ""` on the 0.12/1.0 layouts, which read it unconditionally) and the single `{file, hash: sha256-of-our-wheel}` entry in whichever table the generation keeps it. pyproject + `metadata.content-hash` untouched; CRLF locks keep their line endings. A lock written by Poetry < 1.4 emits `pypi_poetry_integrity_unverified` (that installer verifies no local hashes and skips an already-installed version) | `poetry check --lock && poetry sync`, cold cache (hash fail-closed from Poetry 1.4; byte-stable lock) — see `docs/testing/poetry-compatibility.md` | @@ -2166,6 +2167,7 @@ Empty string means unset at every layer: exported-but-empty flag-bound vars are | `SOCKET_NO_VLT_INSTALL_CLEANUP` | `--no-vlt-install-cleanup` | `false` | Hosted mode, `rollback`, `remove`: keep stale vlt installed copies. | | `SOCKET_NO_GEM_INSTALL_REFRESH` | `--no-gem-install-refresh` | `false` | Hosted mode, `rollback`, `remove`: leave the project's stale installed gems and cached archives alone. | | `SOCKET_GEM_REFRESH_SHARED` | `--gem-refresh-shared` | `false` | Hosted mode, `rollback`, `remove`: also refresh stale gems in gem homes outside the project. | +| `SOCKET_ALLOW_VENDORED_IN_BAZEL` | `--allow-vendored-in-bazel` | `false` | Vendored mode: write for a package although the project is in a Bazel workspace, where the build may not find the vendored copy. | | `SOCKET_PATCH_VERSION` | `--update ` | (latest) | Local to `--update`; the same pin `install.sh` honors. | | `SOCKET_BATCH_SIZE` | `scan --batch-size` | `500` authenticated / `100` proxy | Local to `scan`. | | `SOCKET_MAX_NEW_PATCHES` | `scan --max-new-patches` | (unlimited) | Local to `scan` (v5.0): a count or `none`; empty is unset, malformed exits 2. | @@ -2469,6 +2471,7 @@ command's integration tests check its output. | `vendor_yarn_berry_unsupported` | `failed` | vendor (npm): yarn-berry Plug'n'Play layout; use its native `yarn patch` workflow. | | `vendor_bun_lockb_invalid` | `failed` | vendor / scan / get `--mode vendored`: the binary lock is malformed, unreadable, unsupported or cannot be rewritten safely. The detail names the parser, hash or filesystem error. Refused before patch downloads and before hosted takeover; the package's `failed` event carries `errorCode` and `error`, while `get ` also carries top-level `error.code`. Dry-run predicts the same refusal. | | `vendor_bun_workspace_unsupported` | `failed` | vendor / scan / get `--mode vendored` (bun): the text lock holds `workspace:` packages and its `lockfileVersion` is below 2 — Bun 1.2–1.3 resolve a workspace member's local-tarball path relative to the member; a committed version-2 lock is the proof every consumer runs Bun ≥ 1.4 (deliberate over-approximation: root-only declared packages would install on version 1 too). Detail names the version integer and a version-specific remedy: delete `bun.lock` and re-lock with Bun ≥ 1.4 (an in-place `bun install` keeps the existing version) — then, for a version-1 lock, "or use `--mode hosted`, which accepts version-1 workspace locks"; for a version-0 lock, "or delete `bun.lock`, re-lock with Bun ≥ 1.2 (which writes lockfileVersion 1) and use `--mode hosted`" (hosted refuses version-0 workspace locks, so a bare hosted pointer would send the user into a second refusal). Refused before any write — in the pre-download preflight on `get`/`scan` (see `vendor_bun_lockb_invalid` for the placements); in the shared preflight that `vendor` and the vendor step run BEFORE a hosted → vendored takeover's revert (a hosted-redirected purl stays hosted-wired, ledger and lock untouched; `vendor --dry-run` previews the same `failed` code); and in the engine when the run would write a NEW local tuple. Exempt: purls the vendor ledger wires at the selected uuid, purls whose every `bun.lock` instance is already a `.socket/vendor/npm/` tuple (any uuid), in-sync re-runs and `repair` redownloads. | +| `vendor_composer_bazel_workspace` | `failed` | vendor / scan / get `--mode vendored` (composer): the project is in a Bazel workspace (its directory, or one above it up to the root of its git checkout, holds `MODULE.bazel`, `REPO.bazel`, `WORKSPACE` or `WORKSPACE.bazel`). Vendored mode makes `composer.lock` name the patched copy by a path relative to the directory Composer runs in; a Bazel repository rule or build action that runs `composer install` from the lock anywhere else fails with Composer's `Source path "…" is not found for package …`, and the build with it (Bazel 9.3.2, 8.8.2, 7.7.2). Refused before any download and before a hosted → vendored takeover or eject (the hosted pin stays; the eject reports it under `eject_refused`, detail `kept hosted: …`); nothing is written; exit 1 / `partialFailure`; `scan` and `get` with `--dry-run` list the package as would-refuse (`skipped` with this code, exit 0), and `vendor --dry-run` reports it `failed` with exit 1, as it does every refusal. The detail names the Bazel root and the mode that writes no such path, `--mode hosted`. Not refused: a run with `--allow-vendored-in-bazel`, `vendor --revert`, and a package whose `composer.lock` entry already names the copy of the same patch (the run leaves the lock as it is, so a project vendored earlier keeps passing its runs, a `scan` or `get` with no `--mode` included; a new patch for it is refused). A project that commits its `vendor/` tree is refused like any other: the tree shows that Composer has run in the project's directory, not that no rule of the workspace runs it elsewhere as well. See docs/ecosystems.md "Bazel workspaces: Composer". | | `vendor_lockfile_missing` / `vendor_lockfile_version_unsupported` (bun preflight placement) | `failed` | scan / get `--mode vendored` (bun): the pre-download preflight found `bun.lock` unreadable / at a `lockfileVersion` other than 0 to 3 (a newer version: update socket-patch; no integer: re-lock with Bun ≥ 1.2 — the same text as hosted's `redirect_bun_lock_unsupported`) or outside bun's single-line `packages` grammar. Same placements as `vendor_bun_lockb_invalid`; nothing fetched, no patch record. An unreadable `.socket/vendor/state.json` met by the same preflight is `vendor_state_unreadable` (see that row), never one of these. | | `bun_lockb_invalid` | scan `warnings[]` (run-level) | scan (every mode): the native binary inventory could not parse or read `bun.lockb`; detail names the format or filesystem error. Also printed as `Warning: …` on stderr. Exit and status remain unchanged. The warning is retained on empty and non-empty scans; valid binary locks are inventoried normally without a runtime or install. | | `bun_lock_unsupported` | scan `warnings[]` (run-level) | scan (every mode): the text `bun.lock` is there but the lock reader refuses it — a `lockfileVersion` newer than 3, no integer version, or a `packages` section outside bun's single-line grammar (a comment line, a re-indent) — so the packages it lists were not read from the lockfile. Detail is the shared version gate's text or `bun.lock: line : …`, followed by `, so the packages it lists were NOT read from the lockfile: a package that is locked but not installed was not scanned`. Also printed as `Warning: …` on stderr, and a scan that then finds no package at all ends `No packages found (see the warning).` instead of advising an install. Status and exit are unchanged (0): before, a checkout without `node_modules` scanned as an empty project and the lock was not mentioned. Installed packages are still crawled, and hosted / vendored runs keep their own refusal (`redirect_bun_lock_unsupported` / `vendor_lockfile_version_unsupported`). The hosted in-memory engine reports the same code among its warnings. | diff --git a/crates/socket-patch-cli/src/args.rs b/crates/socket-patch-cli/src/args.rs index 919bc0cbe..bebd4ba9a 100644 --- a/crates/socket-patch-cli/src/args.rs +++ b/crates/socket-patch-cli/src/args.rs @@ -421,6 +421,23 @@ pub struct GlobalArgs { value_parser = parse_bool_flag, )] pub gem_refresh_shared: bool, + + /// Vendored mode (`vendor`, `scan`/`get --mode vendored`): write for a + /// package although the project is in a Bazel workspace, where the + /// build may not find the vendored copy. Vendored mode declines such + /// a package by default; today that is a Composer package + /// (`vendor_composer_bazel_workspace`), whose lock names the copy by a + /// path that a Bazel rule running `composer install` in another + /// directory does not find. Pass it when the build does find the + /// copy. Other subcommands accept it silently. + #[arg( + help_heading = GLOBAL_OPTIONS, + long = "allow-vendored-in-bazel", + env = "SOCKET_ALLOW_VENDORED_IN_BAZEL", + default_value_t = false, + value_parser = parse_bool_flag, + )] + pub allow_vendored_in_bazel: bool, } impl GlobalArgs { @@ -742,8 +759,9 @@ pub(crate) fn socket_dir_of(manifest_path: &Path, cwd: &Path) -> PathBuf { /// Mirror the flags that core reads from the env (`SOCKET_OFFLINE`, /// `SOCKET_DEBUG`, `SOCKET_TELEMETRY_DISABLED`, `SOCKET_API_URL`, -/// `SOCKET_PROXY_URL`) into it. Core's own parse of `SOCKET_OFFLINE` and -/// `SOCKET_DEBUG` matches only `"1" | "true"`, +/// `SOCKET_PROXY_URL`, `SOCKET_ALLOW_VENDORED_IN_BAZEL`) into it. Core's +/// own parse of `SOCKET_OFFLINE` and `SOCKET_DEBUG` matches only +/// `"1" | "true"`, /// and the telemetry kill-switch and endpoint resolvers never see the parsed /// flags: without the mirror `--offline` still sent telemetry, and an /// on-prem `--api-url` run sent it, token included, to `api.socket.dev`. @@ -762,6 +780,9 @@ pub(crate) fn apply_env_toggles(common: &GlobalArgs) { if common.no_telemetry { std::env::set_var("SOCKET_TELEMETRY_DISABLED", "1"); } + if common.allow_vendored_in_bazel { + std::env::set_var(socket_patch_core::vendor::refusals::ALLOW_IN_BAZEL_ENV, "1"); + } if let Some(url) = common.api_url.as_deref().filter(|s| !s.is_empty()) { std::env::set_var("SOCKET_API_URL", url); } @@ -803,6 +824,7 @@ pub const GLOBAL_ARG_ENV_VARS: &[&str] = &[ "SOCKET_NO_GEM_NATIVE_REBUILD", "SOCKET_NO_GEM_INSTALL_REFRESH", "SOCKET_GEM_REFRESH_SHARED", + "SOCKET_ALLOW_VENDORED_IN_BAZEL", ]; /// Every env var a subcommand-local flag binds (one per `env = "..."` @@ -877,6 +899,7 @@ impl Default for GlobalArgs { no_gem_native_rebuild: false, no_gem_install_refresh: false, gem_refresh_shared: false, + allow_vendored_in_bazel: false, } } } diff --git a/crates/socket-patch-cli/src/commands/vendor.rs b/crates/socket-patch-cli/src/commands/vendor.rs index 5f7720515..d90e89e11 100644 --- a/crates/socket-patch-cli/src/commands/vendor.rs +++ b/crates/socket-patch-cli/src/commands/vendor.rs @@ -1987,6 +1987,57 @@ impl EjectSnapshot { } } +/// An eject refused whole, with nothing changed: each pin of `refused` is +/// a `failed` event with its code and its reason, the envelope's error is +/// `eject_refused`, and the exit code is 1. +async fn eject_refused( + common: &GlobalArgs, + run_warnings: Vec<(String, String)>, + client: &socket_patch_core::api::client::ApiClient, + telemetry: &TelemetryAuth, + eject_uuids: &HashMap, + refused: &[(String, &'static str, String)], +) -> i32 { + let mut env = Envelope::new(Command::Vendor); + env.warnings.extend( + run_warnings + .into_iter() + .map(|(code, detail)| RunWarning { code, detail }), + ); + env.dry_run = common.dry_run; + if common.json { + env.warnings + .extend(crate::commands::vex_sources::api_auth_fallback_warning( + client, + )); + } + for (purl, code, why) in refused { + report_vendor_failure(common, purl, why); + env.record( + PatchEvent::new(PatchAction::Failed, purl.clone()).with_error(*code, why.clone()), + ); + } + env.mark_error(EnvelopeError::new( + "eject_refused", + "not every hosted pin can be restored to its upstream registry entry and vendored; \ + nothing was changed", + )); + if common.json { + println!("{}", env.to_pretty_json()); + } + track_outcomes_for_vendor( + "vendor", + true, + &env, + common.dry_run, + eject_uuids, + &[], + telemetry, + ) + .await; + 1 +} + /// Standalone `vendor` with hosted pins in scope: EJECT. The patch set comes /// only from the lockfile pins (purl + the uuid in each hosted URL), even /// when `.socket/manifest.json` exists: manifest-only patches are not @@ -2029,6 +2080,30 @@ async fn run_eject( get_api_client_with_overrides(common.api_client_overrides()).await; let telemetry = TelemetryAuth::for_client(&client); let service = common.vendor_service_config(Some(client.clone()), use_public_proxy); + // The refusals that rest on the Bazel workspace need no record and no + // registry, so they are asked before either is fetched: the pins stay + // hosted whatever the network says. + let pairs: Vec<(&str, &str)> = pins + .iter() + .map(|pin| (pin.purl.as_str(), pin.uuid.as_str())) + .collect(); + let declined: Vec<(String, &'static str, String)> = + vendor::refusals::bazel_workspace_refusals(&common.cwd, &pairs) + .await + .into_iter() + .map(|(purl, code, detail)| (purl.to_string(), code, format!("kept hosted: {detail}"))) + .collect(); + if !declined.is_empty() { + return eject_refused( + common, + run_warnings, + &client, + &telemetry, + &eject_uuids, + &declined, + ) + .await; + } if !common.json && !common.silent { println!( "{} {} into .socket/vendor/...", @@ -2196,44 +2271,15 @@ async fn run_eject( } } if !refused.is_empty() { - let mut env = Envelope::new(Command::Vendor); - env.warnings.extend( - run_warnings - .into_iter() - .map(|(code, detail)| RunWarning { code, detail }), - ); - env.dry_run = common.dry_run; - if common.json { - env.warnings - .extend(crate::commands::vex_sources::api_auth_fallback_warning( - &client, - )); - } - for (purl, code, why) in &refused { - report_vendor_failure(common, purl, why); - env.record( - PatchEvent::new(PatchAction::Failed, purl.clone()).with_error(*code, why.clone()), - ); - } - env.mark_error(EnvelopeError::new( - "eject_refused", - "not every hosted pin can be restored to its upstream registry entry and vendored; \ - nothing was changed", - )); - if common.json { - println!("{}", env.to_pretty_json()); - } - track_outcomes_for_vendor( - "vendor", - true, - &env, - common.dry_run, - &eject_uuids, - &[], + return eject_refused( + common, + run_warnings, + &client, &telemetry, + &eject_uuids, + &refused, ) .await; - return 1; } // A dry run stops at the verified plan: restoring the live lock to diff --git a/crates/socket-patch-cli/tests/cli_global_args.rs b/crates/socket-patch-cli/tests/cli_global_args.rs index 24db0bcb2..cf08df57d 100644 --- a/crates/socket-patch-cli/tests/cli_global_args.rs +++ b/crates/socket-patch-cli/tests/cli_global_args.rs @@ -147,6 +147,9 @@ fn global_flag_cases() -> Vec<(&'static str, Option<&'static str>, fn(&GlobalArg ("--gem-refresh-shared", None, |c| { assert!(c.gem_refresh_shared) }), + ("--allow-vendored-in-bazel", None, |c| { + assert!(c.allow_vendored_in_bazel) + }), ("--lock-timeout", Some("30"), |c| { assert_eq!(c.lock_timeout, Some(30)) }), @@ -259,6 +262,7 @@ fn global_flag_cases_cover_every_global_field() { no_gem_native_rebuild: _, no_gem_install_refresh: _, gem_refresh_shared: _, + allow_vendored_in_bazel: _, strict: _, allow_patch_conflicts: _, vendor_source: _, @@ -589,6 +593,9 @@ fn bool_env_vars_reject_zero_and_falsey() { ("SOCKET_DEBUG", |c| c.debug), ("SOCKET_TELEMETRY_DISABLED", |c| c.no_telemetry), ("SOCKET_JSON", |c| c.json), + ("SOCKET_ALLOW_VENDORED_IN_BAZEL", |c| { + c.allow_vendored_in_bazel + }), ]; let parse_list = |var: &str, value: &str| { @@ -627,7 +634,7 @@ fn bool_env_vars_reject_zero_and_falsey() { #[serial_test::serial] fn empty_bool_env_var_resolves_to_false_not_crash() { // (env var, accessor) for every boolean global. - let bool_vars: [(&str, fn(&GlobalArgs) -> bool); 16] = [ + let bool_vars: [(&str, fn(&GlobalArgs) -> bool); 17] = [ ("SOCKET_OFFLINE", |c| c.offline), ("SOCKET_STRICT", |c| c.strict), ("SOCKET_GLOBAL", |c| c.global), @@ -652,6 +659,9 @@ fn empty_bool_env_var_resolves_to_false_not_crash() { c.no_gem_install_refresh }), ("SOCKET_GEM_REFRESH_SHARED", |c| c.gem_refresh_shared), + ("SOCKET_ALLOW_VENDORED_IN_BAZEL", |c| { + c.allow_vendored_in_bazel + }), ]; for (var, accessor) in bool_vars { diff --git a/crates/socket-patch-cli/tests/common/clap_parse.rs b/crates/socket-patch-cli/tests/common/clap_parse.rs index 7e4f772f6..d49f2169c 100644 --- a/crates/socket-patch-cli/tests/common/clap_parse.rs +++ b/crates/socket-patch-cli/tests/common/clap_parse.rs @@ -83,6 +83,7 @@ pub fn global_defaults() -> Globals { no_gem_native_rebuild: false, no_gem_install_refresh: false, gem_refresh_shared: false, + allow_vendored_in_bazel: false, }) } diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs index 1d3b7614e..627d2ae0b 100644 --- a/crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs @@ -1,5 +1,5 @@ -//! Hosted and agent mode for a Composer package in a Bazel workspace that -//! builds PHP with rules of its own. +//! Hosted, agent and vendored mode for a Composer package in a Bazel +//! workspace that builds PHP with rules of its own. //! //! No Bazel ruleset builds PHP or takes Composer packages, and the Bazel //! registry holds none. A PHP project under Bazel writes the few lines that @@ -20,8 +20,15 @@ //! fresh output base: hosted mode in the first two projects, where Bazel //! runs Composer on the lock that `socket-patch` rewrites, and agent mode //! in the third, where Bazel reads the files that `socket-patch` patches in -//! place. Vendored mode is in no flow: it breaks the first two builds -//! today. The harness builds `//:consumer`, so a test writes the root +//! place. Vendored mode has two flows of its own, which their tests +//! describe. In the first project it is declined, because the build would +//! fail on the path it writes into the lock, and hosted mode patches the +//! build in its place; with the flag that lifts the refusal the build does +//! fail. In the third it is declined as well, and with the flag the build +//! is the patched one once the developer has installed from the lock. The +//! tests at the end that need no Bazel hold every command that would +//! vendor to the refusal, and to the user's word, which alone lifts it. +//! The harness builds `//:consumer`, so a test writes the root //! `BUILD.bazel` that points that label at the consumer of its project. //! //! - (a) `bazel build` and `bazel run //:consumer` print the unpatched @@ -132,7 +139,8 @@ use socket_patch_core::hash::git_sha256::compute_git_sha256_from_bytes; use crate::envelope::{codes_in, event_for, event_triples}; use crate::harness::{ - changed, fixture_path, Cell, Fixture, Ran, Release, StandIn, Tree, CONSUMER, NOTHING, REQUIRED, + cell_without_bazel, changed, fixture_path, Cell, Fixture, Ran, Release, StandIn, Tree, + CONSUMER, NOTHING, REQUIRED, }; use crate::hosted_memory_common::hermetic::{self, Extra}; use crate::hosted_memory_common::{read_tree, Patch, ORG}; @@ -189,6 +197,11 @@ const PRODUCT: &str = "pkg:composer/socketsecurity/bazel-fixture-app@1.0.0"; const REFUSAL: &str = "The checksum verification of the file failed"; /// The hosts a proxy is never asked for: a stand-in listens on loopback. const LOOPBACK: &str = "127.0.0.1,localhost"; +/// The code under which vendored mode declines a Composer project in a +/// Bazel workspace, and the flag and the variable that lift it. +const DECLINED: &str = "vendor_composer_bazel_workspace"; +const ALLOW: &str = "--allow-vendored-in-bazel"; +const ALLOW_ENV: &str = "SOCKET_ALLOW_VENDORED_IN_BAZEL"; /// The two tools of the machine a flow runs: the program, the arguments /// that make it print its release, the text before the release, and the @@ -579,7 +592,9 @@ impl Machine { fn composer(&self, test: &str, files: &[(&str, &[u8])], args: &[&str]) -> (Tree, String) { let project = tempfile::tempdir_in(self.root()).expect("a project directory"); for (rel, bytes) in files { - std::fs::write(project.path().join(rel), bytes).expect("write a project file"); + let path = project.path().join(rel); + std::fs::create_dir_all(path.parent().expect("a file path")).expect("create a parent"); + std::fs::write(path, bytes).expect("write a project file"); } let mut cmd = hermetic::tool_command("composer"); hermetic::scrub_socket_vars(&mut cmd); @@ -689,6 +704,17 @@ fn open(test: &str, project: &str, args: &[&str]) -> Option { /// the patch server (a hosted pin is recognised by that origin) and as the /// registry `rollback` reads the upstream entry from. fn socket_patch(cell: &Cell, standin: &StandIn, project: &str, args: &[&str]) -> Ran { + socket_patch_with(cell, standin, project, args, &[]) +} + +/// [`socket_patch`] with the variables `more` set as well. +fn socket_patch_with( + cell: &Cell, + standin: &StandIn, + project: &str, + args: &[&str], + more: &[(&str, &str)], +) -> Ran { let origin = standin.origin(); let packagist = format!("{origin}/{PACKAGIST}"); let env = [ @@ -696,7 +722,7 @@ fn socket_patch(cell: &Cell, standin: &StandIn, project: &str, args: &[&str]) -> ("SOCKET_PACKAGIST_URL", packagist.as_str()), ]; let args = [args, &["--cwd", project][..]].concat(); - cell.socket_patch(&origin, &args, &env) + cell.socket_patch(&origin, &args, &[&env[..], more].concat()) } /// What a command reports when its one event is about the package: that @@ -1176,6 +1202,368 @@ fn agent_committed_vendor() { ); } +/// The one command of vendored mode. +const VENDORED: [&str; 4] = ["scan", "--mode", "vendored", "--yes"]; + +/// Vendored mode where it declines: `socket-patch ` in `project` +/// ends with `code` and reports the package under [`DECLINED`], as the one +/// event `action`, in words that name hosted mode and the flag. No file of +/// the checkout differs from `before`. Gives the finished command. +fn declines( + flow: &Flow, + project: &str, + args: &[&str], + code: i32, + action: &str, + before: &Tree, +) -> Ran { + let (cell, standin) = (&flow.cell, &flow.standin); + let ran = socket_patch(cell, standin, project, args); + let (test, said) = (&cell.test, &ran.dump); + assert_eq!(ran.code, code, "{test}: {said}"); + let declined = one(action, DECLINED); + assert_eq!(event_triples(&ran.envelope), declined, "{test}: {said}"); + let event = event_for(&ran.envelope, PURL); + let why = event["error"].as_str().or(event["reason"].as_str()); + let why = why.unwrap_or_default(); + let named = ["`--mode hosted`", &format!("`{ALLOW}`")]; + assert!( + named.iter().all(|part| why.contains(part)), + "{test}: {said}" + ); + assert_eq!(changed(before, &cell.tree()), NOTHING, "{test}: {said}"); + ran +} + +/// Where vendored mode keeps the copy of the package for `patch`, as the +/// lock names it: a path from the directory Composer runs in. +fn copy_of(patch: &Patched) -> String { + format!(".socket/vendor/composer/{}/{NAME}@{VERSION}", patch.uuid) +} + +/// Vendored mode where it goes on: `scan --mode vendored --yes ` in +/// `project` vendors `patch`. It keeps the patched package under the +/// project's `.socket/vendor`, with its ledger, and rewrites the lock, and +/// no other file of the checkout differs from `before`. The entry of the +/// package has that copy as its `dist`, by the relative path of +/// [`copy_of`] and under the id of the patch, to be copied and never +/// linked, and no `source`; the rest of the lock says what it said. Gives +/// the files of the checkout as the command left them. +fn vendors(flow: &Flow, project: &str, patch: &Patched, before: &Tree, allow: &[&str]) -> Tree { + let (cell, standin) = (&flow.cell, &flow.standin); + let args = [&VENDORED[..], allow].concat(); + let scan = socket_patch(cell, standin, project, &args); + let (test, said) = (&cell.test, &scan.dump); + assert_eq!(scan.code, 0, "{test}: {said}"); + assert_eq!(scan.envelope["status"], "success", "{test}: {said}"); + assert_eq!(scan.envelope["summary"]["applied"], 1, "{test}: {said}"); + let event = event_for(&scan.envelope, PURL); + assert_eq!(event["uuid"], patch.uuid.as_str(), "{test}: {said}"); + let events = ["downloaded", "applied", "skipped"].map(|action| { + let code = if action == "skipped" { + "vendor_prebuilt_downloaded" + } else { + "" + }; + (PURL.to_string(), action.to_string(), code.to_string()) + }); + assert_eq!(event_triples(&scan.envelope), events, "{test}: {said}"); + let vendored = cell.tree(); + let lock = lock_of(project); + let kept = format!("{project}/.socket/vendor"); + let copy = format!("{project}/{}", copy_of(patch)); + let mut written = vec![ + format!("{kept}/composer/{}/socket-patch.vendor.json", patch.uuid), + format!("{copy}/composer.json"), + format!("{copy}/{FILE}"), + format!("{kept}/state.json"), + lock.clone(), + ]; + written.sort(); + assert_eq!(changed(before, &vendored), written, "{test}: {said}"); + let patched = patch.package.source.as_bytes(); + assert_eq!(vendored[&format!("{copy}/{FILE}")], patched, "{test}"); + + let parsed = |tree: &Tree| -> Value { serde_json::from_slice(&tree[&lock]).expect("a lock") }; + let mut expected = parsed(before); + let entry = expected["packages"][0].as_object_mut().expect("an entry"); + entry.remove("source"); + entry["dist"] = json!({ "type": "path", "url": copy_of(patch), "reference": patch.uuid }); + entry.insert("transport-options".to_string(), json!({ "symlink": false })); + assert_eq!(parsed(&vendored), expected, "{test}: {lock}"); + vendored +} + +/// Vendored mode in the project whose packages a repository rule +/// installs. The lock would name the copy by a path from the directory +/// Composer runs in, and the rule runs Composer in a repository of Bazel's +/// that holds the two Composer files and nothing else. So the command +/// declines: it writes nothing, and the build stays the one of before. +/// Hosted mode, which the refusal names, patches that build. With the flag +/// the command does what it did before the refusal was there: it reports +/// the package vendored, the build fails, and `vex` attests the package +/// all the same. `vendor --revert`, which takes no flag, puts every file +/// back, and the build with them. +#[test] +#[ignore = "real Bazel; run with --ignored"] +fn vendored_repository_rule() { + let test = "composer::vendored_repository_rule"; + let started = Instant::now(); + let Some(flow) = open(test, API, &["--no-install"]) else { + return; + }; + let (cell, standin, upstream) = (&flow.cell, &flow.standin, &flow.upstream); + let release = cell.release().to_string(); + let patch = Patched::new(); + grant(standin, upstream, &patch, &patch.package); + + // (a) The unpatched build. + let before = cell.builds_unpatched(&upstream.package.marker); + + // (b) The one command of vendored mode, in the project: declined, with + // nothing written and nothing of the patch fetched. + declines(&flow, API, &VENDORED, 1, "failed", &before); + assert_eq!(standin.asked(&patch.route), 0, "{test}"); + + // (c) The same build passes, on the upstream package. + cell.builds_patched(&before, &upstream.package.marker); + + // The remedy the refusal names: hosted mode patches the build, and its + // undo puts every file back. + let pinned = pins(&flow, API, &patch, &before); + cell.builds_patched(&pinned, &patch.package.marker); + let undo = socket_patch(cell, standin, API, &["rollback", "--yes"]); + cell.undone(&undo, "rolledBack", &before, &upstream.package.marker); + + // With the flag the package is vendored, and the same build fails: + // Composer finds no copy where the rule runs it. + vendors(&flow, API, &patch, &before, &[ALLOW]); + let build = cell.bazel("build", &[CONSUMER]); + assert!(!build.ok(), "{test}: the build passed:\n{}", build.dump()); + let copy = copy_of(&patch); + let missing = format!("Source path \"{copy}\" is not found for package {NAME}"); + // Composer breaks the message into lines of its console's width, inside + // a word too, so the two are compared without white space. + let squeezed = |text: &str| text.split_whitespace().collect::(); + assert!( + squeezed(&build.stderr()).contains(&squeezed(&missing)), + "{test}: the build did not fail with {missing:?}:\n{}", + build.dump() + ); + attested(&flow, API); + + // (d) The undo: every file is the one of before, and so is the build. + let undo = socket_patch(cell, standin, API, &["vendor", "--revert"]); + cell.undone(&undo, "removed", &before, &upstream.package.marker); + + println!( + "{test}: Bazel {release} with {}: `scan --mode vendored --cwd {API}` declined \ + ({DECLINED}), wrote nothing, and the build stayed {:?}; `scan --mode hosted` built \ + {:?} and `rollback` restored all {} files; with {ALLOW} the package was vendored and \ + the next build failed (Composer: {missing}) while `vex` attested the package; `vendor \ + --revert` restored the files and the build; {:?}", + flow.tools, + upstream.package.marker, + patch.package.marker, + before.len(), + started.elapsed() + ); +} + +/// Vendored mode in the project that commits its `vendor/` tree. The +/// command declines it like any project in a Bazel workspace: the tree +/// does not show that no rule of the workspace runs Composer somewhere +/// else as well. Here none does, and Composer runs in the project's +/// directory, so with the flag the path the lock names leads to the copy. +/// The build reads the committed tree and stays the unpatched one until +/// the developer's `composer install` has copied the package from +/// `.socket/vendor`; then it is the patched one. +#[test] +#[ignore = "real Bazel; run with --ignored"] +fn vendored_committed_vendor() { + let test = "composer::vendored_committed_vendor"; + let started = Instant::now(); + let Some(flow) = open(test, LEGACY, &[]) else { + return; + }; + let (cell, standin, upstream) = (&flow.cell, &flow.standin, &flow.upstream); + let release = cell.release().to_string(); + let patch = Patched::new(); + grant(standin, upstream, &patch, &patch.package); + + // (a) The unpatched build, from the committed tree. + let before = cell.builds_unpatched(&upstream.package.marker); + + // (b) The one command of vendored mode, in the project: declined, with + // nothing written. With the flag it vendors the package. + declines(&flow, LEGACY, &VENDORED, 1, "failed", &before); + let vendored = vendors(&flow, LEGACY, &patch, &before, &[ALLOW]); + + // (c) The same build passes and is the unpatched one: Bazel reads the + // tree, which no command has touched. + cell.builds_patched(&vendored, &upstream.package.marker); + // The developer installs from the lock, in the project's directory: + // Composer copies the package from `.socket/vendor`, with no request + // to a host, and the build is the patched one. + let prefix = format!("{LEGACY}/"); + let project: Vec<(&str, &[u8])> = vendored + .iter() + .filter_map(|(path, bytes)| Some((path.strip_prefix(&prefix)?, bytes.as_slice()))) + .collect(); + let requests = standin.requests(); + let (installed, said) = flow.machine.composer(test, &project, &["install"]); + assert_eq!(standin.requests(), requests, "{test}: {said}"); + for (rel, bytes) in &installed { + cell.write(&format!("{LEGACY}/{rel}"), bytes); + } + let copied = format!("{LEGACY}/vendor/{NAME}/{FILE}"); + let reinstalled = cell.tree(); + assert_eq!( + reinstalled[&copied], + patch.package.source.as_bytes(), + "{test}: {said}" + ); + cell.builds_patched(&reinstalled, &patch.package.marker); + + // (d) The undo: the lock is the one of before and `.socket/vendor` is + // gone. The tree keeps what Composer installed, as in any project, + // until the developer installs again. + let undo = socket_patch(cell, standin, LEGACY, &["vendor", "--revert"]); + let said = &undo.dump; + assert_eq!(undo.code, 0, "{test}: {said}"); + let reverted = cell.tree(); + let lock = lock_of(LEGACY); + assert_eq!(reverted[&lock], before[&lock], "{test}: {said}"); + let kept = format!("{LEGACY}/.socket/vendor/"); + let left: Vec<&String> = reverted + .keys() + .filter(|path| path.starts_with(&kept)) + .collect(); + assert!(left.is_empty(), "{test}: {left:?}\n{said}"); + + println!( + "{test}: Bazel {release} with {}: `scan --mode vendored --cwd {LEGACY}` declined \ + ({DECLINED}) and wrote nothing; with {ALLOW} it vendored the package into {} and the \ + build stayed {:?}; after the developer's `composer install` it built {:?} from \ + {copied}; `vendor --revert` restored {lock} and removed the copy; {:?}", + flow.tools, + copy_of(&patch), + upstream.package.marker, + patch.package.marker, + started.elapsed() + ); +} + +/// Without a Bazel, a PHP or a Composer: a checkout whose project [`API`] +/// has a lock that pins the upstream package of a stand-in, with the files +/// `marks` at the root of the checkout, which make it a Bazel workspace +/// when one has a name of Bazel's, and a patch of the package that the +/// stand-in grants. +fn checkout(marks: &[&str]) -> (Flow, Patched) { + let cell = cell_without_bazel("9.3.2"); + let (upstream, patch) = (Upstream::new(), Patched::new()); + let standin = registry(&upstream); + grant(&standin, &upstream, &patch, &patch.package); + cell.write(".git/HEAD", "ref: refs/heads/main\n"); + for mark in marks { + cell.write(mark, ""); + } + cell.write(&format!("{API}/composer.json"), manifest(API)); + let entry = metadata(&standin.origin(), &upstream); + let lock = json!({ "packages": [entry], "packages-dev": [] }); + cell.write(&lock_of(API), format!("{lock:#}\n")); + let flow = Flow { + machine: Machine::new(&standin), + cell, + standin, + upstream, + tools: String::new(), + }; + (flow, patch) +} + +/// Without a Bazel: in a Bazel workspace every command that would vendor +/// the package declines the project and writes nothing: the scan, `get`, +/// `vendor` for a patch the manifest records and, where the package is +/// pinned hosted, the scan that would take the pin over and the `vendor` +/// that would eject it, so the pin stays. A dry run of the scan names the +/// refusal and passes; one of `vendor` fails as its run does. No command +/// fetches the patched archive. +#[test] +fn every_command_that_would_vendor_declines_a_project_in_a_bazel_workspace() { + let (flow, patch) = checkout(&["MODULE.bazel"]); + let (cell, standin) = (&flow.cell, &flow.standin); + let before = cell.tree(); + let get = ["get", PURL, "--mode", "vendored", "--yes"]; + let dry_run = [&VENDORED[..], &["--dry-run"]].concat(); + declines(&flow, API, &VENDORED, 1, "failed", &before); + declines(&flow, API, &get, 1, "failed", &before); + declines(&flow, API, &dry_run, 0, "skipped", &before); + + let record = ["get", PURL, "--save-only", "--yes"]; + let saved = socket_patch(cell, standin, API, &record); + assert_eq!(saved.code, 0, "{}", saved.dump); + let vendor = [["vendor", "--yes"], ["vendor", "--dry-run"]]; + for vendor in &vendor { + declines(&flow, API, vendor, 1, "failed", &cell.tree()); + } + std::fs::remove_dir_all(cell.path(&format!("{API}/.socket"))).expect("remove .socket"); + + // The eject is refused whole, before it restores a pin or asks the + // service for a record, and so is its dry run. + let pinned = pins(&flow, API, &patch, &before); + declines(&flow, API, &VENDORED, 1, "failed", &pinned); + for eject in &vendor { + let asked = standin.requests(); + let eject = declines(&flow, API, eject, 1, "failed", &pinned); + assert_eq!(standin.requests(), asked, "{}", eject.dump); + let refused = &eject.envelope["error"]["code"]; + assert_eq!(*refused, "eject_refused", "{}", eject.dump); + let why = event_for(&eject.envelope, PURL)["error"].as_str(); + let kept = why.is_some_and(|why| why.starts_with("kept hosted: ")); + assert!(kept, "{}", eject.dump); + } + assert_eq!(standin.asked(&patch.route), 0); +} + +/// Without a Bazel: vendored mode goes on as before in a project that is +/// in no Bazel workspace, and in one on the user's word, the flag or its +/// variable. A later run without the flag passes and writes nothing: the +/// lock already names the copy of that patch, so the run is in sync, and +/// so is a scan that names no mode, which runs in vendored mode in a +/// vendored project. `vendor --revert` takes no flag: it puts every file +/// back. +#[test] +fn vendored_mode_goes_on_outside_bazel_and_on_the_users_word() { + let (flow, patch) = checkout(&["BUILD.bazel"]); + vendors(&flow, API, &patch, &flow.cell.tree(), &[]); + + let (flow, patch) = checkout(&["WORKSPACE"]); + let (cell, standin) = (&flow.cell, &flow.standin); + let before = cell.tree(); + let reverts = |why: &str| { + let undo = socket_patch(cell, standin, API, &["vendor", "--revert", "--yes"]); + assert_eq!(undo.code, 0, "{why}: {}", undo.dump); + assert_eq!(changed(&before, &cell.tree()), NOTHING, "{why}"); + }; + let by_variable = socket_patch_with(cell, standin, API, &VENDORED, &[(ALLOW_ENV, "1")]); + let said = &by_variable.dump; + assert_eq!(by_variable.code, 0, "{said}"); + assert_eq!(by_variable.envelope["summary"]["applied"], 1, "{said}"); + reverts(ALLOW_ENV); + let vendored = vendors(&flow, API, &patch, &before, &[ALLOW]); + for in_sync in [&VENDORED[..], &["scan", "--yes"]] { + let again = socket_patch(cell, standin, API, in_sync); + let said = &again.dump; + assert_eq!(again.code, 0, "{in_sync:?}: {said}"); + assert_eq!(again.envelope["status"], "success", "{in_sync:?}: {said}"); + let refused = said.contains(DECLINED); + assert!(!refused, "{in_sync:?}: {said}"); + assert_eq!(changed(&vendored, &cell.tree()), NOTHING, "{in_sync:?}"); + } + reverts(ALLOW); +} + /// Without a Bazel, a PHP or a Composer: the fixture's directories and the /// legs of its workflow are those of its table. The fixture commits no /// lock, no `vendor/` tree and no root `BUILD.bazel`: the run makes them. diff --git a/crates/socket-patch-core/src/utils/env_compat.rs b/crates/socket-patch-core/src/utils/env_compat.rs index 3fb0748ac..aef8cc0da 100644 --- a/crates/socket-patch-core/src/utils/env_compat.rs +++ b/crates/socket-patch-core/src/utils/env_compat.rs @@ -2,7 +2,7 @@ //! accepted `SOCKET_CLI_*` peer aliases. /// Whether `name` is exactly `1` or `true`. -fn env_flag(name: &str) -> bool { +pub(crate) fn env_flag(name: &str) -> bool { matches!(std::env::var(name).as_deref(), Ok("1" | "true")) } diff --git a/crates/socket-patch-core/src/utils/process.rs b/crates/socket-patch-core/src/utils/process.rs index 5de62a0b7..7694c70f5 100644 --- a/crates/socket-patch-core/src/utils/process.rs +++ b/crates/socket-patch-core/src/utils/process.rs @@ -103,6 +103,7 @@ pub const CHILD_ENV_SCRUB: &[&str] = &[ "SOCKET_NO_GEM_NATIVE_REBUILD", "SOCKET_NO_GEM_INSTALL_REFRESH", "SOCKET_GEM_REFRESH_SHARED", + "SOCKET_ALLOW_VENDORED_IN_BAZEL", "SOCKET_PATCH_VERSION", "SOCKET_SAVE_ONLY", "SOCKET_ALL_RELEASES", diff --git a/crates/socket-patch-core/src/vendor/composer_lock.rs b/crates/socket-patch-core/src/vendor/composer_lock.rs index e7324ed2b..77f04ebd2 100644 --- a/crates/socket-patch-core/src/vendor/composer_lock.rs +++ b/crates/socket-patch-core/src/vendor/composer_lock.rs @@ -73,6 +73,7 @@ use crate::formats::composer::{ composer_lock_packages, ComposerLockPackage, ORIGIN_BOUND_ENTRY_KEYS, }; +pub mod bazel; mod lock_text; pub(super) mod mirror_filters; diff --git a/crates/socket-patch-core/src/vendor/composer_lock/bazel.rs b/crates/socket-patch-core/src/vendor/composer_lock/bazel.rs new file mode 100644 index 000000000..84fce4bd6 --- /dev/null +++ b/crates/socket-patch-core/src/vendor/composer_lock/bazel.rs @@ -0,0 +1,188 @@ +//! Composer vendored mode in a Bazel workspace: declined unless the user +//! allows it. +//! +//! Vendored mode points the lock entry at the committed copy by a path +//! from the directory Composer runs in +//! (`.socket/vendor/composer///@`). No Bazel +//! ruleset builds PHP, so a project under Bazel brings its packages into +//! the build with a few lines of its own: a repository rule or a genrule +//! that runs `composer install` from the committed lock, in a directory of +//! Bazel's that holds the two Composer files and nothing else. There the +//! path names nothing: Composer fails (`Source path "..." is not found for +//! package ...`) and the build with it, after a command that exited 0 +//! (Bazel 9.3.2, 8.8.2 and 7.7.2). +//! +//! Whether a workspace has such a rule cannot be read from its files. A +//! rule may take the lock through a glob, or live in another module. A +//! committed `vendor/` tree shows that Composer has run in the project's +//! directory, not that no rule runs it elsewhere as well: a workspace with +//! both fails the same way (Bazel 9.3.2). So every Composer project in a +//! Bazel workspace is declined, and the user's word is the one exception. +//! +//! What is declined is the write of that path. A package whose lock entry +//! already names the copy of the same patch is let through: the run leaves +//! the lock as it is, so a project that was vendored before (by an earlier +//! release, or with the flag) keeps passing its in-sync runs. + +use std::path::Path; + +use crate::formats::composer::composer_lock_packages; +use crate::utils::env_compat::env_flag; +use crate::utils::fs::{read_regular_to_string, run_blocking}; +use crate::utils::pnpm_workspace::without_verbatim_prefix; +use crate::utils::purl::parse_composer_purl; +use crate::vendor::common::parse_json_text; +use crate::vendor::refusals::ALLOW_IN_BAZEL_ENV; + +use super::{COMPOSER_LOCK, LOCK_MEMO}; + +/// The code of [`refusals`]. +pub const CODE: &str = "vendor_composer_bazel_workspace"; + +/// The Composer `candidates` (`(purl, uuid)` pairs) that vendored mode +/// declines in the project at `project`, each with [`CODE`] and the detail. +/// It declines a project in a Bazel workspace +/// ([`crate::bazel::bazel_root`]) unless the user allowed it +/// ([`ALLOW_IN_BAZEL_ENV`]), and lets through a package the lock already +/// names by the copy of that patch. Read-only: it looks for the root, reads +/// the lock, and starts no program. +pub async fn refusals<'a>( + project: &Path, + candidates: &[(&'a str, &str)], +) -> Vec<(&'a str, &'static str, String)> { + refusals_unless(project, candidates, env_flag(ALLOW_IN_BAZEL_ENV)).await +} + +/// [`refusals`] for a user who `allowed` it or did not. +async fn refusals_unless<'a>( + project: &Path, + candidates: &[(&'a str, &str)], + allowed: bool, +) -> Vec<(&'a str, &'static str, String)> { + if allowed || candidates.is_empty() { + return Vec::new(); + } + let dir = project.to_path_buf(); + let Some(root) = run_blocking(move || crate::bazel::bazel_root(&dir)).await else { + return Vec::new(); + }; + // The root is canonical: on Windows that spelling has a prefix no user + // types and no tool prints. + let detail = format!( + "the project is in the Bazel workspace at {}, and vendored mode makes composer.lock \ + name the patched copy by a path relative to the directory Composer runs in: a \ + Bazel rule that runs `composer install` anywhere else does not find it, and the \ + build fails; use `--mode hosted`, which names the patched archive by a URL that \ + Composer fetches from any directory, or pass `--allow-vendored-in-bazel` if \ + Composer only runs in the project's directory", + without_verbatim_prefix(root.dir).display() + ); + // A lock that cannot be read wires nothing: the backend says so itself. + let lock = match read_regular_to_string(&project.join(COMPOSER_LOCK)).await { + Ok(text) => LOCK_MEMO + .parse(text.as_bytes(), || parse_json_text(&text)) + .ok(), + Err(_) => None, + }; + let wired = |purl: &str, uuid: &str| { + let (Some(lock), Some(((vendor, name), _))) = (&lock, parse_composer_purl(purl)) else { + return false; + }; + let pkg = format!("{vendor}/{name}"); + composer_lock_packages(lock) + .into_iter() + .any(|p| p.name.is_some_and(|n| n.eq_ignore_ascii_case(&pkg)) && p.wired_to(uuid)) + }; + candidates + .iter() + .filter(|(purl, uuid)| !wired(purl, uuid)) + .map(|(purl, _)| (*purl, CODE, detail.clone())) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + const PURL: &str = "pkg:composer/acme/widget@1.0.0"; + const UUID: &str = "11111111-1111-4111-8111-111111111111"; + const OTHER_UUID: &str = "33333333-3333-4333-8333-333333333333"; + + /// The purls [`refusals_unless`] declines for a user who did not allow + /// it. + async fn declined<'a>(project: &Path, candidates: &[(&'a str, &str)]) -> Vec<&'a str> { + let refused = refusals_unless(project, candidates, false).await; + refused.into_iter().map(|(purl, _, _)| purl).collect() + } + + #[tokio::test] + async fn a_project_in_a_bazel_workspace_is_declined_and_no_other() { + let tmp = tempfile::tempdir().unwrap(); + let base = fs::canonicalize(tmp.path()).unwrap(); + for dir in ["repo/.git", "repo/services/api", "plain"] { + fs::create_dir_all(base.join(dir)).unwrap(); + } + let (repo, api) = (base.join("repo"), base.join("repo/services/api")); + let one = [(PURL, UUID)]; + assert!(declined(&api, &one).await.is_empty()); + // A workspace file in the project's directory, and one at the root + // of its checkout, under each name Bazel looks for. + for name in crate::bazel::WORKSPACE_FILES { + for dir in [&api, &repo] { + fs::write(dir.join(name), "").unwrap(); + let refused = refusals_unless(&api, &one, false).await; + let [(purl, code, detail)] = &refused[..] else { + panic!("{name}: {refused:?}"); + }; + assert_eq!((*purl, *code), (PURL, CODE), "{name}"); + // The root, not the project below it: the comma ends it. + let shown = without_verbatim_prefix(dir.clone()); + let named = [ + &*format!("the Bazel workspace at {},", shown.display()), + "`--mode hosted`", + "`--allow-vendored-in-bazel`", + ]; + assert!(named.iter().all(|part| detail.contains(part)), "{detail}"); + // The user's word lifts it. + assert!(refusals_unless(&api, &one, true).await.is_empty(), "{name}"); + fs::remove_file(dir.join(name)).unwrap(); + } + } + // A workspace beside the project is not the project's. + fs::write(base.join("plain/MODULE.bazel"), "").unwrap(); + assert!(declined(&api, &one).await.is_empty()); + } + + /// A package the lock already names by the copy of the same patch is + /// let through: the run would leave the lock as it is. Another patch of + /// that package, another package, and a lock that holds the registry + /// entry or cannot be read are declined. + #[tokio::test] + async fn a_package_the_lock_already_wires_to_the_patch_is_let_through() { + let tmp = tempfile::tempdir().unwrap(); + let project = tmp.path(); + fs::write(project.join("MODULE.bazel"), "").unwrap(); + let other = "pkg:composer/acme/gadget@2.0.0"; + let candidates = [(PURL, UUID), (other, UUID)]; + let lock = |dist: serde_json::Value| { + let entries = serde_json::json!([ + { "name": "Acme/Widget", "version": "1.0.0", "dist": dist }, + { "name": "acme/gadget", "version": "2.0.0", + "dist": { "type": "zip", "url": "https://registry.example/gadget.zip" } }, + ]); + let text = serde_json::json!({ "packages": entries, "packages-dev": [] }); + fs::write(project.join("composer.lock"), text.to_string()).unwrap(); + }; + let copy = format!(".socket/vendor/composer/{UUID}/acme/widget@1.0.0"); + lock(serde_json::json!({ "type": "path", "url": copy, "reference": UUID })); + assert_eq!(declined(project, &candidates).await, [other]); + assert_eq!(declined(project, &[(PURL, OTHER_UUID)]).await, [PURL]); + lock(serde_json::json!({ "type": "zip", "url": "https://registry.example/widget.zip" })); + assert_eq!(declined(project, &candidates).await, [PURL, other]); + fs::write(project.join("composer.lock"), "{").unwrap(); + assert_eq!(declined(project, &candidates).await, [PURL, other]); + fs::remove_file(project.join("composer.lock")).unwrap(); + assert_eq!(declined(project, &candidates).await, [PURL, other]); + } +} diff --git a/crates/socket-patch-core/src/vendor/refusals.rs b/crates/socket-patch-core/src/vendor/refusals.rs index 6d91c065e..87e710769 100644 --- a/crates/socket-patch-core/src/vendor/refusals.rs +++ b/crates/socket-patch-core/src/vendor/refusals.rs @@ -22,7 +22,11 @@ //! resolve (`vendor_gem_version_not_locked`, which the callers report //! as a skip); //! 6. a Maven version no module of the build selects -//! (`vendor_maven_version_not_selected`, a skip too). +//! (`vendor_maven_version_not_selected`, a skip too); +//! 7. a Composer project in a Bazel workspace +//! (`vendor_composer_bazel_workspace`), unless the user allowed it +//! ([`ALLOW_IN_BAZEL_ENV`]) or the lock already names the patch's copy +//! ([`bazel_workspace_refusals`]). //! //! Each check lives in its backend's module; this one only dispatches and //! scopes them. @@ -36,13 +40,19 @@ use crate::crawlers::Ecosystem; use crate::patch::redirect::upstream::HostedPin; use crate::utils::purl_key::PurlKey; use crate::vendor::state::{lookup_entry, VendorEntry}; -use crate::vendor::{bun_lock, gem, lock_inventory, npm_flavor, npm_lock, vlt_lock}; +use crate::vendor::{bun_lock, composer_lock, gem, lock_inventory, npm_flavor, npm_lock, vlt_lock}; /// The vendor ledger as the refusals consume it: the caller's own load /// outcome, so an UNREADABLE ledger is a refusal the run can report /// instead of a silently empty ledger. pub type LedgerLoad<'a> = Result<&'a HashMap, &'a std::io::Error>; +/// The binding of `--allow-vendored-in-bazel`, which the CLI mirrors the +/// flag into: `1` or `true` lets vendored mode write for a package although +/// the project is in a Bazel workspace, where the build may not find the +/// vendored copy. Every refusal that rests on the workspace reads it. +pub const ALLOW_IN_BAZEL_ENV: &str = "SOCKET_ALLOW_VENDORED_IN_BAZEL"; + /// Refusal codes that are expected skips, not command failures: the user's /// request is still fully satisfied when these are the only non-successes. /// Every vendored entry point (`vendor`, `scan`, `get`, their dry runs) @@ -92,6 +102,30 @@ fn is_cargo(purl: &str) -> bool { Ecosystem::from_purl(purl) == Some(Ecosystem::Cargo) } +fn is_composer(purl: &str) -> bool { + Ecosystem::from_purl(purl) == Some(Ecosystem::Composer) +} + +/// The refusals of vendored mode that rest on the Bazel workspace of the +/// project at `project_root`, for `candidates` (`(purl, uuid)` pairs): +/// each refused purl with its code and detail. None when the user allowed +/// it ([`ALLOW_IN_BAZEL_ENV`]). Read-only, and it asks no service. Every +/// vendored entry point takes this one answer: [`vendor_refusals`] hands it +/// to a scan, a `get` and a `vendor`, and the eject of hosted pins asks it +/// before it fetches or restores anything. A refusal of this kind for +/// another ecosystem belongs here; Composer's is the only one today. +pub async fn bazel_workspace_refusals<'a>( + project_root: &Path, + candidates: &[(&'a str, &str)], +) -> Vec<(&'a str, &'static str, String)> { + let composer: Vec<(&str, &str)> = candidates + .iter() + .filter(|(purl, _)| is_composer(purl)) + .copied() + .collect(); + composer_lock::bazel::refusals(project_root, &composer).await +} + /// Every refusal the vendor engine can decide for `candidates` (`(purl, /// uuid)` pairs) before any download, in the engine's order (see the /// module docs). Read-only; a candidate absent from the result is left to @@ -171,6 +205,9 @@ pub async fn vendor_refusals( for (purl, (code, detail)) in maven_refusals(project_root, candidates).await { refuse(&mut out, &purl, code, &detail); } + for (purl, code, detail) in bazel_workspace_refusals(project_root, candidates).await { + refuse(&mut out, purl, code, &detail); + } out } @@ -422,6 +459,27 @@ mod tests { serde_json::from_value(json).unwrap() } + /// A Composer project in a Bazel workspace: every Composer candidate + /// is refused with the one answer of the project, and no candidate of + /// another ecosystem is. Without the workspace file nothing is. The + /// switch is read from the environment, so the test holds it unset. + #[tokio::test] + #[serial_test::serial] + async fn composer_candidates_in_a_bazel_workspace_are_refused() { + let _unset = crate::test_env::EnvGuard::unset(ALLOW_IN_BAZEL_ENV); + let tmp = tempfile::tempdir().unwrap(); + let (one, other) = ("pkg:composer/a/b@1.0.0", "pkg:composer/c/d@2.0.0"); + let pairs = [(one, UUID), (other, OTHER_UUID), ("pkg:pypi/x@1.0.0", UUID)]; + let empty = HashMap::new(); + assert!(run(tmp.path(), &pairs, Ok(&empty)).await.is_empty()); + std::fs::write(tmp.path().join("MODULE.bazel"), "").unwrap(); + let got = run(tmp.path(), &pairs, Ok(&empty)).await; + assert_eq!(got.keys().collect::>(), [one, other]); + assert_eq!(got[one].code, "vendor_composer_bazel_workspace"); + assert_eq!(got[one], got[other]); + assert!(!refusal_is_benign(got[one].code)); + } + mod gem { use super::*; diff --git a/docs/ecosystems.md b/docs/ecosystems.md index ab1d2884b..4a4bff99f 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -23,7 +23,7 @@ The backticked slug in each row is the value `-e`/`--ecosystems` accepts (e.g. | Maven (`maven`) — Maven and Gradle | ✅ in place in every copy the build consumes: each `~/.m2` copy it reads and each Gradle `files-2.1` copy; `~/.m2` `.sha1`/`.md5` sidecars are rewritten, Gradle copies get advisories; jar-member records swap in the patch service's whole jar — prefer vendored / hosted, see [Maven & NuGet caveats](#maven--nuget-caveats) and [Gradle](#gradle) | ✅ suffixed Maven repository (`-socket.` pin + `.mvn/maven.config` + fallback file repository) for every pom root, single-module or reactor, or Gradle 6.8+ same-GAV repository with settings wiring and SHA-256 checks (a root with both `pom.xml` and a Gradle build wires both); see [JVM vendoring](design/maven-vendoring.md) and [Gradle](#gradle) | ✅ fail-closed by a Socket-only `-socket.` suffix: pom projects get a pinned `` (`${property}` versions are refused); Gradle 6.8+ builds get an owned settings script, lock-entry rewrites and a resolution tripwire — see [Maven & NuGet caveats](#maven--nuget-caveats) and [Gradle](#gradle) | | sbt / Mill / scala-cli (`maven`) | ✅ Coursier caches (sbt 1.3+, sbt 2, Mill, scala-cli) and Ivy caches (sbt 0.13–1.2, `useCoursier := false`) patched in place, Coursier checksum sidecars resynced — see [Scala build tools](#scala-build-tools-sbt-mill-scala-cli) | ✅ sbt 0.13.18+: generated `socket-patch-vendor.sbt` over the committed suffixed `.socket/vendor/maven2` tree; scala-cli directory builds: owned `socket-patch.scala` + same-GAV `.socket/vendor/coursier` tree (Linux / macOS); Mill: not wired (agent or hosted guidance) | ✅ sbt 0.13.18+: one generated `socket-patch.sbt`, gated on sbt's own `sbt update` records; Mill / scala-cli: paste-able snippets only (`redirect_mill_manual_snippet`, `redirect_scala_cli_manual_snippet`) | | NuGet (`nuget`) | ✅ in-place patching deletes `.nupkg.metadata` and advises on the `.nupkg.sha512` tamper-evidence sidecar — prefer vendored / hosted, see [Maven & NuGet caveats](#maven--nuget-caveats) | ✅ committed folder feed (its `.gitignore` re-includes the nupkg against `*.nupkg` rules) + `packageSourceMapping` + `packages.lock.json` contentHash pin | ✅ `nuget.config` source + source-mapping, `packages.lock.json` contentHash rewrite. See the locked-mode note in [Maven & NuGet caveats](#maven--nuget-caveats) | -| Composer (`composer`) | ✅ in place (`vendor/`) | ✅ `composer.lock` `dist: path` rewrite | ✅ `composer.lock` dist url + shasum rewrite; the entry's `source` and `dist.mirrors` are removed. See [composer-compatibility.md](testing/composer-compatibility.md) | +| Composer (`composer`) | ✅ in place (`vendor/`) | ✅ `composer.lock` `dist: path` rewrite; declined in a Bazel workspace, see [Bazel workspaces: Composer](#bazel-workspaces-composer) | ✅ `composer.lock` dist url + shasum rewrite; the entry's `source` and `dist.mirrors` are removed. See [composer-compatibility.md](testing/composer-compatibility.md) | | Deno (`deno`) | ✅ in place (the only mode for Deno) | ❌ refused (`vendor_unsupported_ecosystem`) | ❌ not supported | > **Maven / NuGet sidecar caveat**: Maven and NuGet are fully enabled in every mode. @@ -2615,6 +2615,76 @@ Important limits: The implemented hosted shape and fresh-checkout behavior are exercised by [`e2e_golang_hosted_build.rs`](../crates/socket-patch-cli/tests/e2e_golang_hosted_build.rs). +## Bazel workspaces: Composer + +No Bazel ruleset builds PHP or takes Composer packages, so a PHP project in a Bazel workspace +brings its packages into the build with a few lines of its own: a repository rule or a genrule +that runs `composer install` from the committed `composer.lock`, or a committed `vendor/` tree +that Bazel reads as source files. Hosted mode patches the first two when Bazel runs the rule +or the action again: Composer runs on the lock that socket-patch rewrote and fetches the +patched archive. For the third, see vendored mode with the flag below. Agent mode is not +supported in a Bazel workspace. + +Vendored mode is declined there by default. It makes `composer.lock` name the patched copy by +a path relative to the directory Composer runs in +(`.socket/vendor/composer///@`). A rule or a build action runs +Composer in a directory of Bazel's that holds `composer.json` and `composer.lock` and nothing +else, so Composer fails with `Source path "..." is not found for package ...`, and the build +fails with it, after a command that exited 0 (Bazel 9.3.2, 8.8.2 and 7.7.2). What a workspace's +rules do with the lock cannot be read from its files: a rule may take the lock through a glob, +or live in another module, and a committed `vendor/` tree does not show that no rule runs +Composer somewhere else as well. So socket-patch does not try to. + +A project is in a Bazel workspace by the lookup of +[Bazel workspaces: `.bazelignore`](#bazel-workspaces-bazelignore): its directory, or one above +it up to the root of its git checkout, holds `MODULE.bazel`, `REPO.bazel`, `WORKSPACE` or +`WORKSPACE.bazel`. There, every command that would vendor a Composer package reports the +package `failed` with `vendor_composer_bazel_workspace` and exits 1: `scan --mode vendored`, +`get --mode vendored` and `vendor`. It writes nothing and downloads no patch, and a +package that is pinned hosted stays pinned. With `--dry-run`, `scan` and `get` list the package +as one the run would refuse and exit 0; `vendor --dry-run` reports the refusal and exits 1, as +it does for any package it could not vendor. The message names the Bazel root and the mode +that writes no such path: `--mode hosted`. + +Vendored mode goes on when you say so, with `--allow-vendored-in-bazel` or +`SOCKET_ALLOW_VENDORED_IN_BAZEL=1`. That is for a project where Composer only runs in the +project's directory, or where the build finds the copy all the same: + +- A project that Bazel does not build: a Dockerfile runs `composer install` in it. +- A project that commits its `vendor/` tree, when no rule of the workspace installs from the + lock as well. The build is the patched one after your next `composer install` in the + project, which copies the package from `.socket/vendor`, as in a project without Bazel. +- A project whose rule brings the project's `.socket` directory to where it runs Composer. + +socket-patch checks none of these. The switch is not Composer's alone: it lets vendored mode +write for any package it would decline because the project is in a Bazel workspace, where the +build may not find the vendored copy. The Composer refusal is the only such refusal today. + +`vendor --revert` is never declined. Neither is a package that is already vendored: where +`composer.lock` already names the copy of the same patch, a vendored run leaves the lock as it +is. So a project that was vendored with the flag, or by an earlier release, keeps passing its +runs without the flag, a `scan` or `get` with no `--mode` included, which runs in vendored mode +there. A new patch for that package, or another package, is declined until the run has the +flag. + +Limits: + +- A project in a Bazel workspace that Bazel does not build, and one that commits its `vendor/` + tree, are declined like any other until they have the flag. +- A Bazel root that the lookup does not reach is not seen, and vendored mode goes on there as + before: one above the git checkout (a project that is a checkout of its own, such as a + submodule, in a Bazel repository), one above a project that is in no checkout or whose + checkout's `.git` belongs to another user, and the home directory itself for a project + below it. +- Hosted mode, which the message names, changes `composer.lock` and nothing else. It patches + the build when Bazel runs Composer on that lock again. A repository rule that links the lock + with `rctx.symlink` is not run again on Bazel 8 and 9 until its repository is fetched again, + and a workspace that takes the package without the lock (an `http_archive` of its archive) + never reads it: there the hosted run exits 0 and the build stays the unpatched one. + +[`e2e_bazel_build/composer.rs`](../crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs) +runs these flows on a real Bazel. + ## Ruby gems: hosted mode `scan --mode hosted` makes Bundler install a patched gem from that patch's own gem source on