diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 5e266c294..4087f3e87 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -894,7 +894,7 @@ Recognition rules that hold for every ecosystem: | Hosted: a discovered patch-host reference (or a live pre-v5 redirect-ledger record) | The installed copies the build **consumes** through the hosted wiring are hash-verified when any exist: the Go replacement module, never the pristine `M@v` in the module cache; the Socket-registry cargo source dir; maven's suffixed version. Installed evidence wins: `hash_mismatch` / `not_applied` are omitted. With **nothing installed**, a discovered reference whose lock pins the artifact (or whose format's rewriter never writes a pin) attests from that pin, which is the same evidence as in-run `scan --mode hosted --vex`. A pre-v5 ledger-only record, or a reference whose required pin is missing, stays `package_not_found`. So do purls that `--ecosystems` kept out of the crawl, because "not installed" has to mean the crawler looked. The same goes for npm purls when an installed pnpm tree records its virtual store outside the project (`enableGlobalVirtualStore`, or a `virtualStoreDir` that climbs out): transitive deps there are invisible to the crawler. A pnpm `modulesDir` inside the project is crawled. A Composer purl is omitted as `composer_install_unread` instead when `composer.json`'s `config.vendor-dir` puts the install outside the project (an absolute path, `~/…`, `$VAR/…`, or one that climbs out) and an install is there (its `composer/installed.json` exists, or the directory cannot be resolved): the crawler reads no tree outside the project that only a project file names, so that install was not checked and may predate the redirect (#686). While nothing is installed there the pin still attests, and with `COMPOSER_VENDOR_DIR` set to the directory the crawler reads it and the installed bytes decide. A yarn Plug'n'Play project (`.pnp.cjs` / `.pnp.js`) has no tree to crawl: an npm purl there attests from the lock's pin only when the PnP loader itself resolves it through the patch (berry names the hosted url, yarn 1 the resolved url's `#` in its cache folder). A loader written before the lock was rewired runs the registry copy, so the purl stays omitted until `yarn install` rewrites it (#519). | `(redirected)` | | Agent: a manifest record with no live hosted/vendored wiring | The installed tree, unchanged. **Every** installed copy the crawler finds for the purl (npm nests duplicates of one `name@version`; pnpm, vlt, Bun and Deno stores add peer-variant copies and copies bundled inside other packages) must hash to the patched bytes, as `apply` patches every copy (Maven: every copy a build consumes, Gradle hash dirs included — see [Gradle builds](#gradle-builds-v50)). One unpatched copy omits the purl with that copy's tag (`not_applied` / `hash_mismatch`). | none | -**Unlinked store entries.** In every row an installed copy is one the install can load. Bun never prunes `node_modules/.bun`, and pnpm keeps a `node_modules/.pnpm` entry it no longer uses (pnpm 7–11 for up to 7 days, pnpm 12 until `pnpm prune`). After the in-place install that a hosted or vendored scan asks for, the project links a new entry (on pnpm, `@socket-+` for a release routed through the registry of its patch and `@file+…` for a vendored tarball, `file+…` on pnpm 8 and earlier), and the upstream `@` entry stays in the store, pristine. An entry that no link leads into is not an installed copy: `vex` and `apply --check` do not hash it, so it neither omits a patch as `not_applied` nor warns `vendored_tree_out_of_sync`. For pnpm this applies to a package version only when its copies sit in two or more entries of one store and do not hold the same bytes in each; a version in one entry is hashed as before, linked or not, and so are copies that hold the same bytes (the peer variants of a package), since leaving one out would change no verdict; copies of more than 64 MB or 50,000 files are not compared and count as holding other bytes. For a version in two entries, every symbolic link is read below the scanned directory (or below the project that holds the store, when the scan started inside it), in every directory whatever its name; nothing above that directory is read for its own sake. A link to a directory outside it is followed: that directory, and each `node_modules` in a directory above it (up to the home directory, which is not read), are read the same way and once each, so a `link:` dependency on another checkout and Bazel's `bazel-*` links are read; a directory above the scanned one is read only this way. A link that leads nowhere (no target, a loop, a path through a file) is left out. An entry is hashed when a link outside the store's entries resolves into it, or a link inside a hashed entry does; the entries left out are those that only other left-out entries link, and only beside a hashed entry of the same version: when none of a version's entries is linked, all are hashed as before (a hosted scan leaves such a pair when the patched package is a peer dependency: the dependent gets a new entry, and its old entry keeps its link to the old one). A copy in the store but in none of its entries is always hashed. Only links are read, not a path to an entry that a file spells out or `NODE_PATH` names. Every store is judged once per run, by the same links for the record check and the hosted one. Where a link may have gone unread, every entry is hashed: on Windows; when the scanned directory and the store's project are unrelated (`--global`, a `node_modules` linked from another checkout), or the store's project lies above the git checkout the run started in or, above the scanned directory, is the home directory or above it; under `node-linker=hoisted`, `shamefully-hoist` or a moved virtual store; when the install record or the lockfile in the store is missing or unreadable, `pnpm-lock.yaml` is unreadable, or either lists its importers in a way pnpm does not write; when a lockfile names an importer that is not a directory inside the workspace's root (under a `modulesDir` of more than one level, the directory whose `modulesDir` setting names the store's `node_modules`); when a directory or a link cannot be read, or all that is read comes to more than 500,000 directories and links; when a link resolves to the home directory or one above it, to a file outside what was read that has a `node_modules` in a directory above it (the home directory and above are not looked at), to the store itself, or into the store by a path that names none of its entries (another spelling of the store's path or of an entry's name); and when no link resolves into any entry (`docs/ecosystems.md`, "npm: which `node_modules` trees are crawled", has the list). `rollback` and `remove` restore unlinked entries too. +**Unlinked store entries.** In every row an installed copy is one the install can load. Bun never prunes `node_modules/.bun`, and pnpm keeps a `node_modules/.pnpm` entry it no longer uses (pnpm 7–11 for up to 7 days, pnpm 12 until `pnpm prune`). After the in-place install that a hosted or vendored scan asks for, the project links a new entry (on pnpm, `@socket-+` for a release routed through the registry of its patch and `@file+…` for a vendored tarball, `file+…` on pnpm 8 and earlier), and the upstream `@` entry stays in the store, pristine. An entry that no link leads into is not an installed copy: `vex` and `apply --check` do not hash it, so it neither omits a patch as `not_applied` nor warns `vendored_tree_out_of_sync`. For pnpm this applies to a package version only when its copies sit in two or more entries of one store and do not hold the same bytes in each; a version in one entry is hashed as before, linked or not, and so are copies that hold the same bytes (the peer variants of a package), since leaving one out would change no verdict; copies of more than 64 MB or 50,000 files are not compared and count as holding other bytes. For a version in two entries, every symbolic link (on Windows, every junction as well) is read below the scanned directory (or below the project that holds the store, when the scan started inside it), in every directory whatever its name; nothing above that directory is read for its own sake. A link to a directory outside it is followed: that directory, and each `node_modules` in a directory above it (up to the home directory, which is not read), are read the same way and once each, so a `link:` dependency on another checkout and Bazel's `bazel-*` links are read; a directory above the scanned one is read only this way. On Windows a directory outside is not read, and a link to one keeps every entry. A link that leads nowhere (no target, a loop, a path through a file) is left out. An entry is hashed when a link outside the store's entries resolves into it, or a link inside a hashed entry does; the entries left out are those that only other left-out entries link, and only beside a hashed entry of the same version: when none of a version's entries is linked, all are hashed as before (a hosted scan leaves such a pair when the patched package is a peer dependency: the dependent gets a new entry, and its old entry keeps its link to the old one). A copy in the store but in none of its entries is always hashed. Only links are read, not a path to an entry that a file spells out or `NODE_PATH` names. Every store is judged once per run, by the same links for the record check and the hosted one. Where a link may have gone unread, every entry is hashed: when the scanned directory and the store's project are unrelated (`--global`, a `node_modules` linked from another checkout), or the store's project lies above the git checkout the run started in or, above the scanned directory, is the home directory or above it; under `node-linker=hoisted`, `shamefully-hoist` or a moved virtual store; when the install record or the lockfile in the store is missing or unreadable, `pnpm-lock.yaml` is unreadable, or either lists its importers in a way pnpm does not write; when a lockfile names an importer that is not a directory inside the workspace's root (under a `modulesDir` of more than one level, the directory whose `modulesDir` setting names the store's `node_modules`); when a directory or a link cannot be read, or all that is read comes to more than 500,000 directories and links; when a link resolves to the home directory or one above it, on Windows to any directory outside the one that was read, to a file outside what was read that has a `node_modules` in a directory above it (the home directory and above are not looked at), to the store itself, or into the store by a path that names none of its entries (another spelling of the store's path or of an entry's name); and when no link resolves into any entry (`docs/ecosystems.md`, "npm: which `node_modules` trees are crawled", has the list). `rollback` and `remove` restore unlinked entries too. **Liveness gates.** These gates run before hashing, and `--no-verify` / `--vex-no-verify` skips only the hashing, never the gates: diff --git a/crates/socket-patch-cli/tests/common/pnpm_links.rs b/crates/socket-patch-cli/tests/common/pnpm_links.rs index 66a3ffe99..c93827541 100644 --- a/crates/socket-patch-cli/tests/common/pnpm_links.rs +++ b/crates/socket-patch-cli/tests/common/pnpm_links.rs @@ -1,8 +1,9 @@ //! An independent reading of which entries of a pnpm virtual store an //! install can load (#2353), for the real-pnpm suites to hold `vex` //! against. It shares no code with the crates under test: its own walk of -//! the tree and its own reading of every link. Reached as -//! `common::pnpm_links`, or on its own with +//! the tree and its own reading of every link. [`link_dir`] and +//! [`unlink`] add and remove a link for those suites to move the reading +//! with. Reached as `common::pnpm_links`, or on its own with //! `#[path = "common/pnpm_links.rs"] mod pnpm_links;`. #![allow(dead_code)] @@ -107,11 +108,49 @@ pub fn reachable_store_entries(tree: &Path, store: &Path) -> BTreeSet { } } +/// A link at `link` to the directory `target`, in a directory made for +/// it: a symbolic link on Unix, a junction on Windows, which takes no +/// privilege there. +pub fn link_dir(target: &Path, link: &Path) { + std::fs::create_dir_all(link.parent().unwrap()).unwrap(); + #[cfg(unix)] + std::os::unix::fs::symlink(target, link).unwrap(); + #[cfg(windows)] + { + // `mklink` reads a `/` in a path as a switch, and does not take + // the `\\?\` that `canonicalize` puts before a drive letter. + let spelled = |path: &Path| -> PathBuf { + let path: PathBuf = path.components().collect(); + let text = path.to_str().unwrap(); + match text.strip_prefix(r"\\?\") { + Some(rest) if rest.as_bytes().get(1) == Some(&b':') => PathBuf::from(rest), + _ => PathBuf::from(text), + } + }; + let made = std::process::Command::new("cmd") + .args(["/C", "mklink", "/J"]) + .arg(spelled(link)) + .arg(spelled(target)) + .output() + .unwrap(); + assert!(made.status.success(), "mklink /J {link:?}: {made:?}"); + } +} + +/// Take the link at `link` away, and nothing it leads to. Windows removes +/// a link to a directory as a directory. +pub fn unlink(link: &Path) { + std::fs::remove_file(link) + .or_else(|_| std::fs::remove_dir(link)) + .unwrap_or_else(|e| panic!("{link:?}: {e}")); +} + /// The regular files below `tree` that spell out the directory of one of -/// the entries `entries` of the virtual store `store` (`/`), read -/// in every real directory but `.git` and those entries themselves. pnpm -/// writes such a path into the launcher of a package's command -/// (`node_modules/.bin`), where no link shows it. +/// the entries `entries` of the virtual store `store` (`/`, and +/// `\` as Windows writes a path), read in every real directory +/// but `.git` and those entries themselves. pnpm writes such a path into +/// the launcher of a package's command (`node_modules/.bin`), where no +/// link shows it. pub fn files_naming_store_entries( tree: &Path, store: &Path, @@ -121,7 +160,8 @@ pub fn files_naming_store_entries( let store = std::fs::canonicalize(store).unwrap(); let needles: Vec> = entries .iter() - .map(|entry| format!("{entry}/").into_bytes()) + .flat_map(|entry| [format!("{entry}/"), format!("{entry}\\")]) + .map(String::into_bytes) .collect(); let mut naming = Vec::new(); let mut dirs = vec![tree.clone()]; diff --git a/crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs b/crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs index 761614d5a..79a200eef 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs @@ -1942,10 +1942,7 @@ async fn registry_route_survives_resolution_and_cleans_up(pm: &str, tag: &str) { // entries in the store with nothing linking to them. `vex` judges the // copies the install links: both patches attest, and a linked copy // put back on the upstream bytes is refused again. - // - // pnpm links directories with junctions on Windows, which vex does not - // read as links: it judges every entry there. - if !cfg!(windows) { + { let warm = tmp.path().join("warm"); std::fs::create_dir_all(&warm).unwrap(); for file in ["package.json", ".npmrc"] { @@ -3609,7 +3606,6 @@ async fn pnpm11_registry_route_is_left_for_a_bazel_hub_added_later() { /// has one; the root then pins pnpm in `engines`, since the scan takes the /// pnpm that installed the project from `node_modules/.modules.yaml`, and /// that record is written below the `modulesDir`. -#[cfg(unix)] fn write_route_workspace(proj: &Path, registry: &MockServer, modules_dir: Option<&str>) { let put = |path: &str, bytes: &[u8]| { let path = proj.join(path); @@ -3653,7 +3649,6 @@ fn write_route_workspace(proj: &Path, registry: &MockServer, modules_dir: Option /// What `vex` says of the project at `dir`: its exit code, the advisories /// it attests with their status, and its stderr. -#[cfg(unix)] fn route_vex(dir: &Path, server: &MockServer) -> (i32, Vec<(String, String)>, String) { route_vex_env(dir, server, &[]) } @@ -3700,7 +3695,6 @@ fn route_vex_env( /// the default registry, scanned, and installed again over the same /// `node_modules`, as a scan asks. `None` when the fixture install fails /// where the toolchain is not required. -#[cfg(unix)] fn route_workspace_installed_in_place( tmp: &Path, proj: &Path, @@ -3767,7 +3761,6 @@ fn route_workspace_installed_in_place( /// directory or a real package's own `node_modules` is a path node can /// load it by. The independent reading finds it, and `vex` refuses the /// patch as `not_applied` until the link is gone. -#[cfg(unix)] async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: &str) { if !has_corepack_pm(pm) { println!("SKIP {tag}: `corepack {pm}` unavailable"); @@ -3814,14 +3807,42 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: 1, "{tag}: the old entry of the peer-dependent package keeps its link to the old leaf" ); - for (through, name) in [ - ("node_modules/peer-user", "peer-user"), - ("packages/b/node_modules/leaf", "leaf"), - ("packages/b/node_modules/needs-leaf/../leaf", "leaf"), - ("packages/a/node_modules/mid/../leaf", "leaf"), - ("node_modules/.pnpm/node_modules/leaf", "leaf"), + // What a dependent loads is the package beside it in its store entry + // (Windows folds a `..` before it follows the link, so the entry is + // resolved first). + let beside = |dependent: &str| { + let real = std::fs::canonicalize(proj.join(dependent)) + .unwrap_or_else(|e| panic!("{tag}: {dependent}: {e}")); + real.parent().unwrap().join("leaf") + }; + for (through, name, package) in [ + ( + "node_modules/peer-user", + "peer-user", + proj.join("node_modules/peer-user"), + ), + ( + "packages/b/node_modules/leaf", + "leaf", + proj.join("packages/b/node_modules/leaf"), + ), + ( + "packages/b/node_modules/needs-leaf/../leaf", + "leaf", + beside("packages/b/node_modules/needs-leaf"), + ), + ( + "packages/a/node_modules/mid/../leaf", + "leaf", + beside("packages/a/node_modules/mid"), + ), + ( + "node_modules/.pnpm/node_modules/leaf", + "leaf", + proj.join("node_modules/.pnpm/node_modules/leaf"), + ), ] { - let index = std::fs::read_to_string(proj.join(through).join("index.js")) + let index = std::fs::read_to_string(package.join("index.js")) .unwrap_or_else(|e| panic!("{tag}: {name} through {through}: {e}")); assert!( index.starts_with(MARKER), @@ -3890,8 +3911,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: host.join("node_modules/leaf"), ), ] { - std::fs::create_dir_all(link.parent().unwrap()).unwrap(); - std::os::unix::fs::symlink(&old_leaf, &link).unwrap(); + pnpm_links::link_dir(&old_leaf, &link); assert_eq!( std::fs::read_to_string(link.join("index.js")).unwrap(), route_index("leaf", false), @@ -3913,7 +3933,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: stderr.contains("leaf@1.0.0") && stderr.contains("not_applied"), "{tag}: a link from {from}: the refusal says why:\n{stderr}" ); - std::fs::remove_file(&link).unwrap(); + pnpm_links::unlink(&link); } let (code, attested, stderr) = route_vex(&proj, &server); assert_eq!(code, 0, "{tag}: vex without the links:\n{stderr}"); @@ -3922,17 +3942,36 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: "{tag}: without the links leaf is attested again: {attested:?}\n{stderr}" ); - // A link that leads out of the project is followed. A directory - // beside the project that links nothing of the store changes nothing; - // one whose `node_modules` links the upstream leaf, as a member that - // left the workspace keeps doing, refuses the patch. The test's own - // directory stands in for the home directory, so no `node_modules` - // that the machine keeps above it is read. + // A link that leads out of the project is followed (on Windows it + // keeps every entry instead, below). A directory beside the project + // that links nothing of the store changes nothing; one whose + // `node_modules` links the upstream leaf, as a member that left the + // workspace keeps doing, refuses the patch. The test's own directory + // stands in for the home directory, so no `node_modules` that the + // machine keeps above it is read. let home = [("HOME".to_string(), tmp.path().to_str().unwrap().to_string())]; let outside = tmp.path().join("shared"); std::fs::create_dir_all(outside.join("lib")).unwrap(); - std::fs::create_dir_all(proj.join("tools")).unwrap(); - std::os::unix::fs::symlink(outside.join("lib"), proj.join("tools/lib")).unwrap(); + pnpm_links::link_dir(&outside.join("lib"), &proj.join("tools/lib")); + if cfg!(windows) { + // Windows does not read what a link out of the project leads to. + // For as long as a link to a directory outside is there, every + // entry is judged, and the upstream leaf refuses its patch. + let (_, attested, stderr) = route_vex(&proj, &server); + assert!( + !attested.contains(¬_affected(leaf)) && stderr.contains("not_applied"), + "{tag}: a link to a directory outside the project must keep every entry on \ + Windows: {attested:?}\n{stderr}" + ); + pnpm_links::unlink(&proj.join("tools/lib")); + let (code, attested, stderr) = route_vex(&proj, &server); + assert_eq!(code, 0, "{tag}: vex once that link is gone:\n{stderr}"); + assert!( + attested.contains(¬_affected(leaf)), + "{tag}: without that link leaf is attested again: {attested:?}\n{stderr}" + ); + return; + } let (code, attested, stderr) = route_vex_env(&proj, &server, &home); assert_eq!( code, 0, @@ -3943,8 +3982,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: "{tag}: a link to a directory that links nothing must change nothing: \ {attested:?}\n{stderr}" ); - std::fs::create_dir_all(outside.join("node_modules")).unwrap(); - std::os::unix::fs::symlink(&old_leaf, outside.join("node_modules/leaf")).unwrap(); + pnpm_links::link_dir(&old_leaf, &outside.join("node_modules/leaf")); let reachable = pnpm_links::reachable_store_entries(&proj, &store); assert!( reachable.contains("leaf@1.0.0"), @@ -3959,7 +3997,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: "{tag}: a directory outside that links the upstream leaf must refuse its patch: \ {attested:?}\n{stderr}" ); - std::fs::remove_file(outside.join("node_modules/leaf")).unwrap(); + pnpm_links::unlink(&outside.join("node_modules/leaf")); let (code, attested, stderr) = route_vex_env(&proj, &server, &home); assert_eq!(code, 0, "{tag}: vex once the stale link is gone:\n{stderr}"); assert!( @@ -3970,7 +4008,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: // A `node_modules` there that is a link itself is one more link: into // the upstream entry's `node_modules`, it loads the upstream leaf. std::fs::remove_dir(outside.join("node_modules")).unwrap(); - std::os::unix::fs::symlink(old_leaf.parent().unwrap(), outside.join("node_modules")).unwrap(); + pnpm_links::link_dir(old_leaf.parent().unwrap(), &outside.join("node_modules")); let reachable = pnpm_links::reachable_store_entries(&proj, &store); assert!( reachable.contains("leaf@1.0.0"), @@ -3986,7 +4024,7 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: "{tag}: a node_modules outside that is a link into the upstream entry must refuse \ the patch: {attested:?}\n{stderr}" ); - std::fs::remove_file(outside.join("node_modules")).unwrap(); + pnpm_links::unlink(&outside.join("node_modules")); let (code, attested, stderr) = route_vex_env(&proj, &server, &home); assert_eq!(code, 0, "{tag}: vex once that link is gone:\n{stderr}"); assert!( @@ -3995,19 +4033,17 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag: ); } -#[cfg(unix)] #[tokio::test(flavor = "multi_thread")] #[serial_test::serial] -#[ignore = "wall-bound real-pnpm installs (~1min); runs on Linux and macOS as an e2e CI matrix leg"] +#[ignore = "wall-bound real-pnpm installs (~1min); runs on all 3 OSes as an e2e CI matrix leg"] async fn pnpm12_registry_route_in_place_install_is_judged_by_every_link() { registry_route_in_place_install_is_judged_by_every_link(PNPM_NAMED_REGISTRY, "pnpm12-in-place") .await; } -#[cfg(unix)] #[tokio::test(flavor = "multi_thread")] #[serial_test::serial] -#[ignore = "wall-bound real-pnpm installs (~1min); runs on Linux and macOS as an e2e CI matrix leg"] +#[ignore = "wall-bound real-pnpm installs (~1min); runs on all 3 OSes as an e2e CI matrix leg"] async fn pnpm11_registry_route_in_place_install_is_judged_by_every_link() { registry_route_in_place_install_is_judged_by_every_link(PNPM_TERTIARY, "pnpm11-in-place").await; } @@ -4027,7 +4063,6 @@ async fn pnpm11_registry_route_in_place_install_is_judged_by_every_link() { /// member's own modules dir refuses its patch until the link is gone. A /// run from a member finds no patch to attest: the scan's files are the /// workspace root's. -#[cfg(unix)] async fn registry_route_in_place_install_under_a_nested_modules_dir(pm: &str, tag: &str) { if !has_corepack_pm(pm) { println!("SKIP {tag}: `corepack {pm}` unavailable"); @@ -4063,7 +4098,9 @@ async fn registry_route_in_place_install_under_a_nested_modules_dir(pm: &str, ta std::fs::canonicalize(store.join(&routed).join("node_modules/leaf")).unwrap(), "{tag}: a member's links sit in its own modulesDir" ); - let current = std::fs::read_to_string(store.join("lock.yaml")).unwrap(); + let current = std::fs::read_to_string(store.join("lock.yaml")) + .unwrap() + .replace("\r\n", "\n"); assert!( current.contains("\n packages/b:\n") && !proj.join("sub/packages").exists(), "{tag}: the lockfile in the store names the members from the workspace root:\n{current}" @@ -4091,7 +4128,7 @@ async fn registry_route_in_place_install_under_a_nested_modules_dir(pm: &str, ta // to load it: the independent reading finds it, and vex refuses the // patch until the link is gone. let link = proj.join("packages/a/sub/node_modules/old-leaf"); - std::os::unix::fs::symlink(store.join("leaf@1.0.0/node_modules/leaf"), &link).unwrap(); + pnpm_links::link_dir(&store.join("leaf@1.0.0/node_modules/leaf"), &link); let reachable = pnpm_links::reachable_store_entries(&proj, &store); assert!( reachable.contains("leaf@1.0.0"), @@ -4105,7 +4142,7 @@ async fn registry_route_in_place_install_under_a_nested_modules_dir(pm: &str, ta && stderr.contains("not_applied"), "{tag}: a link from a member's modules dir must refuse leaf: {attested:?}\n{stderr}" ); - std::fs::remove_file(&link).unwrap(); + pnpm_links::unlink(&link); let (code, attested, stderr) = route_vex(&proj, &server); assert_eq!(code, 0, "{tag}: vex without the link:\n{stderr}"); assert!( @@ -4117,10 +4154,9 @@ async fn registry_route_in_place_install_under_a_nested_modules_dir(pm: &str, ta assert!(attested.is_empty(), "{tag}: {attested:?}\n{stderr}"); } -#[cfg(unix)] #[tokio::test(flavor = "multi_thread")] #[serial_test::serial] -#[ignore = "wall-bound real-pnpm installs (~1min); runs on Linux and macOS as an e2e CI matrix leg"] +#[ignore = "wall-bound real-pnpm installs (~1min); runs on all 3 OSes as an e2e CI matrix leg"] async fn pnpm12_registry_route_in_place_install_under_a_nested_modules_dir() { registry_route_in_place_install_under_a_nested_modules_dir( PNPM_NAMED_REGISTRY, @@ -4129,10 +4165,9 @@ async fn pnpm12_registry_route_in_place_install_under_a_nested_modules_dir() { .await; } -#[cfg(unix)] #[tokio::test(flavor = "multi_thread")] #[serial_test::serial] -#[ignore = "wall-bound real-pnpm installs (~1min); runs on Linux and macOS as an e2e CI matrix leg"] +#[ignore = "wall-bound real-pnpm installs (~1min); runs on all 3 OSes as an e2e CI matrix leg"] async fn pnpm11_registry_route_in_place_install_under_a_nested_modules_dir() { registry_route_in_place_install_under_a_nested_modules_dir( PNPM_TERTIARY, diff --git a/crates/socket-patch-cli/tests/e2e_vendor_pnpm_build.rs b/crates/socket-patch-cli/tests/e2e_vendor_pnpm_build.rs index af820ceb0..c60813ab3 100644 --- a/crates/socket-patch-cli/tests/e2e_vendor_pnpm_build.rs +++ b/crates/socket-patch-cli/tests/e2e_vendor_pnpm_build.rs @@ -894,8 +894,10 @@ fn assert_in_place_install_verifies( before: (&[u8], &[u8]), bytes: (&[u8], &[u8]), ) { - // pnpm links directories with junctions on Windows, which vex does not - // read as links: it judges every entry there. + // Not run on Windows yet: this suite has no leg on the Windows CI + // runner, so the steps below were never tried there. The rule itself + // runs there (the unit tests of `crawlers::npm_crawler`, and the + // registry-route legs of `e2e_redirect_pnpm_build`). if cfg!(windows) { return; } @@ -1026,9 +1028,7 @@ fn assert_in_place_install_verifies( // A link to the upstream entry is a way to load it, in whatever // directory it sits: the warning is back until the link is gone. let stray = warm.join("vendor/old"); - std::fs::create_dir_all(stray.parent().unwrap()).unwrap(); - #[cfg(unix)] - std::os::unix::fs::symlink(stale.parent().unwrap(), &stray).unwrap(); + common::pnpm_links::link_dir(stale.parent().unwrap(), &stray); assert!( reachable().contains(&upstream_entry), "{pm}: the independent reading must find the link: {:?}", @@ -1039,7 +1039,7 @@ fn assert_in_place_install_verifies( stderr.contains(TREE_OUT_OF_SYNC), "{pm}: a link in vendor/ loads the upstream entry, so it must warn:\n{stderr}" ); - std::fs::remove_file(&stray).unwrap(); + common::pnpm_links::unlink(&stray); let stderr = vex_warnings("without the link to the upstream entry"); assert!( !stderr.contains(TREE_OUT_OF_SYNC), diff --git a/crates/socket-patch-core/src/crawlers/npm_crawler.rs b/crates/socket-patch-core/src/crawlers/npm_crawler.rs index fc1133567..96000f1e8 100644 --- a/crates/socket-patch-core/src/crawlers/npm_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/npm_crawler.rs @@ -911,11 +911,11 @@ fn orphaned_pnpm_store_entry_sync( /// [`links_reached_from_sync`]): the directory behind it is read, and /// each `node_modules` above that directory, up to the home directory. /// Only so is a directory above `tree` read: when a link leads to it, or -/// to a directory below it. +/// to a directory below it. Not on Windows, where a directory outside +/// `tree` is not read. /// /// Every entry is kept wherever a link may have gone unread: /// -/// - on Windows, where pnpm links directories with junctions; /// - when neither of the two directories holds the other (a /// `node_modules` that is a link to another checkout's, a global /// install), or the directory that holds the store's `node_modules` @@ -949,6 +949,7 @@ fn orphaned_pnpm_store_entry_sync( /// to a file outside what is read that has a `node_modules` in a /// directory above it: what that file loads by paths of its own is not /// read; +/// - on Windows, when a link leads to a directory outside `tree`; /// - when a link resolves to the store itself, into it by a path that is /// spelled another way than the store's (macOS reaches a volume under /// `/System/Volumes/Data` as well, and a file system that folds case @@ -971,9 +972,6 @@ fn live_pnpm_store_entries_under_sync( use crate::utils::relpath::normalize_lexically; use crate::utils::repo_root::{upward_readable, ReadScope}; - if cfg!(windows) { - return None; - } let store = std::fs::canonicalize(store_path).ok()?; let nm = store.parent()?; let beside = nm.parent()?; @@ -1033,7 +1031,7 @@ fn live_pnpm_store_entries_under_sync( let record_names_store = |modules_dir: &Path| -> Option { let record = match read_regular_to_string_sync(&modules_dir.join(PNPM_MODULES_YAML)) { Ok(record) => record, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => return None, + Err(e) if not_there(&e) => return None, Err(_) => return Some(false), }; Some( @@ -1057,7 +1055,7 @@ fn live_pnpm_store_entries_under_sync( // Every importer of both lockfiles is a directory inside the root. let wanted = match read_regular_to_string_sync(&project.join(PNPM_LOCK)) { Ok(wanted) => Some(wanted), - Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, + Err(e) if not_there(&e) => None, Err(_) => return None, }; for lock in [Some(¤t), wanted.as_ref()].into_iter().flatten() { @@ -1095,8 +1093,8 @@ fn live_pnpm_store_entries_under_sync( // Wherever a link leads, it may lie in the store by another spelling // of the store's path: each directory above its target is held - // against the store by identity, once (nothing is opened: a target - // may be a pipe). + // against the store by identity, once (nothing is read or waited on: + // a target may be a pipe). let store_id = file_id(&store)?; let mut elsewhere: HashSet<&Path> = HashSet::new(); for link in links.iter() { @@ -1108,7 +1106,10 @@ fn live_pnpm_store_entries_under_sync( continue; } for dir in link.target.ancestors() { - if !elsewhere.insert(dir) { + // A directory above the store is not the store, and neither + // is any above that: they need not be asked (Windows opens a + // directory to tell, which a file server may not allow). + if store.starts_with(dir) || !elsewhere.insert(dir) { break; } if file_id(dir).is_none_or(|id| id == store_id) { @@ -1191,6 +1192,10 @@ fn live_pnpm_store_entries_under_sync( /// names it however its path is spelled ([`file_id`]), not by its path /// alone. `budget` holds for all the directories and links read, and for /// each `node_modules` looked for. +/// +/// On Windows a directory outside `tree` is not read: a link that leads +/// to one leaves the answer unknown, and only `tree` is read there. A +/// link to a file outside is judged there as everywhere. fn links_reached_from_sync( tree: &Path, home: Option<&Path>, @@ -1251,7 +1256,9 @@ fn links_reached_from_sync( files.push(target); continue; } - if not_read(&target) { + // Windows: a directory outside is not read there, where links + // are made and resolved another way and this was not run. + if cfg!(windows) || not_read(&target) { return None; } for dir in target.ancestors().skip(1) { @@ -1316,32 +1323,144 @@ fn links_reached_from_sync( /// is not there, a level of its path is a file, or it loops. Nothing is /// loaded through such a link, so it is left out; any other failure (a /// directory that cannot be searched) leaves the answer unknown. +/// +/// Windows is read by its own error codes, not by the kinds `std` folds +/// them into: `NotFound` also holds a drive or a network path that cannot +/// be reached, which says nothing of what is there. The codes are the +/// ones a junction of each kind fails with (measured on Windows Server +/// 2025: see `test_links_that_lead_nowhere_fail_with_the_errors_read_as_such`). fn leads_nowhere(error: &std::io::Error) -> bool { - use std::io::ErrorKind; - if matches!(error.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) { - return true; + #[cfg(windows)] + { + use windows_sys::Win32::Foundation::{ + ERROR_CANT_RESOLVE_FILENAME, ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, + }; + // No such name, no such directory on the way to it (a level that + // is a file reads the same), and a loop. + const NOWHERE: [u32; 3] = [ + ERROR_FILE_NOT_FOUND, + ERROR_PATH_NOT_FOUND, + ERROR_CANT_RESOLVE_FILENAME, + ]; + error + .raw_os_error() + .and_then(|code| u32::try_from(code).ok()) + .is_some_and(|code| NOWHERE.contains(&code)) } - #[cfg(unix)] + #[cfg(not(windows))] + { + use std::io::ErrorKind; + if matches!(error.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) { + return true; + } + #[cfg(unix)] + { + error.raw_os_error() == Some(libc::ELOOP) + } + #[cfg(not(unix))] + { + false + } + } +} + +/// Whether the link at `link`, which failed to resolve with `error`, +/// leads nowhere ([`leads_nowhere`]). +/// +/// Resolving a link opens what it leads to and then names it, and the +/// name can fail where the target is there: on Windows a volume without a +/// drive letter has no path of the kind `canonicalize` gives, and the +/// failure reads like a directory that is missing. So the target is asked +/// for by itself as well, and the link leads nowhere only when that fails +/// the same way. +/// +/// On Windows a drive letter or a share is there for one session and not +/// for another (a mapped drive is not seen by an elevated process), and a +/// link into one that is not there fails like a link to a directory that +/// is missing. What it leads to cannot be told then, so the drive or +/// share that the link's own target names must be there. +fn link_leads_nowhere(link: &Path, error: &std::io::Error) -> bool { + if !leads_nowhere(error) || !std::fs::metadata(link).is_err_and(|e| leads_nowhere(&e)) { + return false; + } + #[cfg(windows)] { - error.raw_os_error() == Some(libc::ELOOP) + use std::path::Component; + let Ok(target) = std::fs::read_link(link) else { + return false; + }; + match target.components().next() { + // A relative target is on the volume of the link itself. + Some(prefix @ Component::Prefix(_)) => { + let root: PathBuf = [prefix, Component::RootDir].into_iter().collect(); + std::fs::metadata(root).is_ok() + } + _ => true, + } } - #[cfg(not(unix))] + #[cfg(not(windows))] { - false + true } } -/// The device and inode of the file at `path`, which name one file however -/// its path is spelled. Read with `stat`, so nothing is opened. `None` -/// when it cannot be read, and on a platform without the pair. -fn file_id(path: &Path) -> Option<(u64, u64)> { +/// Whether a read failed because nothing is there by that name. An error +/// of the system is read by its own code on Windows, as [`leads_nowhere`] +/// reads one: the `NotFound` kind also holds a network path that cannot +/// be reached, which says nothing of what is there. +fn not_there(error: &std::io::Error) -> bool { + #[cfg(windows)] + if let Some(code) = error.raw_os_error() { + use windows_sys::Win32::Foundation::{ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND}; + return u32::try_from(code) + .is_ok_and(|code| code == ERROR_FILE_NOT_FOUND || code == ERROR_PATH_NOT_FOUND); + } + error.kind() == std::io::ErrorKind::NotFound +} + +/// What names one file however its path is spelled: two paths lead to one +/// file when their ids are equal. +#[derive(PartialEq)] +#[cfg_attr(not(any(unix, windows)), allow(dead_code))] +struct FileId { + /// The device and the inode. + #[cfg(unix)] + inode: (u64, u64), + /// A handle to the file. Windows names a file by the serial number + /// of its volume and its index there, which it reads from a handle + /// and keeps unchanged only for as long as one is open. + #[cfg(windows)] + handle: same_file::Handle, +} + +/// The [`FileId`] of the file at `path`; `None` when it cannot be read, +/// and on a platform that has none. Unix reads it with `stat`, so nothing +/// is opened (a link may lead to a named pipe). Windows has no `stat`: +/// the file is opened the way `std::fs::metadata` opens it there, with no +/// access to what it holds, which reads nothing and waits on nothing. +fn file_id(path: &Path) -> Option { #[cfg(unix)] { use std::os::unix::fs::MetadataExt; let meta = std::fs::metadata(path).ok()?; - Some((meta.dev(), meta.ino())) + Some(FileId { + inode: (meta.dev(), meta.ino()), + }) } - #[cfg(not(unix))] + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt; + use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS; + // The flag is what lets a directory be opened. + let file = std::fs::OpenOptions::new() + .access_mode(0) + .custom_flags(FILE_FLAG_BACKUP_SEMANTICS) + .open(path) + .ok()?; + let handle = same_file::Handle::from_file(file).ok()?; + Some(FileId { handle }) + } + #[cfg(not(any(unix, windows)))] { let _ = path; None @@ -1453,7 +1572,7 @@ fn sweep_tree_sync(tree: &Path, budget: usize, read: &HashSet) -> Resul Ok(children) => children, // Removed since the directory above it was listed (a // build's temp dir): it holds no link any more. - Err(e) if e.kind() == std::io::ErrorKind::NotFound && dir != tree => { + Err(e) if not_there(&e) && dir != tree => { return Some((links, dirs)); } Err(_) => return None, @@ -1470,7 +1589,7 @@ fn sweep_tree_sync(tree: &Path, budget: usize, read: &HashSet) -> Resul let path = child.path(); match std::fs::canonicalize(&path) { Ok(target) => links.push(SweptLink { path, target }), - Err(e) if leads_nowhere(&e) => {} + Err(e) if link_leads_nowhere(&path, &e) => {} Err(_) => return None, } } @@ -1869,7 +1988,7 @@ impl LiveStoreEntries { } } // Nothing is read for a store that cannot be judged in any case. - let judged = !cfg!(windows) && self.scanned.is_some(); + let judged = self.scanned.is_some(); let split: HashSet<&Path> = entry_copies .into_iter() .filter(|(store, copies)| { @@ -7122,18 +7241,58 @@ mod tests { { // Rebuilt from components so every separator is `\`: `mklink` // reads a `/` inside a path (`a@1/node_modules/b`) as a switch. - let link: PathBuf = link.components().collect(); - let target: PathBuf = target.components().collect(); + // It does not take the `\\?\` form of a canonical path either. + let link: PathBuf = unverbatim(link).components().collect(); + let target: PathBuf = unverbatim(target).components().collect(); let status = std::process::Command::new("cmd") .args(["/C", "mklink", "/J"]) .arg(&link) .arg(&target) .status() .unwrap(); - assert!(status.success(), "mklink /J failed"); + assert!(status.success(), "mklink /J {link:?} {target:?} failed"); + } + } + + /// `path` without the `\\?\` that `canonicalize` puts before a drive + /// letter. + #[cfg(windows)] + fn unverbatim(path: &Path) -> PathBuf { + let text = path.to_str().unwrap(); + match text.strip_prefix(r"\\?\") { + Some(rest) if rest.as_bytes().get(1) == Some(&b':') => PathBuf::from(rest), + _ => path.to_path_buf(), } } + /// Whether a link to a file can be made here. Windows has no junction + /// to a file, and a symbolic link takes a privilege there: Developer + /// Mode, or an elevated process, as on a CI runner. + fn can_link_files() -> bool { + #[cfg(windows)] + { + static CAN: std::sync::OnceLock = std::sync::OnceLock::new(); + *CAN.get_or_init(|| { + let tmp = tempfile::tempdir().unwrap(); + let file = tmp.path().join("file"); + std::fs::write(&file, "").unwrap(); + std::os::windows::fs::symlink_file(&file, tmp.path().join("link")).is_ok() + }) + } + #[cfg(not(windows))] + { + true + } + } + + /// Take the link at `link` away, whatever it leads to: Windows removes + /// a link to a directory as a directory. + fn unlink(link: &Path) { + std::fs::remove_file(link) + .or_else(|_| std::fs::remove_dir(link)) + .unwrap_or_else(|e| panic!("{link:?}: {e}")); + } + /// A small vlt tree on the host's own link kind (junctions on Windows): /// the importer link resolves at the importer root, a dependency link /// inside a store entry is an edge (never a second inventory entry), @@ -8364,13 +8523,14 @@ mod tests { /// A link at `link` to `target`, relative where the platform allows /// (Bun writes its `node_modules` links relative); a junction to the - /// resolved target on Windows. + /// resolved target on Windows, or a symbolic link when that target is + /// a file (see [`can_link_files`]). fn rel_link(target: &str, link: &Path) { std::fs::create_dir_all(link.parent().unwrap()).unwrap(); #[cfg(unix)] std::os::unix::fs::symlink(target, link).unwrap(); #[cfg(windows)] - link_dir( + abs_link( &crate::utils::relpath::normalize_lexically_keeping_escapes( &link.parent().unwrap().join(target), ), @@ -8669,14 +8829,39 @@ mod tests { } /// A link at `link` to the absolute path `target`, in a directory made - /// for it. Returns `link`. - #[cfg(unix)] + /// for it: a symbolic link on Unix; on Windows a junction, or a + /// symbolic link when `target` is a file (see [`can_link_files`]). + /// Returns `link`. fn abs_link(target: &Path, link: &Path) -> PathBuf { std::fs::create_dir_all(link.parent().unwrap()).unwrap(); + #[cfg(unix)] std::os::unix::fs::symlink(target, link).unwrap(); + #[cfg(windows)] + if target.is_file() { + // A link keeps its target as written, and Windows takes no + // `/` there. + let target: PathBuf = unverbatim(target).components().collect(); + std::os::windows::fs::symlink_file(target, link).unwrap(); + } else { + link_dir(target, link); + } link.to_path_buf() } + /// A symbolic link at `link` to the file at the relative path `target`, + /// which need not be there (see [`can_link_files`]). + fn file_link(target: &str, link: &Path) { + #[cfg(unix)] + std::os::unix::fs::symlink(target, link).unwrap(); + #[cfg(windows)] + { + // A link keeps its target as written, and Windows takes no + // `/` there. + let target: PathBuf = Path::new(target).components().collect(); + std::os::windows::fs::symlink_file(target, link).unwrap(); + } + } + /// What `vex`'s live-copy filter keeps of each list of `lists` (the /// copies of one package version each) for a crawl of the directory /// `scanned`, and the judgement it made. @@ -8698,13 +8883,23 @@ mod tests { (lists, live) } + /// An `assert_eq!` that goes on: a difference is added to `$failed`, + /// so that one run of a table names every case that went wrong. + macro_rules! expect_eq { + ($failed:ident, $left:expr, $right:expr, $($arg:tt)+) => {{ + let (left, right) = (&$left, &$right); + if left != right { + $failed.push(format!("{}: {left:?} != {right:?}", format!($($arg)+))); + } + }}; + } + /// [`judge_lists`] for the copies of one package version. async fn live_copies(scanned: &Path, copies: &[&PathBuf]) -> Vec { judge_lists(scanned, &[copies]).await.0.remove(0) } /// The trees `live` read the links of that lie below `base`, sorted. - #[cfg(unix)] fn swept_below(live: &LiveStoreEntries, base: &Path) -> Vec { let base = std::fs::canonicalize(base).unwrap(); let mut trees: Vec = live @@ -8747,7 +8942,6 @@ mod tests { /// `vex`, whatever it holds, and the entry the importer links stays /// one; the resolver still finds the leftover, so rollback restores /// it, and the scan still lists the package once. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_a_hosted_route_leaves_behind_is_not_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -8788,7 +8982,6 @@ mod tests { /// every pnpm, so the in-place install links the importer to /// `minimist@file+.socket+vendor+npm++minimist-1.2.2.tgz` and /// leaves the upstream entry behind the same way. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_a_vendored_tarball_leaves_behind_is_not_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -8813,7 +9006,6 @@ mod tests { /// `minimist@1.2.2` entry, so that copy is the one `vex` must judge, /// and an entry of the patch's registry that nothing links (a later /// rollback's leftover) is the one left out. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_the_importer_links_stays_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -8839,7 +9031,6 @@ mod tests { /// 0.0.8 from `mkdirp`'s entry alone. The three upstream entries are /// left behind. Returns the root's store, and the routed and the /// leftover copy of each version in that order. - #[cfg(unix)] fn pnpm_workspace_route_leftovers(root: &Path) -> (PathBuf, [PathBuf; 3], [PathBuf; 3]) { let (nm, store) = pnpm_install(root, &["packages/a", "packages/b"]); let entry = |entry: &str, version: &str| pnpm_entry(&store, entry, "minimist", version); @@ -8882,7 +9073,6 @@ mod tests { /// What the filter keeps of the leftover and the routed copy of each /// version of [`pnpm_workspace_route_leftovers`], one list a version. - #[cfg(unix)] async fn live_route_pairs( scanned: &Path, routed: &[PathBuf; 3], @@ -8899,7 +9089,6 @@ mod tests { /// entry links stay live, and the three upstream entries nothing links /// are left out. It makes no difference which of the two lockfiles /// names a member. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_only_a_workspace_member_links_is_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -8923,7 +9112,6 @@ mod tests { /// long as that entry is. Once nothing links `mkdirp`'s entry either, /// no copy of `minimist@0.0.8` is linked: an entry is left out only /// beside a linked one of its version, so both are judged as before. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_only_a_live_entry_links_is_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -8936,7 +9124,7 @@ mod tests { vec![transitive.clone()] ); - std::fs::remove_file(root.join("node_modules/mkdirp")).unwrap(); + unlink(&root.join("node_modules/mkdirp")); assert_eq!( live_copies(&root, &[stale_transitive, transitive]).await, vec![stale_transitive.clone(), transitive.clone()] @@ -8952,7 +9140,6 @@ mod tests { /// `minimist` link still points at the upstream entry, and node run /// from `packages/b` loads that copy. It stays live for as long as /// that link is there; without it, the upstream entry is left out. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_a_former_member_reaches_is_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -9006,7 +9193,6 @@ mod tests { /// outside the store's entries, so both are left out: a link inside an /// entry nothing reaches keeps nothing. One link to the old dependent /// from outside brings both back. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entries_only_each_other_links_are_not_live_copies() { const OLD: &str = "needs-minimist@file+needs-minimist-1.0.0.tgz_minimist@1.2.2"; @@ -9052,7 +9238,6 @@ mod tests { /// store of [`pnpm_hosted_route_leftover`], whose leftover is left out /// as it stands, and the leftover is a live copy for as long as that /// link is there. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_any_link_reaches_is_a_live_copy() { // Adds the link; returns the path that takes it away again. @@ -9113,6 +9298,8 @@ mod tests { // Node loads what a link leads to from where it really is: a // directory outside the project is read like the project, and // so is each `node_modules` above it, or above a file outside. + // Windows does not read what such a link leads to, and keeps + // every entry for as long as the link is there. ( "a link to a directory outside the project that links the leftover", Box::new(|root: &Path| { @@ -9154,40 +9341,6 @@ mod tests { abs_link(&x, &root.join("tools/x")) }), ), - ( - "a link to a file of a former member outside the project", - Box::new(|root: &Path| { - let shared = root.parent().unwrap().join("shared"); - abs_link(&root.join(LEFTOVER), &shared.join("node_modules/minimist")); - std::fs::write(shared.join("gen.js"), "").unwrap(); - abs_link(&shared.join("gen.js"), &root.join("scripts/gen.js")) - }), - ), - ( - "a link to a file two directories below a former member's scoped link", - Box::new(|root: &Path| { - let shared = root.parent().unwrap().join("shared"); - abs_link(&root.join(LEFTOVER), &shared.join("node_modules/@s/mini")); - std::fs::create_dir_all(shared.join("lib/deep")).unwrap(); - std::fs::write(shared.join("lib/deep/gen.js"), "").unwrap(); - abs_link( - &shared.join("lib/deep/gen.js"), - &root.join("scripts/gen.js"), - ) - }), - ), - ( - "a link to a file beside a node_modules that is a link into the entry", - Box::new(|root: &Path| { - let shared = root.parent().unwrap().join("shared"); - abs_link( - root.join(LEFTOVER).parent().unwrap(), - &shared.join("node_modules"), - ); - std::fs::write(shared.join("gen.js"), "").unwrap(); - abs_link(&shared.join("gen.js"), &root.join("scripts/gen.js")) - }), - ), ( "a link to the entry's node_modules", Box::new(|root: &Path| { @@ -9197,15 +9350,6 @@ mod tests { ) }), ), - ( - "a link to a file of the package", - Box::new(|root: &Path| { - abs_link( - &root.join(LEFTOVER).join("package.json"), - &root.join("vendor/minimist.json"), - ) - }), - ), ( "a chain of two links, the second outside the project", Box::new(|root: &Path| { @@ -9225,29 +9369,86 @@ mod tests { }), ), ]; + let mut cases = cases; + if can_link_files() { + let to_a_file: Vec<(&str, AddLink)> = vec![ + // Node loads a linked file from where it is: beside the + // `node_modules` of a member that left the workspace, which is + // not read, so every entry is kept while the link is there. + ( + "a link to a file of a former member outside the project", + Box::new(|root: &Path| { + let shared = root.parent().unwrap().join("shared"); + abs_link(&root.join(LEFTOVER), &shared.join("node_modules/minimist")); + std::fs::write(shared.join("gen.js"), "").unwrap(); + abs_link(&shared.join("gen.js"), &root.join("scripts/gen.js")) + }), + ), + ( + "a link to a file two directories below a former member's scoped link", + Box::new(|root: &Path| { + let shared = root.parent().unwrap().join("shared"); + abs_link(&root.join(LEFTOVER), &shared.join("node_modules/@s/mini")); + std::fs::create_dir_all(shared.join("lib/deep")).unwrap(); + std::fs::write(shared.join("lib/deep/gen.js"), "").unwrap(); + abs_link( + &shared.join("lib/deep/gen.js"), + &root.join("scripts/gen.js"), + ) + }), + ), + ( + "a link to a file beside a node_modules that is a link into the entry", + Box::new(|root: &Path| { + let shared = root.parent().unwrap().join("shared"); + abs_link( + root.join(LEFTOVER).parent().unwrap(), + &shared.join("node_modules"), + ); + std::fs::write(shared.join("gen.js"), "").unwrap(); + abs_link(&shared.join("gen.js"), &root.join("scripts/gen.js")) + }), + ), + ( + "a link to a file of the package", + Box::new(|root: &Path| { + abs_link( + &root.join(LEFTOVER).join("package.json"), + &root.join("vendor/minimist.json"), + ) + }), + ), + ]; + cases.extend(to_a_file); + } + let mut failed = Vec::new(); for (case, add_link) in cases { let (_tmp, _, root) = pnpm_project_dir(); let (_, stale, routed) = pnpm_hosted_route_leftover(&root); let copies = [&stale, &routed]; - assert_eq!( + expect_eq!( + failed, live_copies(&root, &copies).await, vec![routed.clone()], "{case}: before the link" ); let link = add_link(&root); - assert_eq!( + expect_eq!( + failed, live_copies(&root, &copies).await, vec![stale.clone(), routed.clone()], "{case}" ); // The link kept it, not a store that could no longer be judged. - std::fs::remove_file(&link).unwrap(); - assert_eq!( + unlink(&link); + expect_eq!( + failed, live_copies(&root, &copies).await, vec![routed.clone()], "{case}: without the link" ); } + assert!(failed.is_empty(), "{failed:#?}"); } /// #2353: a link may spell an entry in another case than the store @@ -9255,7 +9456,6 @@ mod tests { /// the entry is a live copy (`canonicalize` gives the listed spelling /// back on macOS; where it does not, the entry cannot be named and /// every entry is kept). Where case is kept, the link leads nowhere. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_a_link_in_another_case_reaches_is_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -9277,7 +9477,6 @@ mod tests { /// does not stop the store from being judged. Each case changes one /// thing about the store of [`pnpm_hosted_route_leftover`], and the /// leftover is left out all the same. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_entry_no_link_reaches_from_outside_is_not_a_live_copy() { type Change = Box; @@ -9312,41 +9511,9 @@ mod tests { Box::new(|root: &Path| { let package = root.join(LEFTOVER); rel_link(".", &package.join("self")); - rel_link( - "../minimist/package.json", - &package.join("../.bin/minimist"), - ); rel_link("../..", &package.join("node_modules/up")); }), ), - ( - "a link whose target is not there", - Box::new(|root: &Path| { - abs_link(&root.join(LEFTOVER).join("gone"), &root.join("vendor/x")); - rel_link("nowhere", &root.join("dist/x")); - }), - ), - ( - "a link that loops", - Box::new(|root: &Path| rel_link("loop", &root.join("vendor/loop"))), - ), - ( - "a link through a file", - Box::new(|root: &Path| { - std::fs::write(root.join("file"), "").unwrap(); - rel_link("../file/child", &root.join("vendor/x")); - }), - ), - // Opening a pipe nothing writes to never returns. - ( - "a link to a named pipe", - Box::new(|root: &Path| { - let pipe = root.join("run/pipe"); - std::fs::create_dir_all(pipe.parent().unwrap()).unwrap(); - mkfifo(&pipe); - abs_link(&pipe, &root.join("tools/own")); - }), - ), ( "a link to a directory inside the project", Box::new(|root: &Path| { @@ -9410,36 +9577,46 @@ mod tests { }), ), ]; - // What a link leads to outside the project is read, and links - // nothing of the store: a file with a `node_modules` above it, a - // file below a file and a dangling link of that name, a pipe, and - // directories that link each other and the project. let mut cases = cases; + if can_link_files() { + cases.push(( + "a link inside the leftover entry to a file of it", + Box::new(|root: &Path| { + rel_link( + "../minimist/package.json", + &root.join(LEFTOVER).join("../.bin/minimist"), + ); + }), + )); + } + // Opening a pipe nothing writes to never returns. + #[cfg(unix)] cases.push(( - "a link to a file outside the project", - Box::new(|root: &Path| { - let outside = root.ancestors().nth(2).unwrap().join("elsewhere"); - std::fs::create_dir_all(outside.join("deep")).unwrap(); - std::fs::write(outside.join("deep/file"), "").unwrap(); - abs_link(&outside.join("deep/file"), &root.join("tools/file")); - std::fs::write(outside.join("node_modules"), "").unwrap(); - rel_link("nowhere", &outside.join("deep/node_modules")); - }), - )); - // A file that a link leads to is judged once every directory is - // read: this one lies in a directory another link leads to, which - // is found after it. - cases.push(( - "a link to a file in a directory outside the project that is read", + "a link to a named pipe", Box::new(|root: &Path| { - let out = root.parent().unwrap().join("out"); - write_pkg(&out.join("node_modules/real"), "real", "1.0.0"); - std::fs::create_dir_all(out.join("lib")).unwrap(); - std::fs::write(out.join("lib/gen.js"), "").unwrap(); - abs_link(&out.join("lib/gen.js"), &root.join("a-file.js")); - abs_link(&out, &root.join("z-dir")); + let pipe = root.join("run/pipe"); + std::fs::create_dir_all(pipe.parent().unwrap()).unwrap(); + mkfifo(&pipe); + abs_link(&pipe, &root.join("tools/own")); }), )); + // A link to a file outside the project, with no `node_modules` in + // a directory above the file: a file named so, and a link named + // so whose target is not there, are none. + if can_link_files() { + cases.push(( + "a link to a file outside the project", + Box::new(|root: &Path| { + let outside = root.ancestors().nth(2).unwrap().join("elsewhere"); + std::fs::create_dir_all(outside.join("deep")).unwrap(); + std::fs::write(outside.join("deep/file"), "").unwrap(); + abs_link(&outside.join("deep/file"), &root.join("tools/file")); + std::fs::write(outside.join("node_modules"), "").unwrap(); + rel_link("nowhere", &outside.join("deep/node_modules")); + }), + )); + } + #[cfg(unix)] cases.push(( "a link to a named pipe outside the project", Box::new(|root: &Path| { @@ -9448,31 +9625,80 @@ mod tests { abs_link(&pipe, &root.join("tools/ctl")); }), )); - cases.push(( - "links to directories outside the project", - Box::new(|root: &Path| { - let (x, y) = ( - root.parent().unwrap().join("x"), - root.parent().unwrap().join("y"), - ); - write_pkg(&y.join("node_modules/real"), "real", "1.0.0"); - abs_link(&y, &x.join("deps/y")); - abs_link(&x, &y.join("deps/x")); - abs_link(root, &y.join("app")); - abs_link(&x, &root.join("node_modules/linked")); - abs_link(&x.join("deps"), &root.join("bazel-bin")); - }), - )); + // What a link leads to outside the project is read, and links + // nothing of the store: a file with a `node_modules` above it, + // in a directory that another link leads to, and directories + // that link each other and the project. Not on Windows, which + // does not read them and keeps every entry (see + // `test_pnpm_store_that_cannot_be_judged_keeps_every_entry`). + #[cfg(not(windows))] + { + // A file that a link leads to is judged once every directory is + // read: this one lies in a directory another link leads to, which + // is found after it. + cases.push(( + "a link to a file in a directory outside the project that is read", + Box::new(|root: &Path| { + let out = root.parent().unwrap().join("out"); + write_pkg(&out.join("node_modules/real"), "real", "1.0.0"); + std::fs::create_dir_all(out.join("lib")).unwrap(); + std::fs::write(out.join("lib/gen.js"), "").unwrap(); + abs_link(&out.join("lib/gen.js"), &root.join("a-file.js")); + abs_link(&out, &root.join("z-dir")); + }), + )); + cases.push(( + "links to directories outside the project", + Box::new(|root: &Path| { + let (x, y) = ( + root.parent().unwrap().join("x"), + root.parent().unwrap().join("y"), + ); + write_pkg(&y.join("node_modules/real"), "real", "1.0.0"); + abs_link(&y, &x.join("deps/y")); + abs_link(&x, &y.join("deps/x")); + abs_link(root, &y.join("app")); + abs_link(&x, &root.join("node_modules/linked")); + abs_link(&x.join("deps"), &root.join("bazel-bin")); + }), + )); + } + // The links that lead nowhere, last: a platform that cannot make + // one ends the table there. + let nowhere: Vec<(&str, Change)> = vec![ + ( + "a link whose target is not there", + Box::new(|root: &Path| { + abs_link(&root.join(LEFTOVER).join("gone"), &root.join("vendor/x")); + rel_link("nowhere", &root.join("dist/x")); + }), + ), + ( + "a link that loops", + Box::new(|root: &Path| rel_link("loop", &root.join("vendor/loop"))), + ), + ( + "a link through a file", + Box::new(|root: &Path| { + std::fs::write(root.join("file"), "").unwrap(); + rel_link("../file/child", &root.join("vendor/x")); + }), + ), + ]; + cases.extend(nowhere); + let mut failed = Vec::new(); for (case, change) in cases { let (_tmp, _, root) = pnpm_project_dir(); let (_, stale, routed) = pnpm_hosted_route_leftover(&root); change(&root); - assert_eq!( + expect_eq!( + failed, live_copies(&root, &[&stale, &routed]).await, vec![routed], "{case}" ); } + assert!(failed.is_empty(), "{failed:#?}"); } /// A named pipe at `path`, which nothing writes to. @@ -9485,6 +9711,62 @@ mod tests { assert!(made.success(), "mkfifo {path:?}"); } + /// #2353: node looks up the packages of a file in the `node_modules` + /// of each directory above it, so a link to a file outside the project + /// keeps every entry when one is there: up to the home directory, + /// which is not looked at, nor is anything above it. A file or a + /// dangling link named `node_modules` is none. + #[test] + fn test_pnpm_store_link_to_a_file_outside_is_judged_by_the_node_modules_above_it() { + if !can_link_files() { + return; + } + let tmp = tempfile::tempdir().unwrap(); + let base: PathBuf = tmp.path().components().collect(); + let home = base.join("home"); + let root = home.join("projects/app"); + std::fs::create_dir_all(&root).unwrap(); + let (nm, _, _) = pnpm_hosted_route_leftover(&root); + let store = nm.join(".pnpm"); + let file = home.join("projects/shared/lib/gen.js"); + std::fs::create_dir_all(file.parent().unwrap()).unwrap(); + std::fs::write(&file, "").unwrap(); + abs_link(&file, &root.join("scripts/gen.js")); + // The home directory as the temp dir spells it, and as the real + // path of a link does: macOS reaches the temp dir through a link, + // and Windows may name it by a short name, without `\\?\`. + let spelled_home = home; + let base = std::fs::canonicalize(&base).unwrap(); + let home = base.join("home"); + let judged = |home: &Path| { + live_pnpm_store_entries_under_sync( + &store, + &root, + Some(home), + LINK_SWEEP_BUDGET, + &mut HashMap::new(), + ) + .is_some() + }; + + assert!(judged(&home)); + std::fs::write(home.join("projects/shared/lib/node_modules"), "").unwrap(); + rel_link("nowhere", &home.join("projects/shared/node_modules")); + std::fs::create_dir_all(home.join("node_modules")).unwrap(); + std::fs::create_dir_all(base.join("node_modules")).unwrap(); + assert!(judged(&home), "not in the home directory"); + assert!( + judged(&spelled_home), + "the home directory by another spelling of its path" + ); + assert!( + !judged(&base.join("elsewhere")), + "with the home directory elsewhere" + ); + std::fs::create_dir_all(home.join("projects/node_modules")).unwrap(); + assert!(!judged(&home), "below the home directory"); + } + /// #2353: what a link leads to outside the project is read up to the /// home directory. A `node_modules` there or above it is not looked /// at, and a link to the home directory, or to one above it, leaves the @@ -9667,7 +9949,6 @@ mod tests { /// version in one entry: each is kept though nothing links it, and /// the project is not read for them. Where one version does sit in /// two entries, the others are still kept as they are. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_is_judged_only_for_a_version_in_two_of_its_entries() { let (_tmp, _, root) = pnpm_project_dir(); @@ -9730,7 +10011,7 @@ mod tests { swept_below(&live, &root), vec![std::fs::canonicalize(&root).unwrap()] ); - std::fs::remove_file(&hoisted).unwrap(); + unlink(&hoisted); assert_eq!( live_copies(&root, &[&peers[0], &peers[1]]).await, vec![peers[0].clone()] @@ -9755,7 +10036,6 @@ mod tests { /// hoist dir `.pnpm/node_modules`, so a package that is a real /// directory there loads without any link, and a hidden child of the /// store is not judged at all. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_copy_in_none_of_its_entries_is_always_a_live_copy() { let (_tmp, _, root) = pnpm_project_dir(); @@ -9783,7 +10063,6 @@ mod tests { /// the project reads everything below that directory, so a link in /// another checkout there counts. The same link is out of reach of a /// scan of the project alone. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_is_judged_by_the_links_below_the_outer_directory() { let (_tmp, base, root) = pnpm_project_dir(); @@ -9802,7 +10081,7 @@ mod tests { ); let link = abs_link(&stale, &root.join("vendor/x")); assert_eq!(live_copies(&member, &copies).await, both); - std::fs::remove_file(link).unwrap(); + unlink(&link); // A project run reads no project file above its git checkout. With // the member a checkout of its own, the project above it is not @@ -9832,7 +10111,6 @@ mod tests { /// #2353: several pnpm stores below one scanned directory are judged /// from one read of its links, and a link in one project's /// `node_modules` keeps an entry of the other's store. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_stores_below_one_scanned_directory_share_one_sweep() { let (_tmp, base, root) = pnpm_project_dir(); @@ -9863,16 +10141,15 @@ mod tests { /// directory, with the real path it resolves to, and follows none: a /// link behind a link to a directory outside the tree is not read. A /// link whose target is not there is left out. One directory or link - /// more than the budget, and there is no answer. - #[cfg(unix)] + /// more than the budget, and there is no answer. The link to a file + /// is made where one can be (see [`can_link_files`]). #[test] fn test_links_below_lists_every_link_and_follows_none() { let tmp = tempfile::tempdir().unwrap(); let base = std::fs::canonicalize(tmp.path()).unwrap(); let (tree, outside) = (base.join("tree"), base.join("outside")); std::fs::create_dir_all(outside.join("deep")).unwrap(); - std::fs::write(outside.join("file"), "").unwrap(); - abs_link(&outside.join("file"), &outside.join("deep/unread")); + abs_link(&outside, &outside.join("deep/unread")); // Ten directories, the tree itself among them. for dir in [ "vendor", @@ -9886,7 +10163,6 @@ mod tests { let mut expected = vec![ (tree.join(".hidden/up"), tree.clone()), (tree.join("chain"), outside.clone()), - (tree.join("dist/a/b/file"), tree.join("file")), ( tree.join("node_modules/pkg/node_modules/@s/x"), tree.join("vendor"), @@ -9895,7 +10171,10 @@ mod tests { ]; abs_link(&tree, &tree.join(".hidden/up")); abs_link(&tree.join("vendor/dir"), &tree.join("chain")); - abs_link(&tree.join("file"), &tree.join("dist/a/b/file")); + if can_link_files() { + abs_link(&tree.join("file"), &tree.join("dist/a/b/file")); + expected.push((tree.join("dist/a/b/file"), tree.join("file"))); + } abs_link( &tree.join("vendor"), &tree.join("node_modules/pkg/node_modules/@s/x"), @@ -9912,9 +10191,10 @@ mod tests { Some(links) }; expected.sort(); - // Ten directories and the five links that resolve. - assert_eq!(found(15), Some(expected.clone())); - assert_eq!(found(14), None); + // Ten directories and the links that resolve. + let all = 10 + expected.len(); + assert_eq!(found(all), Some(expected.clone())); + assert_eq!(found(all - 1), None); assert_eq!( links_below_sync(&tree).map(|links| links.len()), Some(expected.len()) @@ -9925,8 +10205,8 @@ mod tests { /// #2353: two copies of a package hold the same bytes when every file /// and link below them does, by path and content, whatever launchers /// sit in the `.bin` of each package's own `node_modules`. A copy that - /// cannot be read through is never the same as another. - #[cfg(unix)] + /// cannot be read through is never the same as another. The link is + /// made where one to a file can be (see [`can_link_files`]). #[test] fn test_same_package_bytes_compares_every_file_and_link() { let tmp = tempfile::tempdir().unwrap(); @@ -9936,7 +10216,9 @@ mod tests { std::fs::create_dir_all(dir.join("lib/node_modules/inner")).unwrap(); std::fs::write(dir.join("lib/index.js"), "module.exports = 1\n").unwrap(); std::fs::write(dir.join("lib/node_modules/inner/index.js"), "inner\n").unwrap(); - std::os::unix::fs::symlink("lib/index.js", dir.join("main.js")).unwrap(); + if can_link_files() { + file_link("lib/index.js", &dir.join("main.js")); + } std::fs::create_dir_all(dir.join("node_modules/.bin")).unwrap(); std::fs::write(dir.join("node_modules/.bin/dep"), name).unwrap(); std::fs::create_dir_all(dir.join("node_modules/bundled")).unwrap(); @@ -9950,7 +10232,7 @@ mod tests { assert!(!same_package_bytes_sync(std::iter::empty::<&Path>())); type Change = fn(&Path); - let changes: [(&str, Change); 8] = [ + let mut changes: Vec<(&str, Change)> = vec![ ("other bytes in a dependency the package bundles", |b| { std::fs::write(b.join("node_modules/bundled/index.js"), "other\n").unwrap() }), @@ -9966,18 +10248,21 @@ mod tests { ("a file under another name", |b| { std::fs::rename(b.join("lib/index.js"), b.join("lib/main.js")).unwrap() }), - ("a link to another target", |b| { + ]; + if can_link_files() { + changes.push(("a link to another target", |b| { std::fs::remove_file(b.join("main.js")).unwrap(); - std::os::unix::fs::symlink("lib/other.js", b.join("main.js")).unwrap(); - }), - ("a file in place of a link", |b| { + file_link("lib/other.js", &b.join("main.js")); + })); + changes.push(("a file in place of a link", |b| { std::fs::remove_file(b.join("main.js")).unwrap(); std::fs::write(b.join("main.js"), "lib/index.js").unwrap(); - }), - ("a copy that is not there", |b| { - std::fs::remove_dir_all(b).unwrap() - }), - ]; + })); + } + // Last: it leaves no copy to take away before the next one. + changes.push(("a copy that is not there", |b| { + std::fs::remove_dir_all(b).unwrap() + })); for (case, change) in changes { std::fs::remove_dir_all(&b).unwrap(); let b = copy("b"); @@ -10034,11 +10319,8 @@ mod tests { /// nothing is left out. Each case changes one thing about the store of /// [`pnpm_hosted_route_leftover`], whose leftover is left out as it /// stands. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_that_cannot_be_judged_keeps_every_entry() { - use std::os::unix::fs::PermissionsExt; - type Change = Box; let record = |from: &'static str, to: &'static str| -> Change { Box::new(move |root: &Path| { @@ -10193,19 +10475,6 @@ mod tests { abs_link(&root.join("node_modules/.pnpm"), &root.join("vendor/store")); }), ), - ( - "a link to a file outside the project below a node_modules", - Box::new(|root: &Path| { - let shared = beside(root); - write_pkg(&shared.join("node_modules/real"), "real", "1.0.0"); - std::fs::create_dir_all(shared.join("lib/deep")).unwrap(); - std::fs::write(shared.join("lib/deep/gen.js"), "").unwrap(); - abs_link( - &shared.join("lib/deep/gen.js"), - &root.join("scripts/gen.js"), - ); - }), - ), ( "a link to a directory outside the project that is a link to the store", Box::new(|root: &Path| { @@ -10219,21 +10488,54 @@ mod tests { }), ), ( - "a link into the store that names none of its entries", + "no link into the store", + Box::new(|root: &Path| unlink(&root.join("node_modules/minimist"))), + ), + ]; + if can_link_files() { + let to_a_file: Vec<(&str, Change)> = vec![ + ( + "a link to a file outside the project below a node_modules", + Box::new(|root: &Path| { + let shared = beside(root); + write_pkg(&shared.join("node_modules/real"), "real", "1.0.0"); + std::fs::create_dir_all(shared.join("lib/deep")).unwrap(); + std::fs::write(shared.join("lib/deep/gen.js"), "").unwrap(); + abs_link( + &shared.join("lib/deep/gen.js"), + &root.join("scripts/gen.js"), + ); + }), + ), + ( + "a link into the store that names none of its entries", + Box::new(|root: &Path| { + abs_link( + &root.join("node_modules/.pnpm/lock.yaml"), + &root.join("vendor/lock.yaml"), + ); + }), + ), + ]; + cases.extend(to_a_file); + } + // Windows does not read what a link leads to outside the project: + // a directory there keeps every entry, whatever it holds. + #[cfg(windows)] + { + cases.push(( + "a link to a directory outside the project", Box::new(|root: &Path| { - abs_link( - &root.join("node_modules/.pnpm/lock.yaml"), - &root.join("vendor/lock.yaml"), - ); + abs_link(&beside(root), &root.join("node_modules/linked")); }), - ), - ( - "no link into the store", + )); + cases.push(( + "a link to a directory above the project", Box::new(|root: &Path| { - std::fs::remove_file(root.join("node_modules/minimist")).unwrap() + abs_link(root.parent().unwrap(), &root.join("tools/up")); }), - ), - ]; + )); + } // macOS reaches the data volume under `/System/Volumes/Data` as // well, and `canonicalize` keeps that spelling. Only where the // temp dir is on that volume. @@ -10255,10 +10557,12 @@ mod tests { )); } } + let mut failed = Vec::new(); for (case, change) in cases { let (_tmp, _, root) = pnpm_project_dir(); let (nm, stale, routed) = pnpm_hosted_route_leftover(&root); - assert_eq!( + expect_eq!( + failed, live_copies(&root, &[&stale, &routed]).await, vec![routed.clone()], "{case}: before the change" @@ -10270,12 +10574,14 @@ mod tests { .filter(|copy| copy.exists()) .map(|copy| (*copy).clone()) .collect(); - assert_eq!( + expect_eq!( + failed, live_copies(&root, &kept.iter().collect::>()).await, kept, "{case}" ); } + assert!(failed.is_empty(), "{failed:#?}"); // A scanned directory that neither holds the project nor lies in // it, and a crawl that scans no project directory (`--global`). @@ -10296,47 +10602,53 @@ mod tests { // Directories that cannot be read (not as root, which reads them // all the same): one below the project, one that the target of a // link lies behind, and one below a directory outside the project - // that a link leads to. - let locked_dirs = [ - ( - "an unreadable directory below the project", - "ws/app/dist/private", - None, - ), - ( - "a link whose target lies behind an unreadable directory", - "locked", - Some(("locked/file", "ws/app/vendor/x")), - ), - ( - "an unreadable directory below one outside the project", - "ws/out/private", - Some(("ws/out", "ws/app/tools/out")), - ), - ]; - for (case, locked, link) in locked_dirs { - let (_tmp, base, root) = pnpm_project_dir(); - let (_, stale, routed) = pnpm_hosted_route_leftover(&root); - let locked = base.join(locked); - std::fs::create_dir_all(&locked).unwrap(); - std::fs::write(locked.join("file"), "").unwrap(); - if let Some((target, link)) = link { - if !base.join(target).is_dir() { - std::fs::write(base.join(target), "").unwrap(); + // that a link leads to. Unix alone: Windows has no mode bits to + // take a directory away with. + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + let locked_dirs = [ + ( + "an unreadable directory below the project", + "ws/app/dist/private", + None, + ), + ( + "a link whose target lies behind an unreadable directory", + "locked", + Some(("locked/file", "ws/app/vendor/x")), + ), + ( + "an unreadable directory below one outside the project", + "ws/out/private", + Some(("ws/out", "ws/app/tools/out")), + ), + ]; + for (case, locked, link) in locked_dirs { + let (_tmp, base, root) = pnpm_project_dir(); + let (_, stale, routed) = pnpm_hosted_route_leftover(&root); + let locked = base.join(locked); + std::fs::create_dir_all(&locked).unwrap(); + std::fs::write(locked.join("file"), "").unwrap(); + if let Some((target, link)) = link { + if !base.join(target).is_dir() { + std::fs::write(base.join(target), "").unwrap(); + } + abs_link(&base.join(target), &base.join(link)); + } + assert_eq!( + live_copies(&root, &[&stale, &routed]).await, + vec![routed.clone()], + "{case}: while it can be read" + ); + std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap(); + let unreadable = std::fs::read_dir(&locked).is_err(); + let kept = live_copies(&root, &[&stale, &routed]).await; + std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap(); + if unreadable { + assert_eq!(kept, vec![stale, routed], "{case}"); } - abs_link(&base.join(target), &base.join(link)); - } - assert_eq!( - live_copies(&root, &[&stale, &routed]).await, - vec![routed.clone()], - "{case}: while it can be read" - ); - std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap(); - let unreadable = std::fs::read_dir(&locked).is_err(); - let kept = live_copies(&root, &[&stale, &routed]).await; - std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap(); - if unreadable { - assert_eq!(kept, vec![stale, routed], "{case}"); } } } @@ -10350,7 +10662,6 @@ mod tests { /// beside it. The store is judged from there: the leftover is left /// out, and a link in a member's own modules dir keeps it. Without the /// setting the root is not known, and every entry is kept. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_under_a_nested_modules_dir_is_judged_from_the_root() { for (settings, text) in [ @@ -10383,7 +10694,7 @@ mod tests { both, "{settings}: linked" ); - std::fs::remove_file(link).unwrap(); + unlink(&link); // A member that is not on disk at the root either. std::fs::write( @@ -10477,7 +10788,6 @@ mod tests { /// (measured with `sub/node_modules` on 9.15.9 and 10.11.1). The record /// in the modules dir names the store, so the store is judged; a /// record that names another one, or none, keeps every entry. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_beside_a_modules_dir_that_holds_its_record_is_judged() { let (_tmp, _, root) = pnpm_project_dir(); @@ -10513,7 +10823,7 @@ mod tests { assert_eq!(live_copies(&root, &copies).await, vec![routed.clone()]); let link = abs_link(&stale, &member_nm.join("old")); assert_eq!(live_copies(&root, &copies).await, both); - std::fs::remove_file(link).unwrap(); + unlink(&link); record("../../elsewhere/.pnpm"); assert_eq!(live_copies(&root, &copies).await, both); @@ -10536,7 +10846,6 @@ mod tests { /// leftover that only the worktree's member links stays a copy. From /// the main checkout, which the worktree is no part of, nothing links /// the leftover. - #[cfg(unix)] #[tokio::test] async fn test_pnpm_store_of_another_checkout_keeps_every_entry() { let (_tmp, base, main) = pnpm_project_dir(); @@ -10560,17 +10869,253 @@ mod tests { ); } - /// #2353: pnpm links directories with junctions on Windows, which the - /// sweep does not read, so nothing is left out there. + /// #2353: pnpm on Windows records the virtual store by its absolute + /// path, where every other platform gets the path from the modules + /// dir (`writeModulesManifest` of pnpm 9.15.9 and 11.27.0). A record + /// that names the store that way is the store's, however the path is + /// spelled (a temp dir is often spelled by a short name there), and + /// one that names another directory is not. + #[tokio::test] + async fn test_pnpm_store_its_record_names_by_an_absolute_path_is_judged() { + let (_tmp, base, root) = pnpm_project_dir(); + let (nm, stale, routed) = pnpm_hosted_route_leftover(&root); + let record = |store: &Path| { + let named = serde_json::to_string(store.to_str().unwrap()).unwrap(); + let record = PNPM_ISOLATED_RECORD.replace( + r#""virtualStoreDir":".pnpm""#, + &format!(r#""virtualStoreDir":{named}"#), + ); + assert_ne!(record, PNPM_ISOLATED_RECORD); + std::fs::write(nm.join(".modules.yaml"), record).unwrap(); + }; + let copies: [&PathBuf; 2] = [&stale, &routed]; + + record(&nm.join(".pnpm")); + assert_eq!(live_copies(&root, &copies).await, vec![routed.clone()]); + let elsewhere = base.join("elsewhere/.pnpm"); + std::fs::create_dir_all(&elsewhere).unwrap(); + record(&elsewhere); + assert_eq!(live_copies(&root, &copies).await, vec![stale, routed]); + } + + /// #2353: pnpm links a directory on Windows with a symbolic link where + /// it may make one and with a junction where it may not (pnpm 11 and + /// 12 write relative symbolic links on a CI runner). The stores of the + /// other tests are linked with junctions there; this one is linked as + /// that runner's are, and its record names the store by its absolute + /// path. The leftover is left out, and a link to it keeps it. + /// + /// Needs the privilege to make a symbolic link, as every case that + /// links a file does (see [`can_link_files`]). A GitHub runner has + /// it: a run there that cannot make one fails here, where it would + /// otherwise pass with those cases left out. #[cfg(windows)] #[tokio::test] - async fn test_pnpm_store_leftovers_are_kept_on_windows() { + async fn test_pnpm_store_linked_with_symbolic_links_is_judged_on_windows() { + use std::os::windows::fs::symlink_dir; + + let on_a_runner = std::env::var_os("GITHUB_ACTIONS").is_some(); + assert!( + !on_a_runner || can_link_files(), + "this runner cannot link a file: the cases that link one do not run" + ); let (_tmp, _, root) = pnpm_project_dir(); - let (nm, stale, routed) = pnpm_hosted_route_leftover(&root); + let (nm, store) = pnpm_install(&root, &[]); + let named = serde_json::to_string(store.to_str().unwrap()).unwrap(); + std::fs::write( + nm.join(".modules.yaml"), + PNPM_ISOLATED_RECORD.replace( + r#""virtualStoreDir":".pnpm""#, + &format!(r#""virtualStoreDir":{named}"#), + ), + ) + .unwrap(); + let stale = pnpm_entry(&store, "minimist@1.2.2", "minimist", "1.2.2"); + let routed = pnpm_entry(&store, ROUTED_ENTRY, "minimist", "1.2.2"); + let importer = nm.join("minimist"); + let linked = format!(r".pnpm\{ROUTED_ENTRY}\node_modules\minimist"); + if let Err(e) = symlink_dir(linked, &importer) { + assert!(!on_a_runner, "this runner cannot link a directory: {e}"); + eprintln!("no privilege to make a symbolic link: nothing to judge"); + return; + } + let copies: [&PathBuf; 3] = [&importer, &stale, &routed]; + assert_eq!( - live_copies(&root, &[&nm.join("minimist"), &stale, &routed]).await, - vec![nm.join("minimist"), stale, routed] + live_copies(&root, &copies).await, + vec![importer.clone(), routed.clone()] + ); + let stray = root.join("vendor").join("old"); + std::fs::create_dir_all(stray.parent().unwrap()).unwrap(); + symlink_dir( + r"..\node_modules\.pnpm\minimist@1.2.2\node_modules\minimist", + &stray, + ) + .unwrap(); + assert_eq!( + live_copies(&root, &copies).await, + vec![importer.clone(), stale.clone(), routed.clone()] ); + unlink(&stray); + assert_eq!( + live_copies(&root, &copies).await, + vec![importer.clone(), routed.clone()] + ); + } + + /// #2353: a link leads nowhere when its target is not there, when a + /// level of the path to it is a file, and when it loops. Each is made + /// here and resolved, and the error is the one named: the links are + /// symbolic links on Unix and junctions on Windows, where the codes + /// are `ERROR_FILE_NOT_FOUND`, `ERROR_PATH_NOT_FOUND` (twice) and + /// `ERROR_CANT_RESOLVE_FILENAME`. + #[cfg(any(unix, windows))] + #[test] + fn test_links_that_lead_nowhere_fail_with_the_errors_read_as_such() { + #[cfg(unix)] + let (missing, no_directory, through_a_file, looping) = + (libc::ENOENT, libc::ENOENT, libc::ENOTDIR, libc::ELOOP); + #[cfg(windows)] + let (missing, no_directory, through_a_file, looping) = (2, 3, 3, 1921); + let tmp = tempfile::tempdir().unwrap(); + let base: PathBuf = tmp.path().components().collect(); + std::fs::write(base.join("file"), "").unwrap(); + let mut failed = Vec::new(); + for (case, link, target, code) in [ + ("a target that is not there", "missing", "nowhere", missing), + ( + "a target in a directory that is not there", + "no-directory", + "gone/nowhere", + no_directory, + ), + ( + "a path through a file", + "through-a-file", + "file/child", + through_a_file, + ), + ("a link to itself", "loop", "loop", looping), + ] { + let link = base.join(link); + rel_link(target, &link); + let error = std::fs::canonicalize(&link).unwrap_err(); + expect_eq!( + failed, + error.raw_os_error(), + Some(code), + "{case}: {error:?}" + ); + expect_eq!(failed, leads_nowhere(&error), true, "{case}: {error:?}"); + // What it leads to fails the same way when asked for by + // itself, so the link is one that leads nowhere. + expect_eq!( + failed, + link_leads_nowhere(&link, &error), + true, + "{case}: {error:?}" + ); + } + assert!(failed.is_empty(), "{failed:#?}"); + + // A link whose target is there is none that leads nowhere, + // whatever resolving it failed with: the name of a target can + // fail where the target is there. + std::fs::create_dir_all(base.join("there")).unwrap(); + let live = abs_link(&base.join("there"), &base.join("live")); + let error = std::io::Error::from_raw_os_error(missing); + assert!(leads_nowhere(&error) && !link_leads_nowhere(&live, &error)); + } + + /// #2353: on Windows a link into a drive that is not there is no link + /// that leads nowhere, whatever resolving it fails with (the code is + /// printed): the drive may be there for another session. Where a + /// junction to such a drive cannot be made, nothing is tested. + #[cfg(windows)] + #[test] + fn test_link_into_a_drive_that_is_not_there_does_not_lead_nowhere() { + let Some(letter) = ('D'..='Z') + .rev() + .find(|letter| std::fs::metadata(format!(r"{letter}:\")).is_err()) + else { + eprintln!("every drive letter is taken: nothing to test"); + return; + }; + let tmp = tempfile::tempdir().unwrap(); + let base: PathBuf = unverbatim(&std::fs::canonicalize(tmp.path()).unwrap()) + .components() + .collect(); + let link = base.join("gone"); + let made = std::process::Command::new("cmd") + .args(["/C", "mklink", "/J"]) + .arg(&link) + .arg(format!(r"{letter}:\gone")) + .output() + .unwrap(); + if !made.status.success() { + eprintln!("no junction to {letter}: can be made: {made:?}"); + return; + } + let error = std::fs::canonicalize(&link).unwrap_err(); + eprintln!( + "a junction to {letter}:, which is not there, fails with {:?} ({error})", + error.raw_os_error() + ); + assert!(!link_leads_nowhere(&link, &error), "{error:?}"); + // A sweep that meets it has no answer. + assert!(links_below_sync(&base).is_none()); + } + + /// #2353: no other failure says that a link leads nowhere. One that + /// cannot be told (a directory this user may not search, a drive or a + /// network path that cannot be reached, a link Windows may not follow) + /// leaves the answer unknown, and every entry is kept. + #[cfg(any(unix, windows))] + #[test] + fn test_leads_nowhere_is_no_other_failure() { + #[cfg(unix)] + let (nowhere, unknown) = ( + vec![libc::ENOENT, libc::ENOTDIR, libc::ELOOP], + vec![libc::EACCES, libc::EIO, libc::ENAMETOOLONG, libc::ETIMEDOUT], + ); + // By name: not found (file, path) and a loop; then access denied, + // no such drive, a sharing violation, a network path and a share + // that cannot be reached, a name that is no name, a name that is + // no directory, a link the system may not follow, and a link of a + // kind that is switched off. + #[cfg(windows)] + let (nowhere, unknown) = ( + vec![2, 3, 1921], + vec![5, 15, 32, 53, 67, 123, 267, 1920, 1463], + ); + for code in nowhere { + let error = std::io::Error::from_raw_os_error(code); + assert!(leads_nowhere(&error), "{error:?}"); + } + for code in unknown { + let error = std::io::Error::from_raw_os_error(code); + assert!(!leads_nowhere(&error), "{error:?}"); + } + // Windows goes by the code alone, not by the kind `std` gives it. + let unnamed = std::io::Error::new(std::io::ErrorKind::NotFound, "no code"); + assert_eq!(leads_nowhere(&unnamed), cfg!(not(windows))); + + // A read that finds nothing: by the code where the system gave + // one, so a network path that cannot be reached is no file that + // is missing on Windows, and by the kind where it gave none. + assert!(not_there(&unnamed)); + #[cfg(unix)] + let (missing, unknown) = (vec![libc::ENOENT], vec![libc::EACCES, libc::ENOTDIR]); + #[cfg(windows)] + let (missing, unknown) = (vec![2, 3], vec![5, 15, 53, 67, 1921]); + for code in missing { + let error = std::io::Error::from_raw_os_error(code); + assert!(not_there(&error), "{error:?}"); + } + for code in unknown { + let error = std::io::Error::from_raw_os_error(code); + assert!(!not_there(&error), "{error:?}"); + } } /// #599: Bun never prunes `.bun`. After an in-place `bun install` that diff --git a/crates/socket-patch-core/src/formats/pnpm/modules_yaml.rs b/crates/socket-patch-core/src/formats/pnpm/modules_yaml.rs index 8baa1a406..3d8d0285a 100644 --- a/crates/socket-patch-core/src/formats/pnpm/modules_yaml.rs +++ b/crates/socket-patch-core/src/formats/pnpm/modules_yaml.rs @@ -28,7 +28,7 @@ pub(crate) struct Layout { shamefully_hoist: Option, public_hoist_pattern: Option>, /// The virtual store, relative to `node_modules` (absolute on old - /// pnpm). + /// pnpm, and on Windows). pub(crate) virtual_store_dir: Option, } diff --git a/docs/ecosystems.md b/docs/ecosystems.md index e2f8365f7..bac057793 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -891,11 +891,13 @@ For pnpm the rule is narrow: - For a version in two entries, every symbolic link below one directory is read: the scanned directory, or the project that holds the store's `node_modules` when the scan started inside it (a run from a workspace - member reads the whole project). No directory is skipped by name, so a - link in `vendor/`, `dist/`, a hidden directory, a package's own - `node_modules` or a directory that left the workspace counts. Nothing - above that directory is read for its own sake: pnpm writes no link - there into a store below it. + member reads the whole project). On Windows a junction is read as + well: pnpm links a directory with a symbolic link where it may make + one, and with a junction where it may not. No directory is skipped by + name, so a link in `vendor/`, `dist/`, a hidden directory, a package's + own `node_modules` or a directory that left the workspace counts. + Nothing above that directory is read for its own sake: pnpm writes no + link there into a store below it. - A link to a directory outside that directory is followed, since node loads what is behind it from where it really is. The directory is read the same way, and so is each `node_modules` in a directory above it (a @@ -906,7 +908,8 @@ For pnpm the rule is narrow: read. A directory above the project is read only this way: when a link leads to it, or its `node_modules` sits above what a link leads to. Only links are read there, as in the project: a path out of such - a directory that a file spells out (`require('../util')`) is not. + a directory that a file spells out (`require('../util')`) is not. On + Windows such a link is not followed, and keeps every entry (below). - An entry is judged when a link that sits outside the store's entries resolves into it (into its package, a file of it, or the entry itself), or when a link inside a judged entry does. The store's hoist directory, @@ -941,7 +944,6 @@ dependent). Wherever a link into an entry may have gone unread, every entry is judged, as before: -- on Windows, where pnpm links directories with junctions; - when the scanned directory neither holds the store's project nor lies inside it: a `--global` run, or a `node_modules` that is a link to another checkout's (a git worktree that shares an install); @@ -975,8 +977,12 @@ judged, as before: target of a link cannot be read, or all that is read comes to more than 500,000 directories and links. A link that leads nowhere is left out: its target is not there, it loops, or its path passes through a - file; + file. On Windows a link into a drive that is not there, or to a + network path that cannot be reached, is not such a link: the drive may + be there for another session, so what it leads to cannot be told; - when a link leads to the home directory or to a directory above it; +- on Windows, when a link leads to a directory outside the one that is + read: what lies behind it is not read there; - when a link leads to a file outside what is read and a directory above that file holds a `node_modules`: what the file loads by paths of its own is not read. The home directory and what lies above it are not diff --git a/docs/testing/pnpm-compatibility.md b/docs/testing/pnpm-compatibility.md index 12398e43a..b202f7ea9 100644 --- a/docs/testing/pnpm-compatibility.md +++ b/docs/testing/pnpm-compatibility.md @@ -276,7 +276,7 @@ that the `pnpm install` the scan asks for, run over a `node_modules` that holds the upstream packages, links the patched entries and keeps the upstream ones in the store, where `vex` attests both patches from the linked copies and refuses a linked copy that is put back on the upstream bytes (see "The install -over an existing `node_modules`" below; not on Windows); +over an existing `node_modules`" below); that `pnpm add`, `pnpm update`, `pnpm dedupe` and `pnpm install --fix-lockfile` keep both packages routed and patched; and that after each cleanup (a @@ -396,7 +396,7 @@ version that sits in two entries of one store with other bytes in each they read every link below the scanned directory, and leave out an entry that no path from outside the store's entries leads into (see "npm: which `node_modules` trees are crawled" in [ecosystems](../ecosystems.md) for the rule and for the layouts -where every entry is still judged, Windows among them). There the upstream +where every entry is still judged). There the upstream entry reads as an unpatched copy until it is gone: `vex` omits a hosted patch as `not_applied`, or warns `vendored_tree_out_of_sync` for a vendored one. `pnpm prune` removes the entry at once (measured on 11.29.0 and @@ -414,6 +414,55 @@ a warning; a link to the upstream entry in `bazel-bin`, in an external repository's contents, or in a `node_modules` above the output tree's workspace directory brings the warning back. +Windows is judged the same way, but for a link that leads out of the +scanned directory to a directory: it is not followed there, and keeps +every entry. Measured with pnpm 11 and 12 on a `windows-latest` CI runner, +on the registry route, in the workspace of the +`*_in_place_install_is_judged_by_every_link` legs below: + +- The install leaves the same entries. `leaf@1.0.0` stays beside + `leaf@socket-+1.0.0` with the upstream bytes, and the old entries of + the dependents keep their links to it. The new entry of the peer-dependent + tarball package gets a hashed name there + (`needs-leaf@file+needs-leaf-_`), since pnpm's + `virtualStoreDirMaxLength` is shorter on Windows. The rule does not read + an entry's name. +- pnpm links a directory with a symbolic link where it may make one (on + that runner, with a relative target) and with a junction where it may + not. Both are links to `vex`, and no directory there resolves elsewhere + without being one. +- A real path is spelled `\\?\C:\…`, with every short name written out + (`RUNNER~1` as `runneradmin`), the same way for the store and for the + target of each link, so the two compare. +- A launcher is three files (`.bin/`, `.cmd`, `.ps1`) that + hold absolute paths with backslashes. After the install the ones that + name the old entry sit inside the old entries, which nothing links. + +Read from pnpm's source, not measured: `writeModulesManifest` of 9.15.9 and +11.27.0 records the virtual store in `.modules.yaml` by its absolute path +on Windows, and by its path from the modules dir elsewhere. Either way the +record is the store's when the path it names resolves to the store. + +Resolving a junction fails with `ERROR_FILE_NOT_FOUND` when its target is +not there, with `ERROR_PATH_NOT_FOUND` when a directory on the way to the +target is not there or is a file, and with `ERROR_CANT_RESOLVE_FILENAME` +when it leads to itself (measured on the same runner, by +`test_links_that_lead_nowhere_fail_with_the_errors_read_as_such`). Those +three are the links that lead nowhere on Windows, and only when the +target fails the same way when it is asked for by itself: a target on a +volume without a drive letter is there, but has no path to name it by. +Every other failure keeps every entry: access that is denied, a network +path that cannot be reached, a link Windows may not follow. So does a +link whose target names a drive or a share that is not there, whatever +it fails with: a mapped drive is there for one session and not for +another (`test_link_into_a_drive_that_is_not_there_does_not_lead_nowhere`, +where a junction into such a drive can be made). Not measured: the code +for a junction to a file. The +identity of a file, which tells the store from another spelling of its +path, is the serial number of its volume and its index there, read from +a handle that is opened without access to what the file holds. A +directory above the store is not asked for it. + A workspace with a `modulesDir` of more than one level is judged from the workspace's root. Measured with `modulesDir: sub/node_modules`: pnpm 10.12.1, 10.34.9, 11.29.0 and 12.25.1 put the virtual store in @@ -433,7 +482,7 @@ upstream entry from `vendor/`, which brings the warning back. Two more registry-route legs run the install in a workspace whose patched package is hoisted and is also the peer dependency of a tarball package (`*_in_place_install_is_judged_by_every_link`, -`*_in_place_install_under_a_nested_modules_dir`; not on Windows): `vex` +`*_in_place_install_under_a_nested_modules_dir`): `vex` attests both patches; a link to the upstream entry from `vendor/`, `dist/`, a hidden directory or a package's own `node_modules` refuses the patch until the link is gone; and under the nested `modulesDir` both patches are @@ -443,9 +492,22 @@ upstream entry. Where `vex` attests, a second reading of the links that no path from outside the store's entries leads into an entry that holds the upstream bytes, and where a link was added it must find that link. Hand-built stores of the same shapes cover the rule offline: -`crawlers::npm_crawler` (`test_pnpm_store_*`, `test_links_below_*`), -`e2e_vex_redirect`, `e2e_vex_vendor`, the `apply --check` suite and the -`rollback` suite, which restores an unlinked entry. +`crawlers::npm_crawler` (`test_pnpm_store_*`, `test_links_below_*`, +`test_links_that_lead_nowhere_*`), `e2e_vex_redirect`, `e2e_vex_vendor`, +the `apply --check` suite and the `rollback` suite, which restores an +unlinked entry. + +The registry-route legs run on Linux, macOS and Windows. On Windows the +links a leg adds are junctions, beside the symbolic links pnpm writes on +the runner, and the unit tests of `crawlers::npm_crawler` link their stores +with junctions there (one with symbolic links, as that runner's are). A +case that links a file runs only where a symbolic link to a file can be +made: Windows has no junction to a file, and a symbolic link takes Developer +Mode or an elevated process. The cases that take a directory away with +`chmod`, link a named pipe, or spell a path through macOS's +`/System/Volumes/Data` do not run on Windows. The vendored capstone has +no leg on the Windows runner, and its in-place steps are skipped on +Windows until it has one. ## Run locally