From 28aa489de9cd4f995ec2fc02e2c9d224e89f4c01 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 10 May 2023 20:06:59 -0400 Subject: feat(compile): unstable npm and node specifier support (#19005) This is the initial support for npm and node specifiers in `deno compile`. The npm packages are included in the binary and read from it via a virtual file system. This also supports the `--node-modules-dir` flag, dependencies specified in a package.json, and npm binary commands (ex. `deno compile --unstable npm:cowsay`) Closes #16632 --- cli/npm/resolvers/mod.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'cli/npm/resolvers/mod.rs') diff --git a/cli/npm/resolvers/mod.rs b/cli/npm/resolvers/mod.rs index 86d3840f3..f54e509f0 100644 --- a/cli/npm/resolvers/mod.rs +++ b/cli/npm/resolvers/mod.rs @@ -18,7 +18,7 @@ use deno_npm::resolution::NpmResolutionSnapshot; use deno_npm::resolution::PackageReqNotFoundError; use deno_npm::resolution::SerializedNpmResolutionSnapshot; use deno_npm::NpmPackageId; -use deno_runtime::deno_fs; +use deno_runtime::deno_fs::FileSystem; use deno_runtime::deno_node::NodePermissions; use deno_runtime::deno_node::NodeResolutionMode; use deno_runtime::deno_node::NpmResolver; @@ -32,7 +32,7 @@ use serde::Deserialize; use serde::Serialize; use crate::args::Lockfile; -use crate::util::fs::canonicalize_path_maybe_not_exists; +use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs; use crate::util::progress_bar::ProgressBar; use self::common::NpmPackageFsResolver; @@ -49,6 +49,7 @@ pub struct NpmProcessState { /// Brings together the npm resolution with the file system. pub struct CliNpmResolver { + fs: Arc, fs_resolver: Arc, resolution: Arc, maybe_lockfile: Option>>, @@ -57,6 +58,7 @@ pub struct CliNpmResolver { impl std::fmt::Debug for CliNpmResolver { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("NpmPackageResolver") + .field("fs", &"") .field("fs_resolver", &"") .field("resolution", &"") .field("maybe_lockfile", &"") @@ -66,11 +68,13 @@ impl std::fmt::Debug for CliNpmResolver { impl CliNpmResolver { pub fn new( + fs: Arc, resolution: Arc, fs_resolver: Arc, maybe_lockfile: Option>>, ) -> Self { Self { + fs, fs_resolver, resolution, maybe_lockfile, @@ -81,6 +85,10 @@ impl CliNpmResolver { self.fs_resolver.root_dir_url() } + pub fn node_modules_path(&self) -> Option { + self.fs_resolver.node_modules_path() + } + pub fn resolve_pkg_id_from_pkg_req( &self, req: &NpmPackageReq, @@ -88,12 +96,17 @@ impl CliNpmResolver { self.resolution.resolve_pkg_id_from_pkg_req(req) } - fn resolve_pkg_folder_from_deno_module_at_pkg_id( + pub fn resolve_pkg_folder_from_pkg_id( &self, pkg_id: &NpmPackageId, ) -> Result { let path = self.fs_resolver.package_folder(pkg_id)?; - let path = canonicalize_path_maybe_not_exists(&path)?; + let path = canonicalize_path_maybe_not_exists_with_fs(&path, |path| { + self + .fs + .realpath_sync(path) + .map_err(|err| err.into_io_error()) + })?; log::debug!( "Resolved package folder of {} to {}", pkg_id.as_serialized(), @@ -237,7 +250,7 @@ impl NpmResolver for CliNpmResolver { pkg_nv: &NpmPackageNv, ) -> Result { let pkg_id = self.resolution.resolve_pkg_id_from_deno_module(pkg_nv)?; - self.resolve_pkg_folder_from_deno_module_at_pkg_id(&pkg_id) + self.resolve_pkg_folder_from_pkg_id(&pkg_id) } fn resolve_pkg_id_from_pkg_req( @@ -270,7 +283,7 @@ impl NpmResolver for CliNpmResolver { } pub fn create_npm_fs_resolver( - fs: Arc, + fs: Arc, cache: Arc, progress_bar: &ProgressBar, registry_url: Url, @@ -287,6 +300,7 @@ pub fn create_npm_fs_resolver( resolution, )), None => Arc::new(GlobalNpmPackageResolver::new( + fs, cache, registry_url, resolution, -- cgit v1.2.3