From 29186d7e5963f2398b28ee2c043b27e4881075ef Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 15 Jul 2024 15:08:51 -0400 Subject: fix(workspace): do not resolve to self for npm pkg depending on matching req (#24591) Closes #24584 --- cli/args/package_json.rs | 58 +++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'cli/args') diff --git a/cli/args/package_json.rs b/cli/args/package_json.rs index eb1c41c5d..170f8b677 100644 --- a/cli/args/package_json.rs +++ b/cli/args/package_json.rs @@ -7,18 +7,27 @@ use deno_config::package_json::PackageJsonDepValue; use deno_config::workspace::Workspace; use deno_semver::package::PackageReq; +#[derive(Debug)] +pub struct InstallNpmRemotePkg { + pub alias: String, + // todo(24419): use this when setting up the node_modules dir + #[allow(dead_code)] + pub base_dir: PathBuf, + pub req: PackageReq, +} + #[derive(Debug)] pub struct InstallNpmWorkspacePkg { pub alias: String, - pub pkg_dir: PathBuf, + // todo(24419): use this when setting up the node_modules dir + #[allow(dead_code)] + pub base_dir: PathBuf, + pub target_dir: PathBuf, } -// todo(#24419): this is not correct, but it's good enough for now. -// We need deno_npm to be able to understand workspace packages and -// then have a way to properly lay them out on the file system #[derive(Debug, Default)] pub struct PackageJsonInstallDepsProvider { - remote_pkg_reqs: Vec, + remote_pkgs: Vec, workspace_pkgs: Vec, } @@ -29,27 +38,35 @@ impl PackageJsonInstallDepsProvider { pub fn from_workspace(workspace: &Arc) -> Self { let mut workspace_pkgs = Vec::new(); - let mut remote_pkg_reqs = Vec::new(); + let mut remote_pkgs = Vec::new(); let workspace_npm_pkgs = workspace.npm_packages(); for pkg_json in workspace.package_jsons() { let deps = pkg_json.resolve_local_package_json_deps(); - let mut pkg_reqs = Vec::with_capacity(deps.len()); + let mut pkg_pkgs = Vec::with_capacity(deps.len()); for (alias, dep) in deps { let Ok(dep) = dep else { continue; }; match dep { PackageJsonDepValue::Req(pkg_req) => { - if let Some(pkg) = workspace_npm_pkgs - .iter() - .find(|pkg| pkg.matches_req(&pkg_req)) - { + let workspace_pkg = workspace_npm_pkgs.iter().find(|pkg| { + pkg.matches_req(&pkg_req) + // do not resolve to the current package + && pkg.pkg_json.path != pkg_json.path + }); + + if let Some(pkg) = workspace_pkg { workspace_pkgs.push(InstallNpmWorkspacePkg { alias, - pkg_dir: pkg.pkg_json.dir_path().to_path_buf(), + base_dir: pkg_json.dir_path().to_path_buf(), + target_dir: pkg.pkg_json.dir_path().to_path_buf(), }); } else { - pkg_reqs.push(pkg_req) + pkg_pkgs.push(InstallNpmRemotePkg { + alias, + base_dir: pkg_json.dir_path().to_path_buf(), + req: pkg_req, + }); } } PackageJsonDepValue::Workspace(version_req) => { @@ -58,27 +75,28 @@ impl PackageJsonInstallDepsProvider { }) { workspace_pkgs.push(InstallNpmWorkspacePkg { alias, - pkg_dir: pkg.pkg_json.dir_path().to_path_buf(), + base_dir: pkg_json.dir_path().to_path_buf(), + target_dir: pkg.pkg_json.dir_path().to_path_buf(), }); } } } } // sort within each package - pkg_reqs.sort(); + pkg_pkgs.sort_by(|a, b| a.alias.cmp(&b.alias)); - remote_pkg_reqs.extend(pkg_reqs); + remote_pkgs.extend(pkg_pkgs); } - remote_pkg_reqs.shrink_to_fit(); + remote_pkgs.shrink_to_fit(); workspace_pkgs.shrink_to_fit(); Self { - remote_pkg_reqs, + remote_pkgs, workspace_pkgs, } } - pub fn remote_pkg_reqs(&self) -> &Vec { - &self.remote_pkg_reqs + pub fn remote_pkgs(&self) -> &Vec { + &self.remote_pkgs } pub fn workspace_pkgs(&self) -> &Vec { -- cgit v1.2.3