From 8670d19dc47795d16a0c40bed16ee05f48781c39 Mon Sep 17 00:00:00 2001 From: Arnaud Dezandee Date: Sat, 14 Nov 2026 20:51:14 +0100 Subject: [PATCH 1/3] fix(workspace): link npm, Yarn and Bun workspace packages by version range npm and Yarn classic reject the `workspace:` protocol, so in their workspaces every dependency between packages is a plain range like `*` or `^1.0.0`, and the package graph had no edges. npm, Yarn and Bun link a workspace package when its `version` satisfies the range; the graph now does the same for `package.json` `workspaces`, following npm's rule that `*` and an empty range match any version, prereleases included. pnpm workspaces are unchanged: pnpm links such ranges only with `linkWorkspacePackages`, which isn't read yet. Co-authored-by: Claude Opus 5.5 --- CHANGELOG.md | 1 + Cargo.lock | 7 +++ Cargo.toml | 1 + crates/vt_workspace/Cargo.toml | 1 + crates/vt_workspace/src/lib.rs | 95 ++++++++++++++++++++++-------- crates/vt_workspace/src/package.rs | 31 ++++++++-- deny.toml | 1 + 7 files changed, 110 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d5cfa856..5ae33cf59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog +- **Fixed** In npm, Yarn and Bun workspaces, a dependency on a workspace package by version range, like `"@x/a": "*"` or `"@x/a": "^1.0.0"`, now links the two packages when the workspace package's `version` satisfies the range, as these package managers do. Before, only `workspace:` dependencies did, which npm rejects, so `--filter "...@x/a"`, `-t`, `-r` and `dependsOn` with `from: "dependencies"` ignored dependencies between packages in an npm workspace. pnpm workspaces still need `workspace:` ([#???](https://github.com/voidzero-dev/vite-task/pull/???)). - **Fixed** On Windows, tasks with automatic file-access tracking can now run Git Bash's `sh` and other MSYS2 or Cygwin programs, which aborted with `fatal error - MapViewOfFileEx 'shared.5'(…) Win32 error 487` ([#830](https://github.com/voidzero-dev/vite-task/issues/830), [#952](https://github.com/voidzero-dev/vite-task/pull/952)). - **Fixed** On Linux, cached tasks no longer fail to spawn child processes in restricted sandboxes, for example rootless bubblewrap that denies the `seccomp` syscall. When the `seccomp` filter for file-access tracking cannot be installed, the process runs untracked instead and the run is reported as not cached. The not-cached message now reads "automatic file tracking couldn't record everything this task did", which fits both this case and a full tracking channel ([#700](https://github.com/voidzero-dev/vite-task/issues/700), [#701](https://github.com/voidzero-dev/vite-task/pull/701)). - **Fixed** `vp run ` in a package that doesn't define the task now reports `Task "" not found` when another package's `vite.config.*` fails to load, instead of failing with that package's error. Showing the tasks to choose from loads every package's config, so the task selector or task list is skipped then ([#987](https://github.com/voidzero-dev/vite-task/pull/987)). diff --git a/Cargo.lock b/Cargo.lock index 45cdea086..121d492ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2264,6 +2264,12 @@ dependencies = [ "libc", ] +[[package]] +name = "js-semver" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45bf431971205e414201e87a76bdd4c75316dc44c890e9df4d9006ee5d2896c" + [[package]] name = "js-sys" version = "0.3.85" @@ -5421,6 +5427,7 @@ name = "vt_workspace" version = "0.0.0" dependencies = [ "clap", + "js-semver", "petgraph", "rustc-hash", "serde", diff --git a/Cargo.toml b/Cargo.toml index 63c33d9ba..1b9465bfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,7 @@ globset = "0.4.18" getrandom = "0.4.2" hashbrown = { version = "0.16.1", default-features = false, features = ["inline-more"] } itoa = "1.0.17" +js-semver = "1.0.1" jsonc-parser = "0.32.0" libc = "0.2.185" linux-raw-sys = { version = "0.12", default-features = false } diff --git a/crates/vt_workspace/Cargo.toml b/crates/vt_workspace/Cargo.toml index 54346e0c2..b10adc9a4 100644 --- a/crates/vt_workspace/Cargo.toml +++ b/crates/vt_workspace/Cargo.toml @@ -9,6 +9,7 @@ rust-version.workspace = true [dependencies] clap = { workspace = true, features = ["derive"] } +js-semver = { workspace = true } petgraph = { workspace = true, features = ["serde-1"] } rustc-hash = { workspace = true } serde = { workspace = true, features = ["derive"] } diff --git a/crates/vt_workspace/src/lib.rs b/crates/vt_workspace/src/lib.rs index 349f28e1e..4b395d92b 100644 --- a/crates/vt_workspace/src/lib.rs +++ b/crates/vt_workspace/src/lib.rs @@ -18,6 +18,7 @@ use wax::{ walk::{Entry as _, FileIterator as _}, }; +use crate::package::version_satisfies; pub use crate::{ error::Error, package::{DependencyType, PackageJson}, @@ -202,9 +203,13 @@ pub struct PackageInfo { pub absolute_path: Arc, } +/// A dependency's package name, type, and the version range the workspace package must satisfy. +type WorkspaceDependency = (Str, DependencyType, Str); + #[derive(Default)] struct PackageGraphBuilder { - id_and_deps_by_path: HashMap)>, + link_workspace_packages: bool, + id_and_deps_by_path: HashMap)>, name_to_path: HashMap>, graph: DiGraph, } @@ -216,7 +221,9 @@ impl PackageGraphBuilder { absolute_path: Arc, package_json: PackageJson, ) { - let deps = package_json.get_workspace_dependencies().collect::>(); + let deps = package_json + .get_workspace_dependencies(self.link_workspace_packages) + .collect::>(); let package_name = package_json.name.clone(); let id = self.graph.add_node(PackageInfo { package_json, @@ -240,7 +247,7 @@ impl PackageGraphBuilder { fn build(mut self) -> Result, Error> { for (id, deps) in self.id_and_deps_by_path.values() { - for (dep_name, dep_type) in deps { + for (dep_name, dep_type, range) in deps { // Skip dependencies on nameless packages (empty string) // These can't be referenced anyway if dep_name.is_empty() { @@ -260,7 +267,9 @@ impl PackageGraphBuilder { } // Skip self-referential edges: a package listing itself as a dependency // (e.g. for testing purposes) must not create a cycle in the task graph. - if *id != *dep_id { + if *id != *dep_id + && version_satisfies(&self.graph[*dep_id].package_json.version, range) + { self.graph.add_edge(*id, *dep_id, *dep_type); } } @@ -321,6 +330,7 @@ pub fn load_package_graph( WorkspaceMemberGlobs::from_pnpm_workspace_yaml(file_with_path)? } WorkspaceFile::NpmWorkspaceJson(file_with_path) => { + graph_builder.link_workspace_packages = true; WorkspaceMemberGlobs::from_npm_workspace_json(file_with_path)? } WorkspaceFile::NonWorkspacePackage(file_with_path) => { @@ -535,9 +545,12 @@ mod tests { // Create package A fs::create_dir_all(temp_dir_path.join("packages/pkg-a")).unwrap(); + // pnpm links a workspace package for a semver range only with `linkWorkspacePackages` let pkg_a = serde_json::json!({ "name": "pkg-a", - "dependencies": {} + "dependencies": { + "pkg-b": "*" + } }); fs::write(temp_dir_path.join("packages/pkg-a/package.json"), pkg_a.to_string()).unwrap(); @@ -1249,6 +1262,14 @@ mod tests { }); fs::write(temp_dir_path.join("libs/database/package.json"), db_pkg.to_string()).unwrap(); + fs::create_dir_all(temp_dir_path.join("libs/codegen")).unwrap(); + let codegen_pkg = serde_json::json!({ + "name": "@company/codegen", + "version": "0.0.0-dev" + }); + fs::write(temp_dir_path.join("libs/codegen/package.json"), codegen_pkg.to_string()) + .unwrap(); + // Create service with various workspace protocol formats fs::create_dir_all(temp_dir_path.join("services/api")).unwrap(); let api_pkg = serde_json::json!({ @@ -1261,28 +1282,56 @@ mod tests { }); fs::write(temp_dir_path.join("services/api/package.json"), api_pkg.to_string()).unwrap(); - let graph = discover_package_graph(temp_dir_path).unwrap(); - - // Verify packages - assert_eq!(graph.node_count(), 3); // root + database + api - - // Verify workspace dependency exists but not external dependency - let mut found_workspace_dep = false; - for edge_ref in graph.edge_references() { - let source = &graph[edge_ref.source()]; - let target = &graph[edge_ref.target()]; + // npm, Yarn and Bun link a workspace package whose version satisfies the range + fs::create_dir_all(temp_dir_path.join("services/worker")).unwrap(); + let worker_pkg = serde_json::json!({ + "name": "worker-service", + "dependencies": { + "@company/database": "^3.0.0" + }, + "devDependencies": { + "@company/codegen": " * " + } + }); + fs::write(temp_dir_path.join("services/worker/package.json"), worker_pkg.to_string()) + .unwrap(); - if source.package_json.name == "api-service" - && target.package_json.name == "@company/database" - { - found_workspace_dep = true; + // and install from the registry when it doesn't + fs::create_dir_all(temp_dir_path.join("services/legacy")).unwrap(); + let legacy_pkg = serde_json::json!({ + "name": "legacy-service", + "dependencies": { + "@company/database": "^2.0.0", + "@company/codegen": "^0.0.0" } - } + }); + fs::write(temp_dir_path.join("services/legacy/package.json"), legacy_pkg.to_string()) + .unwrap(); - assert!(found_workspace_dep, "Should have found workspace dependency"); + let graph = discover_package_graph(temp_dir_path).unwrap(); - // External dependencies should not create edges - assert_eq!(graph.edge_count(), 1, "Should only have one edge for workspace dependency"); + // root + database + codegen + api + worker + legacy + assert_eq!(graph.node_count(), 6); + + let mut edges: Vec<_> = graph + .edge_references() + .map(|edge_ref| { + ( + graph[edge_ref.source()].package_json.name.as_str(), + graph[edge_ref.target()].package_json.name.as_str(), + *edge_ref.weight(), + ) + }) + .collect(); + edges.sort_unstable_by_key(|(source, target, _)| (*source, *target)); + assert_eq!( + edges, + [ + ("api-service", "@company/database", DependencyType::Normal), + ("worker-service", "@company/codegen", DependencyType::Dev), + ("worker-service", "@company/database", DependencyType::Normal), + ] + ); } #[test] diff --git a/crates/vt_workspace/src/package.rs b/crates/vt_workspace/src/package.rs index 2a6d9fcc0..d514f4bd8 100644 --- a/crates/vt_workspace/src/package.rs +++ b/crates/vt_workspace/src/package.rs @@ -1,3 +1,4 @@ +use js_semver::{Range, Version}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; use vt_str::Str; @@ -16,6 +17,8 @@ pub struct PackageJson { #[serde(default)] pub name: Str, #[serde(default)] + pub version: Str, + #[serde(default)] pub scripts: FxHashMap, #[serde(default)] pub dependencies: FxHashMap, @@ -44,18 +47,24 @@ impl std::fmt::Debug for PackageJson { } impl PackageJson { + /// Returns the dependencies that can resolve to a workspace package, each with the version range that package must + /// satisfy, `*` for a `workspace:` dependency. + /// + /// `link_workspace_packages` also returns dependencies that aren't `workspace:`, for package managers that link a + /// workspace package whenever its version satisfies the range, as npm, Yarn and Bun do. pub fn get_workspace_dependencies( &self, - ) -> impl Iterator + use<'_> { + link_workspace_packages: bool, + ) -> impl Iterator + use<'_> { self.dependencies .iter() .map(|entry| (entry, DependencyType::Normal)) .chain(self.dev_dependencies.iter().map(|entry| (entry, DependencyType::Dev))) .chain(self.peer_dependencies.iter().map(|entry| (entry, DependencyType::Peer))) - .filter_map(|((key, value), dep_type)| { + .filter_map(move |((key, value), dep_type)| { let Some(workspace_version) = value.strip_prefix("workspace:") else { - // TODO: support link-workspace-packages: https://pnpm.io/workspaces#workspace-protocol-workspace) - return None; + // TODO: support pnpm's `linkWorkspacePackages`: https://pnpm.io/settings#linkworkspacepackages + return link_workspace_packages.then(|| (key.clone(), dep_type, value.clone())); }; // TODO: support paths: https://github.com/pnpm/pnpm/pull/2972 Some(( @@ -65,7 +74,21 @@ impl PackageJson { key.clone() }, dep_type, + "*".into(), )) }) } } + +/// Whether a workspace package at `version` satisfies the dependency `range`, following npm: `*` and an empty range +/// match any version, prereleases included, and anything that isn't a semver range, like `file:` or a tag, matches +/// none. Yarn excludes prereleases from `*`; linking them anyway only adds an ordering edge Yarn wouldn't have. +/// +/// +pub(crate) fn version_satisfies(version: &str, range: &str) -> bool { + let range = range.trim(); + range.is_empty() + || range == "*" + || Version::parse(version) + .is_ok_and(|version| Range::parse(range).is_ok_and(|range| range.satisfies(&version))) +} diff --git a/deny.toml b/deny.toml index bf028f611..6acaf9f78 100644 --- a/deny.toml +++ b/deny.toml @@ -53,6 +53,7 @@ allow = [ "BSD-3-Clause", "ISC", "MIT", + "MIT-0", "MPL-2.0", "OpenSSL", "Unicode-DFS-2016", From 41a05cde78351273390600fa7e3c9675f74b49e0 Mon Sep 17 00:00:00 2001 From: Arnaud Dezandee Date: Sat, 14 Nov 2026 21:21:10 +0100 Subject: [PATCH 2/3] docs(changelog): link #999 Co-authored-by: Claude Opus 5.5 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ae33cf59..5539c86af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -- **Fixed** In npm, Yarn and Bun workspaces, a dependency on a workspace package by version range, like `"@x/a": "*"` or `"@x/a": "^1.0.0"`, now links the two packages when the workspace package's `version` satisfies the range, as these package managers do. Before, only `workspace:` dependencies did, which npm rejects, so `--filter "...@x/a"`, `-t`, `-r` and `dependsOn` with `from: "dependencies"` ignored dependencies between packages in an npm workspace. pnpm workspaces still need `workspace:` ([#???](https://github.com/voidzero-dev/vite-task/pull/???)). +- **Fixed** In npm, Yarn and Bun workspaces, a dependency on a workspace package by version range, like `"@x/a": "*"` or `"@x/a": "^1.0.0"`, now links the two packages when the workspace package's `version` satisfies the range, as these package managers do. Before, only `workspace:` dependencies did, which npm rejects, so `--filter "...@x/a"`, `-t`, `-r` and `dependsOn` with `from: "dependencies"` ignored dependencies between packages in an npm workspace. pnpm workspaces still need `workspace:` ([#999](https://github.com/voidzero-dev/vite-task/pull/999)). - **Fixed** On Windows, tasks with automatic file-access tracking can now run Git Bash's `sh` and other MSYS2 or Cygwin programs, which aborted with `fatal error - MapViewOfFileEx 'shared.5'(…) Win32 error 487` ([#830](https://github.com/voidzero-dev/vite-task/issues/830), [#952](https://github.com/voidzero-dev/vite-task/pull/952)). - **Fixed** On Linux, cached tasks no longer fail to spawn child processes in restricted sandboxes, for example rootless bubblewrap that denies the `seccomp` syscall. When the `seccomp` filter for file-access tracking cannot be installed, the process runs untracked instead and the run is reported as not cached. The not-cached message now reads "automatic file tracking couldn't record everything this task did", which fits both this case and a full tracking channel ([#700](https://github.com/voidzero-dev/vite-task/issues/700), [#701](https://github.com/voidzero-dev/vite-task/pull/701)). - **Fixed** `vp run ` in a package that doesn't define the task now reports `Task "" not found` when another package's `vite.config.*` fails to load, instead of failing with that package's error. Showing the tasks to choose from loads every package's config, so the task selector or task list is skipped then ([#987](https://github.com/voidzero-dev/vite-task/pull/987)). From 2c388a41cd2bdbbac97833f3ab2306dd13e0b298 Mon Sep 17 00:00:00 2001 From: Arnaud Dezandee Date: Sat, 14 Nov 2026 22:58:56 +0100 Subject: [PATCH 3/3] refactor(workspace): trim comments Co-authored-by: Claude Opus 5.5 --- crates/vt_workspace/src/lib.rs | 4 ---- crates/vt_workspace/src/package.rs | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/crates/vt_workspace/src/lib.rs b/crates/vt_workspace/src/lib.rs index 4b395d92b..50c323195 100644 --- a/crates/vt_workspace/src/lib.rs +++ b/crates/vt_workspace/src/lib.rs @@ -203,7 +203,6 @@ pub struct PackageInfo { pub absolute_path: Arc, } -/// A dependency's package name, type, and the version range the workspace package must satisfy. type WorkspaceDependency = (Str, DependencyType, Str); #[derive(Default)] @@ -1282,7 +1281,6 @@ mod tests { }); fs::write(temp_dir_path.join("services/api/package.json"), api_pkg.to_string()).unwrap(); - // npm, Yarn and Bun link a workspace package whose version satisfies the range fs::create_dir_all(temp_dir_path.join("services/worker")).unwrap(); let worker_pkg = serde_json::json!({ "name": "worker-service", @@ -1296,7 +1294,6 @@ mod tests { fs::write(temp_dir_path.join("services/worker/package.json"), worker_pkg.to_string()) .unwrap(); - // and install from the registry when it doesn't fs::create_dir_all(temp_dir_path.join("services/legacy")).unwrap(); let legacy_pkg = serde_json::json!({ "name": "legacy-service", @@ -1310,7 +1307,6 @@ mod tests { let graph = discover_package_graph(temp_dir_path).unwrap(); - // root + database + codegen + api + worker + legacy assert_eq!(graph.node_count(), 6); let mut edges: Vec<_> = graph diff --git a/crates/vt_workspace/src/package.rs b/crates/vt_workspace/src/package.rs index d514f4bd8..de6f1b3ff 100644 --- a/crates/vt_workspace/src/package.rs +++ b/crates/vt_workspace/src/package.rs @@ -63,7 +63,6 @@ impl PackageJson { .chain(self.peer_dependencies.iter().map(|entry| (entry, DependencyType::Peer))) .filter_map(move |((key, value), dep_type)| { let Some(workspace_version) = value.strip_prefix("workspace:") else { - // TODO: support pnpm's `linkWorkspacePackages`: https://pnpm.io/settings#linkworkspacepackages return link_workspace_packages.then(|| (key.clone(), dep_type, value.clone())); }; // TODO: support paths: https://github.com/pnpm/pnpm/pull/2972 @@ -80,9 +79,7 @@ impl PackageJson { } } -/// Whether a workspace package at `version` satisfies the dependency `range`, following npm: `*` and an empty range -/// match any version, prereleases included, and anything that isn't a semver range, like `file:` or a tag, matches -/// none. Yarn excludes prereleases from `*`; linking them anyway only adds an ordering edge Yarn wouldn't have. +/// `*` and an empty range match any version, prereleases included, as npm does. /// /// pub(crate) fn version_satisfies(version: &str, range: &str) -> bool {