feat(npm): support bare specifiers from package.json in more subcommands and language server (#17891)

This commit is contained in:
David Sherret 2023-02-23 10:58:10 -05:00 committed by GitHub
parent 214bdbbc2b
commit 344317ec50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 472 additions and 95 deletions

View file

@ -20,6 +20,9 @@ use crate::npm::NpmResolutionPackage;
/// Part of the resolution that interacts with the file system.
#[async_trait]
pub trait NpmPackageFsResolver: Send + Sync {
/// Specifier for the root directory.
fn root_dir_url(&self) -> &Url;
fn resolve_package_folder_from_deno_module(
&self,
id: &NpmPackageId,

View file

@ -68,6 +68,10 @@ impl GlobalNpmPackageResolver {
#[async_trait]
impl NpmPackageFsResolver for GlobalNpmPackageResolver {
fn root_dir_url(&self) -> &Url {
self.cache.root_dir_url()
}
fn resolve_package_folder_from_deno_module(
&self,
id: &NpmPackageId,

View file

@ -44,7 +44,7 @@ pub struct LocalNpmPackageResolver {
resolution: NpmResolution,
registry_url: Url,
root_node_modules_path: PathBuf,
root_node_modules_specifier: ModuleSpecifier,
root_node_modules_url: Url,
}
impl LocalNpmPackageResolver {
@ -58,10 +58,8 @@ impl LocalNpmPackageResolver {
cache,
resolution,
registry_url,
root_node_modules_specifier: ModuleSpecifier::from_directory_path(
&node_modules_folder,
)
.unwrap(),
root_node_modules_url: Url::from_directory_path(&node_modules_folder)
.unwrap(),
root_node_modules_path: node_modules_folder,
}
}
@ -92,8 +90,7 @@ impl LocalNpmPackageResolver {
&self,
specifier: &ModuleSpecifier,
) -> Option<PathBuf> {
let relative_url =
self.root_node_modules_specifier.make_relative(specifier)?;
let relative_url = self.root_node_modules_url.make_relative(specifier)?;
if relative_url.starts_with("../") {
return None;
}
@ -126,6 +123,10 @@ impl LocalNpmPackageResolver {
#[async_trait]
impl NpmPackageFsResolver for LocalNpmPackageResolver {
fn root_dir_url(&self) -> &Url {
&self.root_node_modules_url
}
fn resolve_package_folder_from_deno_module(
&self,
node_id: &NpmPackageId,

View file

@ -19,7 +19,7 @@ use deno_runtime::deno_node::RequireNpmResolver;
use global::GlobalNpmPackageResolver;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashSet;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
@ -214,9 +214,9 @@ impl NpmPackageResolver {
/// Gets if the provided specifier is in an npm package.
pub fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
self
.resolve_package_folder_from_specifier(specifier)
.is_ok()
let root_dir_url = self.fs_resolver.root_dir_url();
debug_assert!(root_dir_url.as_str().ends_with('/'));
specifier.as_ref().starts_with(root_dir_url.as_str())
}
/// If the resolver has resolved any npm packages.
@ -224,6 +224,19 @@ impl NpmPackageResolver {
self.resolution.has_packages()
}
/// Adds the package reqs from a package.json if they exist.
pub async fn add_package_json_deps(
&self,
maybe_package_json_deps: Option<&HashMap<String, NpmPackageReq>>,
) -> Result<(), AnyError> {
if let Some(deps) = maybe_package_json_deps {
let mut package_reqs = deps.values().cloned().collect::<Vec<_>>();
package_reqs.sort(); // deterministic resolution
self.add_package_reqs(package_reqs).await?;
}
Ok(())
}
/// Adds package requirements to the resolver and ensures everything is setup.
pub async fn add_package_reqs(
&self,
@ -250,7 +263,7 @@ impl NpmPackageResolver {
/// This will retrieve and resolve package information, but not cache any package files.
pub async fn set_package_reqs(
&self,
packages: HashSet<NpmPackageReq>,
packages: Vec<NpmPackageReq>,
) -> Result<(), AnyError> {
self.resolution.set_package_reqs(packages).await
}