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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:` ([#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 <task>` in a package that doesn't define the task now reports `Task "<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)).
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions crates/vt_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
91 changes: 68 additions & 23 deletions crates/vt_workspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use wax::{
walk::{Entry as _, FileIterator as _},
};

use crate::package::version_satisfies;
pub use crate::{
error::Error,
package::{DependencyType, PackageJson},
Expand Down Expand Up @@ -202,9 +203,12 @@ pub struct PackageInfo {
pub absolute_path: Arc<AbsolutePath>,
}

type WorkspaceDependency = (Str, DependencyType, Str);

#[derive(Default)]
struct PackageGraphBuilder {
id_and_deps_by_path: HashMap<RelativePathBuf, (PackageNodeIndex, Vec<(Str, DependencyType)>)>,
link_workspace_packages: bool,
id_and_deps_by_path: HashMap<RelativePathBuf, (PackageNodeIndex, Vec<WorkspaceDependency>)>,
name_to_path: HashMap<Str, SmallVec1<[RelativePathBuf; 1]>>,
graph: DiGraph<PackageInfo, DependencyType, PackageIx>,
}
Expand All @@ -216,7 +220,9 @@ impl PackageGraphBuilder {
absolute_path: Arc<AbsolutePath>,
package_json: PackageJson,
) {
let deps = package_json.get_workspace_dependencies().collect::<Vec<_>>();
let deps = package_json
.get_workspace_dependencies(self.link_workspace_packages)
.collect::<Vec<_>>();
let package_name = package_json.name.clone();
let id = self.graph.add_node(PackageInfo {
package_json,
Expand All @@ -240,7 +246,7 @@ impl PackageGraphBuilder {

fn build(mut self) -> Result<DiGraph<PackageInfo, DependencyType, PackageIx>, 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() {
Expand All @@ -260,7 +266,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);
}
}
Expand Down Expand Up @@ -321,6 +329,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) => {
Expand Down Expand Up @@ -535,9 +544,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();

Expand Down Expand Up @@ -1249,6 +1261,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!({
Expand All @@ -1261,28 +1281,53 @@ 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()];
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;
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");
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]
Expand Down
28 changes: 24 additions & 4 deletions crates/vt_workspace/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use js_semver::{Range, Version};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use vt_str::Str;
Expand All @@ -16,6 +17,8 @@ pub struct PackageJson {
#[serde(default)]
pub name: Str,
#[serde(default)]
pub version: Str,
#[serde(default)]
pub scripts: FxHashMap<Str, Str>,
#[serde(default)]
pub dependencies: FxHashMap<Str, Str>,
Expand Down Expand Up @@ -44,18 +47,23 @@ 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<Item = (Str, DependencyType)> + use<'_> {
link_workspace_packages: bool,
) -> impl Iterator<Item = (Str, DependencyType, Str)> + 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;
return link_workspace_packages.then(|| (key.clone(), dep_type, value.clone()));
};
// TODO: support paths: https://github.com/pnpm/pnpm/pull/2972
Some((
Expand All @@ -65,7 +73,19 @@ impl PackageJson {
key.clone()
},
dep_type,
"*".into(),
))
})
}
}

/// `*` and an empty range match any version, prereleases included, as npm does.
///
/// <https://github.com/npm/cli/blob/latest/workspaces/arborist/lib/dep-valid.js>
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)))
}
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ allow = [
"BSD-3-Clause",
"ISC",
"MIT",
"MIT-0",
"MPL-2.0",
"OpenSSL",
"Unicode-DFS-2016",
Expand Down