Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/bazel-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ name: Bazel patch compatibility (npm)

# The npm flows of the real-Bazel tests (`npm::` in `e2e_bazel_build`):
# hosted and vendored mode for a package that aspect_rules_js links from a
# pnpm lock, agent mode for the copy of it in `node_modules`, and hosted
# pnpm lock (hosted mode with the lock routed through the patch's registry,
# pinned beside `trustLockfile: true`, and pinned beside a `patches` entry
# of the hub), agent mode for the copy of it in `node_modules`, and hosted
# mode on a hub that keeps the lock up to date with a pnpm of its own (a
# pnpm 11, and the pnpm 10 that rules_js picks), on each Bazel release
# below. bazel-compatibility.yml holds the legs and what
# every ecosystem shares; this file says when the npm legs run and on which
# releases.
# pnpm 11, and the pnpm 10 that rules_js picks), that hub also for a patch
# whose registry the patch service offers and for a scan that is told not
# to write `trustLockfile: true`, on each Bazel release below.
# bazel-compatibility.yml holds the legs and what every ecosystem shares;
# this file says when the npm legs run and on which releases.
#
# Beyond what every leg needs, an npm leg downloads Node from nodejs.org,
# ruleset archives from codeload.github.com and, for the hubs that keep
Expand Down
10 changes: 5 additions & 5 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

825 changes: 745 additions & 80 deletions crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2807,19 +2807,22 @@ async fn pnpm11_registry_route_reaches_a_newer_release_in_range() {
}

