feat(compile): support "bring your own node_modules" in deno compile (#21377)

Not tested thoroughly. This is a good start.

Closes #21350
This commit is contained in:
David Sherret 2023-11-29 09:32:23 -05:00 committed by GitHub
parent 7e56a0466f
commit 9ac405d587
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 245 additions and 121 deletions

View file

@ -19,7 +19,7 @@ use deno_semver::package::PackageReq;
use crate::args::package_json::get_local_package_json_version_reqs;
use crate::args::NpmProcessState;
use crate::args::NpmProcessStateKind;
use crate::util::fs::canonicalize_path_maybe_not_exists;
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
use crate::util::path::specifier_to_file_path;
use super::common::types_package_name;
@ -188,8 +188,8 @@ impl CliNpmResolver for ByonmCliNpmResolver {
InnerCliNpmResolverRef::Byonm(self)
}
fn root_node_modules_path(&self) -> Option<std::path::PathBuf> {
Some(self.root_node_modules_dir.clone())
fn root_node_modules_path(&self) -> Option<&PathBuf> {
Some(&self.root_node_modules_dir)
}
fn resolve_pkg_folder_from_deno_module_req(
@ -215,7 +215,10 @@ impl CliNpmResolver for ByonmCliNpmResolver {
.unwrap()
.join("node_modules")
.join(key);
return Ok(canonicalize_path_maybe_not_exists(&package_path)?);
return Ok(canonicalize_path_maybe_not_exists_with_fs(
&package_path,
fs,
)?);
}
}
}

View file

@ -288,12 +288,8 @@ impl ManagedCliNpmResolver {
pkg_id: &NpmPackageId,
) -> Result<PathBuf, AnyError> {
let path = self.fs_resolver.package_folder(pkg_id)?;
let path = canonicalize_path_maybe_not_exists_with_fs(&path, |path| {
self
.fs
.realpath_sync(path)
.map_err(|err| err.into_io_error())
})?;
let path =
canonicalize_path_maybe_not_exists_with_fs(&path, self.fs.as_ref())?;
log::debug!(
"Resolved package folder of {} to {}",
pkg_id.as_serialized(),
@ -560,7 +556,7 @@ impl CliNpmResolver for ManagedCliNpmResolver {
&self.progress_bar,
self.api.base_url().clone(),
npm_resolution,
self.root_node_modules_path(),
self.root_node_modules_path().map(ToOwned::to_owned),
self.npm_system_info.clone(),
),
self.global_npm_cache.clone(),
@ -575,7 +571,7 @@ impl CliNpmResolver for ManagedCliNpmResolver {
InnerCliNpmResolverRef::Managed(self)
}
fn root_node_modules_path(&self) -> Option<PathBuf> {
fn root_node_modules_path(&self) -> Option<&PathBuf> {
self.fs_resolver.node_modules_path()
}

View file

@ -29,7 +29,7 @@ pub trait NpmPackageFsResolver: Send + Sync {
fn root_dir_url(&self) -> &Url;
/// The local node_modules folder if it is applicable to the implementation.
fn node_modules_path(&self) -> Option<PathBuf>;
fn node_modules_path(&self) -> Option<&PathBuf>;
fn package_folder(
&self,

View file

@ -75,7 +75,7 @@ impl NpmPackageFsResolver for GlobalNpmPackageResolver {
self.cache.root_dir_url()
}
fn node_modules_path(&self) -> Option<PathBuf> {
fn node_modules_path(&self) -> Option<&PathBuf> {
None
}

View file

@ -122,14 +122,9 @@ impl LocalNpmPackageResolver {
};
// Canonicalize the path so it's not pointing to the symlinked directory
// in `node_modules` directory of the referrer.
canonicalize_path_maybe_not_exists_with_fs(&path, |path| {
self
.fs
.realpath_sync(path)
.map_err(|err| err.into_io_error())
})
.map(Some)
.map_err(|err| err.into())
canonicalize_path_maybe_not_exists_with_fs(&path, self.fs.as_ref())
.map(Some)
.map_err(|err| err.into())
}
}
@ -139,8 +134,8 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
&self.root_node_modules_url
}
fn node_modules_path(&self) -> Option<PathBuf> {
Some(self.root_node_modules_path.clone())
fn node_modules_path(&self) -> Option<&PathBuf> {
Some(&self.root_node_modules_path)
}
fn package_folder(&self, id: &NpmPackageId) -> Result<PathBuf, AnyError> {

View file

@ -75,7 +75,7 @@ pub trait CliNpmResolver: NpmResolver {
}
}
fn root_node_modules_path(&self) -> Option<PathBuf>;
fn root_node_modules_path(&self) -> Option<&PathBuf>;
fn resolve_pkg_folder_from_deno_module_req(
&self,