-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(install/clean): infinite loops on circular dependencies (#211)
* fix(package-manager): avoid infinite loops when clean install circular dependencies * chore: use dashset instread of memo-map for resolved_packages cache * chore: fmt * docs: make it appears nicer --------- Co-authored-by: Khải <[email protected]>
- Loading branch information
Showing
9 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ use pipe_trait::Pipe; | |
use std::{ | ||
fs::{self, OpenOptions}, | ||
io::Write, | ||
path::Path, | ||
}; | ||
|
||
#[test] | ||
|
@@ -164,6 +165,29 @@ fn frozen_lockfile_should_be_able_to_handle_big_lockfile() { | |
|
||
eprintln!("Executing command..."); | ||
pacquet.with_args(["install", "--frozen-lockfile"]).assert().success(); | ||
} | ||
|
||
#[test] | ||
fn should_install_circular_dependencies() { | ||
let CommandTempCwd { pacquet, root, workspace, npmrc_info, .. } = | ||
CommandTempCwd::init().add_mocked_registry(); | ||
let AddMockedRegistry { mock_instance, .. } = npmrc_info; | ||
|
||
eprintln!("Creating package.json..."); | ||
let manifest_path = workspace.join("package.json"); | ||
let package_json_content = serde_json::json!({ | ||
"dependencies": { | ||
"@pnpm.e2e/circular-deps-1-of-2": "1.0.2", | ||
}, | ||
}); | ||
fs::write(manifest_path, package_json_content.to_string()).expect("write to package.json"); | ||
|
||
eprintln!("Executing command..."); | ||
pacquet.with_arg("install").assert().success(); | ||
|
||
assert!(workspace.join("./node_modules/@pnpm.e2e/circular-deps-1-of-2").exists()); | ||
assert!(workspace.join("./node_modules/.pnpm/@[email protected]").exists()); | ||
assert!(workspace.join("./node_modules/.pnpm/@[email protected]").exists()); | ||
|
||
drop((root, mock_instance)); // cleanup | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use crate::InstallPackageFromRegistry; | ||
use async_recursion::async_recursion; | ||
use dashmap::DashSet; | ||
use futures_util::future; | ||
use node_semver::Version; | ||
use pacquet_network::ThrottledClient; | ||
|
@@ -9,6 +10,12 @@ use pacquet_registry::PackageVersion; | |
use pacquet_tarball::MemCache; | ||
use pipe_trait::Pipe; | ||
|
||
/// In-memory cache for packages that have started resolving dependencies. | ||
/// | ||
/// The contents of set is the package's virtual_store_name. | ||
/// e.g. `@pnpm.e2e/[email protected]` → `@[email protected]` | ||
pub type ResolvedPackages = DashSet<String>; | ||
|
||
/// This subroutine install packages from a `package.json` without reading or writing a lockfile. | ||
/// | ||
/// **Brief overview for each package:** | ||
|
@@ -21,6 +28,7 @@ use pipe_trait::Pipe; | |
#[must_use] | ||
pub struct InstallWithoutLockfile<'a, DependencyGroupList> { | ||
pub tarball_mem_cache: &'a MemCache, | ||
pub resolved_packages: &'a ResolvedPackages, | ||
pub http_client: &'a ThrottledClient, | ||
pub config: &'static Npmrc, | ||
pub manifest: &'a PackageManifest, | ||
|
@@ -39,6 +47,7 @@ impl<'a, DependencyGroupList> InstallWithoutLockfile<'a, DependencyGroupList> { | |
config, | ||
manifest, | ||
dependency_groups, | ||
resolved_packages, | ||
} = self; | ||
|
||
let _: Vec<()> = manifest | ||
|
@@ -62,6 +71,7 @@ impl<'a, DependencyGroupList> InstallWithoutLockfile<'a, DependencyGroupList> { | |
config, | ||
manifest, | ||
dependency_groups: (), | ||
resolved_packages, | ||
} | ||
.install_dependencies_from_registry(&dependency) | ||
.await; | ||
|
@@ -75,7 +85,19 @@ impl<'a> InstallWithoutLockfile<'a, ()> { | |
/// Install dependencies of a dependency. | ||
#[async_recursion] | ||
async fn install_dependencies_from_registry(&self, package: &PackageVersion) { | ||
let InstallWithoutLockfile { tarball_mem_cache, http_client, config, .. } = self; | ||
let InstallWithoutLockfile { | ||
tarball_mem_cache, | ||
http_client, | ||
config, | ||
resolved_packages, | ||
.. | ||
} = self; | ||
|
||
// This package has already resolved, there is no need to reinstall again. | ||
if !resolved_packages.insert(package.to_virtual_store_name()) { | ||
tracing::info!(target: "pacquet::install", package = ?package.to_virtual_store_name(), "Skip subset"); | ||
return; | ||
} | ||
|
||
let node_modules_path = self | ||
.config | ||
|