/// A PROJECT ROUTED BEFORE ITS BAZEL WORKSPACE GOT A RULES_JS HUB, with a
/// real pnpm. Hosted mode does not route a lock that a hub reads, so the
/// next scan has to settle the project one of two ways, and a frozen
/// install has to pass afterwards wherever pnpm reads the workspace file:
/// real pnpm. Hosted mode routes a lock that a hub reads only where the
/// build of that hub takes a routed lock, so under any other hub the next
/// scan has to settle the project one of two ways, and a frozen install
/// has to pass afterwards wherever pnpm reads the workspace file:
///
/// * the hub keeps the lock up to date with a pnpm 11 that does not see
/// pnpm-workspace.yaml: the scan leaves the lock to the hub
/// (`redirect_pnpm_bazel_workspace_hidden`) and the route with it, every
/// byte of the lock and of the workspace file as it was, so pnpm still
/// installs the patched release through its registry;
/// * the hub only reads the lock: the scan leaves the route whole, the
/// registry and the override out of the workspace file, the `overrides:`
/// record and the registry keys out of the lock, in one run, and pins the
/// release on `trustLockfile`, which pnpm installs frozen and patched.
/// * the hub keeps the lock up to date with a pnpm 10, which reads no
/// registry of the workspace file it is shown: the scan leaves the route
/// whole, the registry and the override out of the workspace file, the
/// `overrides:` record and the registry keys out of the lock, in one
/// run, and pins the release on `trustLockfile`, which pnpm installs
/// frozen and patched.
async fn registry_route_is_left_for_a_bazel_hub_added_later(pm: &str, tag: &str) {
if !has_corepack_pm(pm) {
println!("SKIP {tag}: `corepack {pm}` unavailable");
Expand Down Expand Up @@ -2965,8 +2968,10 @@ async fn registry_route_is_left_for_a_bazel_hub_added_later(pm: &str, tag: &str)
"{tag}: the routed release still installs patched"
);

// ── a hub that reads the lock ──
std::fs::write(proj.join("MODULE.bazel"), hub("")).unwrap();
// ── a hub whose pnpm reads no registry of the workspace file ──
let listing = ", update_pnpm_lock = True, data = [\"//:pnpm-workspace.yaml\"]";
let older = hub(listing).replace("11.28.10", "10.34.9");
std::fs::write(proj.join("MODULE.bazel"), older).unwrap();
let (code, stdout, stderr) = run_hosted(HostedDriver::Scan, &proj, &server.uri(), &[]);
assert_eq!(code, 0, "{tag}: scan failed.\n{stdout}\n{stderr}");
let env = parse_envelope(&stdout);
Expand Down
24 changes: 15 additions & 9 deletions crates/socket-patch-cli/tests/hosted_memory_bazel_off.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ struct Project {
/// A gem lock beside a rules_ruby hub that holds the gem to its upstream
/// sha256, a pnpm lock beside a rules_js hub that hides
/// `pnpm-workspace.yaml` from pnpm 11, and a pnpm lock that a rules_js hub
/// reads, in a project that proves pnpm 12 and is offered the patch's
/// registry. Each starts its patch API: a test holds the variable before
/// it asks for them.
/// reads in a call that no routed lock was built with (it has a `patches`
/// entry of its own), in a project that proves pnpm 12 and is offered the
/// patch's registry. Each starts its patch API: a test holds the variable
/// before it asks for them.
async fn projects() -> Vec<Project> {
let ruby = format!(
"bazel_dep(name = \"rules_ruby\", version = \"1.0.0-rc2\")\n\
Expand All @@ -129,11 +130,15 @@ async fn projects() -> Vec<Project> {
pnpm_lock = \"//:pnpm-lock.yaml\",\n update_pnpm_lock = True,\n)\n\
pnpm = use_extension(\"@aspect_rules_js//npm:extensions.bzl\", \"pnpm\")\n\
pnpm.pnpm(name = \"pnpm\", pnpm_version = \"11.28.10\")\n";
// A hub that only reads the lock: its call is on line 4.
// A hub that reads the lock and patches the package itself, which
// keeps the lock off the route (one that only reads the lock is routed
// as without Bazel): its call is on line 4.
let reads = "module(name = \"app\")\n\
bazel_dep(name = \"aspect_rules_js\", version = \"3.6.0\")\n\
npm = use_extension(\"@aspect_rules_js//npm:extensions.bzl\", \"npm\")\n\
npm.npm_translate_lock(name = \"npm\", pnpm_lock = \"//:pnpm-lock.yaml\")\n";
npm.npm_translate_lock(\n name = \"npm\",\n \
pnpm_lock = \"//:pnpm-lock.yaml\",\n \
patches = {\"left-pad@1.3.0\": [\"//:own.patch\"]},\n)\n";
let own: [(&str, &[u8]); 2] = [
("pnpm-workspace.yaml", b"# own\npackages:\n - '.'\n"),
("package.json", br#"{ "name": "root", "private": true }"#),
Expand Down Expand Up @@ -203,10 +208,11 @@ async fn projects() -> Vec<Project> {
/// Without the variable, whatever the shell of the test run exports: the
/// engine leaves the gem lock and the pnpm lock of the hub that hides
/// `pnpm-workspace.yaml` as they are and says why; the lock that a hub
/// only reads is pinned and not routed through the patch's registry, and
/// the engine says that the hub is why. Its selection asks for the
/// `MODULE.bazel` of each, and for every file of a Bazel workspace beside
/// a pnpm lock, a `pnpm-workspace.yaml` or a gem lock.
/// reads in a call no routed lock was built with is pinned and not routed
/// through the patch's registry, and the engine says that the hub is why.
/// Its selection asks for the `MODULE.bazel` of each, and for every file
/// of a Bazel workspace beside a pnpm lock, a `pnpm-workspace.yaml` or a
/// gem lock.
#[tokio::test]
#[serial_test::serial]
async fn a_hub_keeps_the_engine_from_pinning_or_routing() {
Expand Down
103 changes: 102 additions & 1 deletion crates/socket-patch-cli/tests/hosted_memory_parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,10 +1450,30 @@ async fn assert_pnpm_workspace_parity_with_roots(
files: &BTreeMap<String, Vec<u8>>,
trust_lockfile_config: bool,
roots: &[&str],
) -> (Value, BTreeMap<String, Vec<u8>>) {
assert_pnpm_workspace_parity_granted(files, trust_lockfile_config, roots, false).await
}

/// [`assert_pnpm_workspace_parity_with_roots`]. With `registry`, the patch
/// service also offers each patch's own registry: the directory its
/// tarball is served from, which hosted mode can route a pnpm lock through.
async fn assert_pnpm_workspace_parity_granted(
files: &BTreeMap<String, Vec<u8>>,
trust_lockfile_config: bool,
roots: &[&str],
registry: bool,
) -> (Value, BTreeMap<String, Vec<u8>>) {
let dir = fixtures_root().join("redirect/npm/pnpm/basic");
let server = MockServer::start().await;
let patches = patches_from_overrides(&dir.join("overrides.json"), Some(&server.uri()));
let mut patches = patches_from_overrides(&dir.join("overrides.json"), Some(&server.uri()));
for patch in patches.iter_mut().filter(|_| registry) {
let url = patch.reference["url"]
.as_str()
.expect("a tarball")
.to_string();
let directory = &url[..=url.rfind('/').expect("the URL of a tarball")];
patch.reference["npmRegistryUrl"] = directory.into();
}
mount_api(&server, &patches).await;
let extra: &[&str] = if trust_lockfile_config {
&[]
Expand Down Expand Up @@ -1653,6 +1673,87 @@ async fn parity_pnpm_bazel_hub_that_hides_the_workspace_file() {
);
}

/// A rules_js hub that reads the pnpm lock, a patch whose registry the
/// patch service offers and a project that proves pnpm 12: both engines
/// give the one answer. A hub that runs no pnpm, and one whose pnpm 11
/// reads the user's `pnpm-workspace.yaml`, have the lock routed through
/// the registry. The same hub with a pnpm 10 keeps the pin with
/// `trustLockfile: true`, and the warning says what kept it off the
/// route. Told not to write to the workspace file, both engines write
/// nothing beside the hub whose pnpm 11 would reject the pin, and say what
/// to do.
#[tokio::test]
async fn parity_pnpm_bazel_hub_and_the_registry_route() {
let lock =
String::from_utf8(read_fixture("redirect/npm/pnpm/basic/input/pnpm-lock.yaml")).unwrap();
let module = |hub: &str, pnpm: &str| {
format!(
"bazel_dep(name = \"aspect_rules_js\", version = \"3.6.0\")\n\
npm = use_extension(\"@aspect_rules_js//npm:extensions.bzl\", \"npm\")\n\
npm.npm_translate_lock(pnpm_lock = \"//:pnpm-lock.yaml\"{hub})\n\
pnpm = use_extension(\"@aspect_rules_js//npm:extensions.bzl\", \"pnpm\")\n\
pnpm.pnpm(name = \"pnpm\", pnpm_version = \"{pnpm}\")\n"
)
};
let listing = ", update_pnpm_lock = True, data = [\"//:pnpm-workspace.yaml\"]";
let manifest = r#"{ "name": "root", "private": true, "packageManager": "pnpm@12.22.0" }"#;
let run = |module: String, trust: bool| {
let lock = lock.clone();
async move {
let extra = [
("MODULE.bazel", module.as_str()),
("package.json", manifest),
];
let files = pnpm_workspace("# own\n", Some(&lock), &[], &extra);
assert_pnpm_workspace_parity_granted(&files, trust, &[""], true).await
}
};
let written = ["pnpm-lock.yaml", "pnpm-workspace.yaml"];
let workspace = |changed: &BTreeMap<String, Vec<u8>>| {
String::from_utf8(changed["pnpm-workspace.yaml"].clone()).unwrap()
};

for hub in ["", listing] {
let (redirect, changed) = run(module(hub, "11.28.10"), true).await;
assert_eq!(changed.keys().collect::<Vec<_>>(), written, "{hub}");
let workspace = workspace(&changed);
assert!(
workspace.contains("namedRegistries:") && !workspace.contains("trustLockfile"),
"{hub}: {workspace}"
);
assert!(
!redirect["warnings"].to_string().contains("Bazel"),
"{hub}: {redirect:#}"
);
}

let (redirect, changed) = run(module(listing, "10.34.9"), true).await;
assert_eq!(changed.keys().collect::<Vec<_>>(), written);
assert_eq!(
workspace(&changed),
"# own\npackages:\n - '.'\ntrustLockfile: true\n"
);
assert!(
redirect["warnings"].to_string().contains(
"a Bazel hub reads the pnpm lock (MODULE.bazel:3), and the pnpm it runs for \
`update_pnpm_lock` is not stated as 11.20 or later (pnpm 10.34.9)"
),
"{redirect:#}"
);

let (redirect, changed) = run(module(listing, "11.28.10"), false).await;
assert!(changed.is_empty(), "{}", describe(&changed));
let warned = redirect["warnings"].to_string();
assert!(
warned.contains("redirect_pnpm_bazel_workspace_hidden")
&& warned.contains(
"let hosted mode write to pnpm-workspace.yaml (this run was told not to, which \
is `--no-trust-lockfile-config` on the command line"
),
"{redirect:#}"
);
}

/// #492: a member lock beside a shared root lock (one listing the member's
/// importer) is a stale leftover pnpm never reads: both engines pin the
/// root lock alone.
Expand Down
83 changes: 79 additions & 4 deletions crates/socket-patch-cli/tests/in_process_pnpm_registry_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,11 @@ const LISTING_HUB: &str = "bazel_dep(name = \"aspect_rules_js\", version = \"3.6
/// the lock, with none, and with nothing crawled at all, it rewrites
/// neither the lock nor pnpm-workspace.yaml, and says why. Before, only
/// the first left the files: the other two took the unused entries out, and
/// with the last of them the file that the hub's `data` names.
/// with the last of them the file that the hub's `data` names. The last
/// hub lists a file that holds a line of the user's own, which is left to
/// the hub by a scan that is told not to write to it
/// (`--no-trust-lockfile-config`): the pnpm of the hub would reject a pin
/// that such a scan adds.
#[tokio::test]
async fn a_hosted_scan_leaves_a_lock_to_its_bazel_hub_whatever_it_is_granted() {
let granting = MockServer::start().await;
Expand All @@ -1716,20 +1720,29 @@ async fn a_hosted_scan_leaves_a_lock_to_its_bazel_hub_whatever_it_is_granted() {
.iter()
.any(|w| w["code"] == "redirect_pnpm_bazel_workspace_hidden")
};
for hub in [HIDING_HUB, LISTING_HUB] {
let told: &[&str] = &["--no-trust-lockfile-config"];
for (hub, extra) in [
(HIDING_HUB, &[][..]),
(LISTING_HUB, &[]),
(LISTING_HUB, told),
] {
for (granted, server, installed) in [
("a patch", &granting, true),
("no patch", &empty, true),
("nothing crawled", &empty, false),
] {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let (workspace, lock_text) = departed_project(root, "removed");
let (mut workspace, lock_text) = departed_project(root, "removed");
if !extra.is_empty() {
workspace.insert_str(0, "# the project's own\n");
std::fs::write(root.join("pnpm-workspace.yaml"), &workspace).unwrap();
}
if !installed {
std::fs::remove_dir_all(root.join("node_modules")).unwrap();
}
std::fs::write(root.join("MODULE.bazel"), hub).unwrap();
let (code, doc) = scan_hosted(root, server, &[]);
let (code, doc) = scan_hosted(root, server, extra);
assert_eq!(code, Some(0), "{granted}: {doc}");
assert_eq!(entries(&doc, "removed"), [], "{granted}: {doc}");
assert_unchanged(root, &workspace, &lock_text, granted);
Expand Down Expand Up @@ -1795,6 +1808,68 @@ async fn a_hub_of_a_root_lock_that_is_not_there_stands_against_no_cleanup() {
}
}

/// A scan that crawls nothing adds no pin: what it does beside a hub is
/// to take out what a route left and no lock uses, in the lock and in
/// pnpm-workspace.yaml together. So it does that also where it is told not
/// to write the setting (`--no-trust-lockfile-config`) beside a hub that
/// lists a file of the user's own: the pnpm of that hub would reject a pin
/// such a scan adds, and this one adds none. The lock it leaves is the
/// one without the record, byte for byte, and nothing is said of Bazel.
/// A hub that hides the file is still left its lock, as a scan that crawls
/// a package leaves it.
#[tokio::test]
async fn a_scan_that_crawls_nothing_tidies_beside_a_listing_hub_also_under_the_option() {
const BARE: &str = "lockfileVersion: '9.0'\n\nimporters:\n\n .: {}\n";
const OWN: &str = "# the project's own\n";
let server = MockServer::start().await;
mock_api(&server, &[], &[]).await;
let record = "overrides:\n leaf@1.0.0: socket-11111111:1.0.0\n\nimporters:\n";
let routed = BARE.replace("importers:\n", record);
let told: &[&str] = &["--no-trust-lockfile-config"];
for (case, hub, extra, tidied) in [
("told", LISTING_HUB, told, true),
("not told", LISTING_HUB, &[][..], true),
("hidden, told", HIDING_HUB, told, false),
("hidden", HIDING_HUB, &[], false),
] {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
for (rel, text) in [
("package.json", r#"{"name":"app","private":true}"#),
("pnpm-workspace.yaml", OWN),
("pnpm-lock.yaml", routed.as_str()),
("MODULE.bazel", hub),
] {
std::fs::write(root.join(rel), text).unwrap();
}
let (code, doc) = scan_hosted(root, &server, extra);
assert_eq!(code, Some(0), "{case}: {doc}");
assert_eq!(doc["scannedPackages"].as_u64(), Some(0), "{case}: {doc}");
let warned = doc["warnings"].to_string();
assert_eq!(
warned.contains("redirect_pnpm_bazel_workspace_hidden"),
!tidied,
"{case}: {doc}"
);
let removed = match tidied {
true => vec![entry("pnpm-lock.yaml", "lockOverride", "leaf@1.0.0")],
false => Vec::new(),
};
assert_eq!(entries(&doc, "removed"), removed, "{case}: {doc}");
let kept = if tidied { BARE } else { routed.as_str() };
assert_eq!(
read(root, "pnpm-lock.yaml").as_deref(),
Some(kept),
"{case}: {doc}"
);
assert_eq!(
read(root, "pnpm-workspace.yaml").as_deref(),
Some(OWN),
"{case}"
);
}
}

// ── what a person is told ────────────────────────────────────────────────

/// (4) Without `--json` each removal is a line, in `repair` and in a
Expand Down
Loading
Loading