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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 crates/vite_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
serde_yml = { workspace = true }
thiserror = { workspace = true }
vec1 = { workspace = true, features = ["smallvec-v1"] }
vite_glob = { workspace = true }
vite_path = { workspace = true }
vite_str = { workspace = true }
Expand Down
66 changes: 29 additions & 37 deletions crates/vite_workspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ mod error;
pub mod package;
mod package_manager;

use std::{fs, io};
use std::{collections::hash_map::Entry, fs, io};

use petgraph::graph::{DefaultIx, DiGraph, EdgeIndex, IndexType, NodeIndex};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use serde::{Deserialize, Serialize};
use vec1::smallvec_v1::SmallVec1;
use vite_glob::GlobPatternSet;
use vite_path::{AbsolutePath, AbsolutePathBuf, RelativePathBuf};
use vite_str::Str;
Expand Down Expand Up @@ -104,17 +105,12 @@ pub struct PackageInfo {
#[derive(Default)]
struct PackageGraphBuilder {
id_and_deps_by_path: HashMap<RelativePathBuf, (PackageNodeIndex, Vec<(Str, DependencyType)>)>,
// Only for packages with a name
name_to_path: HashMap<Str, RelativePathBuf>,
name_to_path: HashMap<Str, SmallVec1<[RelativePathBuf; 1]>>,
graph: DiGraph<PackageInfo, DependencyType, PackageIx>,
}

impl PackageGraphBuilder {
fn add_package(
&mut self,
package_path: RelativePathBuf,
package_json: PackageJson,
) -> Result<(), Error> {
fn add_package(&mut self, package_path: RelativePathBuf, package_json: PackageJson) {
let deps = package_json.get_workspace_dependencies().collect::<Vec<_>>();
let package_name = package_json.name.clone();
let id = self.graph.add_node(PackageInfo { package_json, path: package_path.clone() });
Expand All @@ -123,22 +119,17 @@ impl PackageGraphBuilder {
self.id_and_deps_by_path.insert(package_path.clone(), (id, deps));

// Also maintain name to path mapping for dependency resolution
if !package_name.is_empty()
&& let Some(existing_path) = self.name_to_path.insert(package_name, package_path)
{
// Duplicate package name found
let existing_id = self.id_and_deps_by_path.get(&existing_path).unwrap().0;
let existing_package_info = &self.graph[existing_id];
return Err(Error::DuplicatedPackageName {
name: existing_package_info.package_json.name.clone(),
path1: existing_package_info.path.clone(),
path2: self.graph[id].path.clone(),
});
match self.name_to_path.entry(package_name.clone()) {
Entry::Vacant(entry) => {
entry.insert(SmallVec1::new(package_path));
}
Entry::Occupied(mut entry) => {
entry.get_mut().push(package_path);
}
}
Ok(())
}

fn build(mut self) -> DiGraph<PackageInfo, DependencyType, PackageIx> {
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 {
// Skip dependencies on nameless packages (empty string)
Expand All @@ -148,15 +139,22 @@ impl PackageGraphBuilder {
}

// Resolve dependency name to path, then find the node
if let Some(dep_path) = self.name_to_path.get(dep_name)
&& let Some((dep_id, _)) = self.id_and_deps_by_path.get(dep_path)
if let Some(dep_paths) = self.name_to_path.get(dep_name)
&& let Some((dep_id, _)) = self.id_and_deps_by_path.get(dep_paths.first())
{
if let [dep_path1, dep_path2, ..] = dep_paths.as_slice() {
return Err(Error::DuplicatedPackageName {
name: dep_name.clone(),
path1: dep_path1.clone(),
path2: dep_path2.clone(),
});
}
self.graph.add_edge(*id, *dep_id, *dep_type);
}
// Silently skip if dependency not found - it might be an external package
}
}
self.graph
Ok(self.graph)
}
}

Expand Down Expand Up @@ -205,9 +203,9 @@ pub fn load_package_graph(
WorkspaceFile::NonWorkspacePackage(file) => {
// For non-workspace packages, add the package.json to the graph as a root package
let package_json: PackageJson = serde_json::from_reader(file)?;
graph_builder.add_package(RelativePathBuf::default(), package_json)?;
graph_builder.add_package(RelativePathBuf::default(), package_json);

return Ok(graph_builder.build());
return graph_builder.build();
}
};

Expand All @@ -224,15 +222,15 @@ pub fn load_package_graph(
};

has_root_package = has_root_package || package_path.as_str().is_empty();
graph_builder.add_package(package_path, package_json)?;
graph_builder.add_package(package_path, package_json);
}
// try add the root package anyway if the member globs do not include it.
if !has_root_package {
let package_json_path = workspace_root.path.join("package.json");
match fs::read(&package_json_path) {
Ok(package_json) => {
let package_json: PackageJson = serde_json::from_slice(&package_json)?;
graph_builder.add_package(RelativePathBuf::default(), package_json)?;
graph_builder.add_package(RelativePathBuf::default(), package_json);
}
Err(err) => {
if err.kind() != io::ErrorKind::NotFound {
Expand All @@ -241,7 +239,7 @@ pub fn load_package_graph(
}
}
}
Ok(graph_builder.build())
graph_builder.build()
}

#[cfg(test)]
Expand Down Expand Up @@ -537,15 +535,9 @@ mod tests {
});
fs::write(temp_dir_path.join("packages/pkg-2/package.json"), pkg_2.to_string()).unwrap();

// Should return an error for duplicate package names
// duplicate package names is allowed.
let result = discover_package_graph(temp_dir_path);
assert!(result.is_err());

if let Err(Error::DuplicatedPackageName { name, .. }) = result {
assert_eq!(name, "duplicate");
} else {
panic!("Expected DuplicatedPackageName error, got: {result:?}");
}
assert!(result.is_ok());
}

#[test]
Expand Down