1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

refactor: upgrade to deno_npm 0.6 (#19244)

This commit is contained in:
David Sherret 2023-05-24 16:23:10 -04:00 committed by GitHub
parent 114ec3c1f7
commit 91ca9904b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 46 additions and 64 deletions

4
Cargo.lock generated
View file

@ -1217,9 +1217,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_npm" name = "deno_npm"
version = "0.5.1" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9885542e422eb2500f279c2cf9d048a2b306db60df83ec4c34cefe71ae6cc2a" checksum = "54607b69689ab1e778e5e00545456e6f0c2310205e1bdae01af601c2dace0121"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

View file

@ -53,7 +53,7 @@ deno_bench_util = { version = "0.99.0", path = "./bench_util" }
test_util = { path = "./test_util" } test_util = { path = "./test_util" }
deno_lockfile = "0.14.1" deno_lockfile = "0.14.1"
deno_media_type = { version = "0.1.0", features = ["module_specifier"] } deno_media_type = { version = "0.1.0", features = ["module_specifier"] }
deno_npm = "0.5.1" deno_npm = "0.6.0"
deno_semver = "0.2.1" deno_semver = "0.2.1"
# exts # exts

View file

@ -82,7 +82,7 @@ pub async fn snapshot_from_lockfile(
// now fill the packages except for the dist information // now fill the packages except for the dist information
let mut packages = Vec::with_capacity(lockfile.content.npm.packages.len()); let mut packages = Vec::with_capacity(lockfile.content.npm.packages.len());
for (key, package) in &lockfile.content.npm.packages { for (key, package) in &lockfile.content.npm.packages {
let pkg_id = NpmPackageId::from_serialized(key)?; let id = NpmPackageId::from_serialized(key)?;
// collect the dependencies // collect the dependencies
let mut dependencies = HashMap::with_capacity(package.dependencies.len()); let mut dependencies = HashMap::with_capacity(package.dependencies.len());
@ -92,7 +92,7 @@ pub async fn snapshot_from_lockfile(
} }
packages.push(SerializedNpmResolutionSnapshotPackage { packages.push(SerializedNpmResolutionSnapshotPackage {
pkg_id, id,
dependencies, dependencies,
// temporarily empty // temporarily empty
os: Default::default(), os: Default::default(),
@ -105,10 +105,7 @@ pub async fn snapshot_from_lockfile(
}; };
// now that the lockfile is dropped, fetch the package version information // now that the lockfile is dropped, fetch the package version information
let pkg_nvs = packages let pkg_nvs = packages.iter().map(|p| p.id.nv.clone()).collect::<Vec<_>>();
.iter()
.map(|p| p.pkg_id.nv.clone())
.collect::<Vec<_>>();
let get_version_infos = || { let get_version_infos = || {
FuturesOrdered::from_iter(pkg_nvs.iter().map(|nv| async move { FuturesOrdered::from_iter(pkg_nvs.iter().map(|nv| async move {
let package_info = api.package_info(&nv.name).await?; let package_info = api.package_info(&nv.name).await?;

View file

@ -198,7 +198,7 @@ impl NpmResolution {
.snapshot .snapshot
.read() .read()
.resolve_pkg_from_pkg_req(req) .resolve_pkg_from_pkg_req(req)
.map(|pkg| pkg.pkg_id.clone()) .map(|pkg| pkg.id.clone())
} }
pub fn resolve_pkg_id_from_deno_module( pub fn resolve_pkg_id_from_deno_module(
@ -209,7 +209,7 @@ impl NpmResolution {
.snapshot .snapshot
.read() .read()
.resolve_package_from_deno_module(id) .resolve_package_from_deno_module(id)
.map(|pkg| pkg.pkg_id.clone()) .map(|pkg| pkg.id.clone())
} }
/// Resolves a package requirement for deno graph. This should only be /// Resolves a package requirement for deno graph. This should only be
@ -326,7 +326,7 @@ fn populate_lockfile_from_snapshot(
snapshot snapshot
.resolve_package_from_deno_module(nv) .resolve_package_from_deno_module(nv)
.unwrap() .unwrap()
.pkg_id .id
.as_serialized(), .as_serialized(),
); );
} }
@ -350,8 +350,8 @@ fn npm_package_to_lockfile_info(
.collect(); .collect();
NpmPackageLockfileInfo { NpmPackageLockfileInfo {
display_id: pkg.pkg_id.nv.to_string(), display_id: pkg.id.nv.to_string(),
serialized_id: pkg.pkg_id.as_serialized(), serialized_id: pkg.id.as_serialized(),
integrity: pkg.dist.integrity().to_string(), integrity: pkg.dist.integrity().to_string(),
dependencies, dependencies,
} }

View file

@ -64,7 +64,7 @@ pub async fn cache_packages(
if sync_download { if sync_download {
// we're running the tests not with --quiet // we're running the tests not with --quiet
// and we want the output to be deterministic // and we want the output to be deterministic
packages.sort_by(|a, b| a.pkg_id.cmp(&b.pkg_id)); packages.sort_by(|a, b| a.id.cmp(&b.id));
} }
let mut handles = Vec::with_capacity(packages.len()); let mut handles = Vec::with_capacity(packages.len());
@ -73,7 +73,7 @@ pub async fn cache_packages(
let registry_url = registry_url.clone(); let registry_url = registry_url.clone();
let handle = spawn(async move { let handle = spawn(async move {
cache cache
.ensure_package(&package.pkg_id.nv, &package.dist, &registry_url) .ensure_package(&package.id.nv, &package.dist, &registry_url)
.await .await
}); });
if sync_download { if sync_download {

View file

@ -110,7 +110,7 @@ impl NpmPackageFsResolver for GlobalNpmPackageResolver {
.resolution .resolution
.resolve_package_from_package(name, &referrer_pkg_id)? .resolve_package_from_package(name, &referrer_pkg_id)?
}; };
self.package_folder(&pkg.pkg_id) self.package_folder(&pkg.id)
} }
fn resolve_package_folder_from_specifier( fn resolve_package_folder_from_specifier(

View file

@ -274,9 +274,7 @@ async fn sync_resolution_with_fs(
if sync_download { if sync_download {
// we're running the tests not with --quiet // we're running the tests not with --quiet
// and we want the output to be deterministic // and we want the output to be deterministic
package_partitions package_partitions.packages.sort_by(|a, b| a.id.cmp(&b.id));
.packages
.sort_by(|a, b| a.pkg_id.cmp(&b.pkg_id));
} }
let mut handles: Vec<JoinHandle<Result<(), AnyError>>> = let mut handles: Vec<JoinHandle<Result<(), AnyError>>> =
Vec::with_capacity(package_partitions.packages.len()); Vec::with_capacity(package_partitions.packages.len());
@ -284,13 +282,13 @@ async fn sync_resolution_with_fs(
HashMap::with_capacity(package_partitions.packages.len()); HashMap::with_capacity(package_partitions.packages.len());
for package in &package_partitions.packages { for package in &package_partitions.packages {
if let Some(current_pkg) = if let Some(current_pkg) =
newest_packages_by_name.get_mut(&package.pkg_id.nv.name) newest_packages_by_name.get_mut(&package.id.nv.name)
{ {
if current_pkg.pkg_id.nv.cmp(&package.pkg_id.nv) == Ordering::Less { if current_pkg.id.nv.cmp(&package.id.nv) == Ordering::Less {
*current_pkg = package; *current_pkg = package;
} }
} else { } else {
newest_packages_by_name.insert(&package.pkg_id.nv.name, package); newest_packages_by_name.insert(&package.id.nv.name, package);
}; };
let folder_name = let folder_name =
@ -299,7 +297,7 @@ async fn sync_resolution_with_fs(
let initialized_file = folder_path.join(".initialized"); let initialized_file = folder_path.join(".initialized");
if !cache if !cache
.cache_setting() .cache_setting()
.should_use_for_npm_package(&package.pkg_id.nv.name) .should_use_for_npm_package(&package.id.nv.name)
|| !initialized_file.exists() || !initialized_file.exists()
{ {
let pb = progress_bar.clone(); let pb = progress_bar.clone();
@ -308,21 +306,19 @@ async fn sync_resolution_with_fs(
let package = package.clone(); let package = package.clone();
let handle = spawn(async move { let handle = spawn(async move {
cache cache
.ensure_package(&package.pkg_id.nv, &package.dist, &registry_url) .ensure_package(&package.id.nv, &package.dist, &registry_url)
.await?; .await?;
let pb_guard = pb.update_with_prompt( let pb_guard = pb.update_with_prompt(
ProgressMessagePrompt::Initialize, ProgressMessagePrompt::Initialize,
&package.pkg_id.nv.to_string(), &package.id.nv.to_string(),
); );
let sub_node_modules = folder_path.join("node_modules"); let sub_node_modules = folder_path.join("node_modules");
let package_path = let package_path =
join_package_name(&sub_node_modules, &package.pkg_id.nv.name); join_package_name(&sub_node_modules, &package.id.nv.name);
fs::create_dir_all(&package_path) fs::create_dir_all(&package_path)
.with_context(|| format!("Creating '{}'", folder_path.display()))?; .with_context(|| format!("Creating '{}'", folder_path.display()))?;
let cache_folder = cache.package_folder_for_name_and_version( let cache_folder = cache
&package.pkg_id.nv, .package_folder_for_name_and_version(&package.id.nv, &registry_url);
&registry_url,
);
// for now copy, but in the future consider hard linking // for now copy, but in the future consider hard linking
copy_dir_recursive(&cache_folder, &package_path)?; copy_dir_recursive(&cache_folder, &package_path)?;
// write out a file that indicates this folder has been initialized // write out a file that indicates this folder has been initialized
@ -353,7 +349,7 @@ async fn sync_resolution_with_fs(
if !initialized_file.exists() { if !initialized_file.exists() {
let sub_node_modules = destination_path.join("node_modules"); let sub_node_modules = destination_path.join("node_modules");
let package_path = let package_path =
join_package_name(&sub_node_modules, &package.pkg_id.nv.name); join_package_name(&sub_node_modules, &package.id.nv.name);
fs::create_dir_all(&package_path).with_context(|| { fs::create_dir_all(&package_path).with_context(|| {
format!("Creating '{}'", destination_path.display()) format!("Creating '{}'", destination_path.display())
})?; })?;
@ -363,7 +359,7 @@ async fn sync_resolution_with_fs(
&package_cache_folder_id.with_no_count(), &package_cache_folder_id.with_no_count(),
)) ))
.join("node_modules"), .join("node_modules"),
&package.pkg_id.nv.name, &package.id.nv.name,
); );
hard_link_dir_recursive(&source_path, &package_path)?; hard_link_dir_recursive(&source_path, &package_path)?;
// write out a file that indicates this folder has been initialized // write out a file that indicates this folder has been initialized
@ -375,11 +371,7 @@ async fn sync_resolution_with_fs(
// //
// Symlink node_modules/.deno/<package_id>/node_modules/<dep_name> to // Symlink node_modules/.deno/<package_id>/node_modules/<dep_name> to
// node_modules/.deno/<dep_id>/node_modules/<dep_package_name> // node_modules/.deno/<dep_id>/node_modules/<dep_package_name>
for package in package_partitions for package in package_partitions.iter_all() {
.packages
.iter()
.chain(package_partitions.copy_packages.iter())
{
let sub_node_modules = deno_local_registry_dir let sub_node_modules = deno_local_registry_dir
.join(get_package_folder_id_folder_name( .join(get_package_folder_id_folder_name(
&package.get_package_cache_folder_id(), &package.get_package_cache_folder_id(),
@ -435,7 +427,7 @@ async fn sync_resolution_with_fs(
// 5. Create a node_modules/.deno/node_modules/<package-name> directory with // 5. Create a node_modules/.deno/node_modules/<package-name> directory with
// the remaining packages // the remaining packages
for package in newest_packages_by_name.values() { for package in newest_packages_by_name.values() {
if !found_names.insert(&package.pkg_id.nv.name) { if !found_names.insert(&package.id.nv.name) {
continue; // skip, already handled continue; // skip, already handled
} }
@ -445,12 +437,12 @@ async fn sync_resolution_with_fs(
&package.get_package_cache_folder_id(), &package.get_package_cache_folder_id(),
)) ))
.join("node_modules"), .join("node_modules"),
&package.pkg_id.nv.name, &package.id.nv.name,
); );
symlink_package_dir( symlink_package_dir(
&local_registry_package_path, &local_registry_package_path,
&join_package_name(&deno_node_modules_dir, &package.pkg_id.nv.name), &join_package_name(&deno_node_modules_dir, &package.id.nv.name),
)?; )?;
} }

View file

@ -551,7 +551,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
{ {
let folder = self let folder = self
.npm_resolver .npm_resolver
.resolve_pkg_folder_from_pkg_id(&package.pkg_id)?; .resolve_pkg_folder_from_pkg_id(&package.id)?;
builder.add_dir_recursive(&folder)?; builder.add_dir_recursive(&folder)?;
} }
// overwrite the root directory's name to obscure the user's registry url // overwrite the root directory's name to obscure the user's registry url

View file

@ -170,10 +170,8 @@ fn add_npm_packages_to_json(
}); });
if let Some(pkg) = maybe_package { if let Some(pkg) = maybe_package {
if let Some(module) = module.as_object_mut() { if let Some(module) = module.as_object_mut() {
module.insert( module
"npmPackage".to_string(), .insert("npmPackage".to_string(), pkg.id.as_serialized().into());
pkg.pkg_id.as_serialized().into(),
);
} }
} }
} else { } else {
@ -206,7 +204,7 @@ fn add_npm_packages_to_json(
{ {
dep.insert( dep.insert(
"npmPackage".to_string(), "npmPackage".to_string(),
pkg.pkg_id.as_serialized().into(), pkg.id.as_serialized().into(),
); );
} }
} }
@ -219,15 +217,12 @@ fn add_npm_packages_to_json(
let mut sorted_packages = let mut sorted_packages =
snapshot.all_packages_for_every_system().collect::<Vec<_>>(); snapshot.all_packages_for_every_system().collect::<Vec<_>>();
sorted_packages.sort_by(|a, b| a.pkg_id.cmp(&b.pkg_id)); sorted_packages.sort_by(|a, b| a.id.cmp(&b.id));
let mut json_packages = serde_json::Map::with_capacity(sorted_packages.len()); let mut json_packages = serde_json::Map::with_capacity(sorted_packages.len());
for pkg in sorted_packages { for pkg in sorted_packages {
let mut kv = serde_json::Map::new(); let mut kv = serde_json::Map::new();
kv.insert("name".to_string(), pkg.pkg_id.nv.name.to_string().into()); kv.insert("name".to_string(), pkg.id.nv.name.to_string().into());
kv.insert( kv.insert("version".to_string(), pkg.id.nv.version.to_string().into());
"version".to_string(),
pkg.pkg_id.nv.version.to_string().into(),
);
let mut deps = pkg.dependencies.values().collect::<Vec<_>>(); let mut deps = pkg.dependencies.values().collect::<Vec<_>>();
deps.sort(); deps.sort();
let deps = deps let deps = deps
@ -236,7 +231,7 @@ fn add_npm_packages_to_json(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
kv.insert("dependencies".to_string(), deps.into()); kv.insert("dependencies".to_string(), deps.into());
json_packages.insert(pkg.pkg_id.as_serialized(), kv.into()); json_packages.insert(pkg.id.as_serialized(), kv.into());
} }
json.insert("npmPackages".to_string(), json_packages.into()); json.insert("npmPackages".to_string(), json_packages.into());
@ -335,8 +330,8 @@ impl NpmInfo {
if let Module::Npm(module) = module { if let Module::Npm(module) = module {
let nv = &module.nv_reference.nv; let nv = &module.nv_reference.nv;
if let Ok(package) = npm_snapshot.resolve_package_from_deno_module(nv) { if let Ok(package) = npm_snapshot.resolve_package_from_deno_module(nv) {
info.resolved_ids.insert(nv.clone(), package.pkg_id.clone()); info.resolved_ids.insert(nv.clone(), package.id.clone());
if !info.packages.contains_key(&package.pkg_id) { if !info.packages.contains_key(&package.id) {
info.fill_package_info(package, npm_resolver, npm_snapshot); info.fill_package_info(package, npm_resolver, npm_snapshot);
} }
} }
@ -352,11 +347,9 @@ impl NpmInfo {
npm_resolver: &'a CliNpmResolver, npm_resolver: &'a CliNpmResolver,
npm_snapshot: &'a NpmResolutionSnapshot, npm_snapshot: &'a NpmResolutionSnapshot,
) { ) {
self self.packages.insert(package.id.clone(), package.clone());
.packages if let Ok(size) = npm_resolver.package_size(&package.id) {
.insert(package.pkg_id.clone(), package.clone()); self.package_sizes.insert(package.id.clone(), size);
if let Ok(size) = npm_resolver.package_size(&package.pkg_id) {
self.package_sizes.insert(package.pkg_id.clone(), size);
} }
for id in package.dependencies.values() { for id in package.dependencies.values() {
if !self.packages.contains_key(id) { if !self.packages.contains_key(id) {
@ -536,7 +529,7 @@ impl<'a> GraphDisplayContext<'a> {
None => Specifier(module.specifier().clone()), None => Specifier(module.specifier().clone()),
}; };
let was_seen = !self.seen.insert(match &package_or_specifier { let was_seen = !self.seen.insert(match &package_or_specifier {
Package(package) => package.pkg_id.as_serialized(), Package(package) => package.id.as_serialized(),
Specifier(specifier) => specifier.to_string(), Specifier(specifier) => specifier.to_string(),
}); });
let header_text = if was_seen { let header_text = if was_seen {
@ -554,7 +547,7 @@ impl<'a> GraphDisplayContext<'a> {
}; };
let maybe_size = match &package_or_specifier { let maybe_size = match &package_or_specifier {
Package(package) => { Package(package) => {
self.npm_info.package_sizes.get(&package.pkg_id).copied() self.npm_info.package_sizes.get(&package.id).copied()
} }
Specifier(_) => match module { Specifier(_) => match module {
Module::Esm(module) => Some(module.size() as u64), Module::Esm(module) => Some(module.size() as u64),
@ -608,7 +601,7 @@ impl<'a> GraphDisplayContext<'a> {
)); ));
if let Some(package) = self.npm_info.packages.get(dep_id) { if let Some(package) = self.npm_info.packages.get(dep_id) {
if !package.dependencies.is_empty() { if !package.dependencies.is_empty() {
let was_seen = !self.seen.insert(package.pkg_id.as_serialized()); let was_seen = !self.seen.insert(package.id.as_serialized());
if was_seen { if was_seen {
child.text = format!("{} {}", child.text, colors::gray("*")); child.text = format!("{} {}", child.text, colors::gray("*"));
} else { } else {