2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2023-03-03 17:27:05 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2023-01-24 08:23:19 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_core::futures::future;
|
|
|
|
use deno_core::futures::future::LocalBoxFuture;
|
|
|
|
use deno_core::futures::FutureExt;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-03-04 20:07:11 -05:00
|
|
|
use deno_core::TaskQueue;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::npm::NpmPackageNv;
|
|
|
|
use deno_graph::npm::NpmPackageReq;
|
|
|
|
use deno_graph::source::NpmResolver;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_graph::source::Resolver;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::source::UnknownBuiltInNodeModuleError;
|
2022-11-02 10:47:02 -04:00
|
|
|
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_runtime::deno_node::is_builtin_node_module;
|
2021-10-10 17:26:22 -04:00
|
|
|
use import_map::ImportMap;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::sync::Arc;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2023-03-03 17:27:05 -05:00
|
|
|
use crate::args::package_json::PackageJsonDeps;
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
2023-02-22 14:15:25 -05:00
|
|
|
use crate::npm::NpmRegistryApi;
|
|
|
|
use crate::npm::NpmResolution;
|
2023-02-24 19:35:43 -05:00
|
|
|
use crate::npm::PackageJsonDepsInstaller;
|
2022-08-24 13:36:05 -04:00
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
/// A resolver that takes care of resolution, taking into account loaded
|
|
|
|
/// import map, JSX settings.
|
2023-02-22 14:15:25 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2023-02-15 11:30:54 -05:00
|
|
|
pub struct CliGraphResolver {
|
2022-11-02 10:47:02 -04:00
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
maybe_default_jsx_import_source: Option<String>,
|
|
|
|
maybe_jsx_import_source_module: Option<String>,
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm: bool,
|
|
|
|
npm_registry_api: NpmRegistryApi,
|
|
|
|
npm_resolution: NpmResolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer: PackageJsonDepsInstaller,
|
2023-03-04 20:07:11 -05:00
|
|
|
sync_download_queue: Option<Arc<TaskQueue>>,
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CliGraphResolver {
|
|
|
|
fn default() -> Self {
|
|
|
|
// This is not ideal, but necessary for the LSP. In the future, we should
|
|
|
|
// refactor the LSP and force this to be initialized.
|
|
|
|
let npm_registry_api = NpmRegistryApi::new_uninitialized();
|
|
|
|
let npm_resolution =
|
|
|
|
NpmResolution::new(npm_registry_api.clone(), None, None);
|
|
|
|
Self {
|
|
|
|
maybe_import_map: Default::default(),
|
|
|
|
maybe_default_jsx_import_source: Default::default(),
|
|
|
|
maybe_jsx_import_source_module: Default::default(),
|
|
|
|
no_npm: false,
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer: Default::default(),
|
2023-03-04 20:07:11 -05:00
|
|
|
sync_download_queue: Self::create_sync_download_queue(),
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
impl CliGraphResolver {
|
|
|
|
pub fn new(
|
2022-11-02 10:47:02 -04:00
|
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm: bool,
|
|
|
|
npm_registry_api: NpmRegistryApi,
|
|
|
|
npm_resolution: NpmResolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer: PackageJsonDepsInstaller,
|
2023-02-15 11:30:54 -05:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
maybe_import_map,
|
|
|
|
maybe_default_jsx_import_source: maybe_jsx_import_source_config
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|c| c.default_specifier.clone()),
|
|
|
|
maybe_jsx_import_source_module: maybe_jsx_import_source_config
|
|
|
|
.map(|c| c.module),
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm,
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
package_json_deps_installer,
|
2023-03-04 20:07:11 -05:00
|
|
|
sync_download_queue: Self::create_sync_download_queue(),
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 20:07:11 -05:00
|
|
|
fn create_sync_download_queue() -> Option<Arc<TaskQueue>> {
|
2023-02-22 14:15:25 -05:00
|
|
|
if crate::npm::should_sync_download() {
|
2023-03-04 20:07:11 -05:00
|
|
|
Some(Default::default())
|
2023-02-22 14:15:25 -05:00
|
|
|
} else {
|
|
|
|
None
|
2022-01-31 17:33:57 -05:00
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
pub fn as_graph_resolver(&self) -> &dyn Resolver {
|
2021-11-08 20:26:39 -05:00
|
|
|
self
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
|
|
|
|
pub fn as_graph_npm_resolver(&self) -> &dyn NpmResolver {
|
|
|
|
self
|
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
impl Resolver for CliGraphResolver {
|
2022-08-24 13:36:05 -04:00
|
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
2022-11-02 10:47:02 -04:00
|
|
|
self.maybe_default_jsx_import_source.clone()
|
2022-08-24 13:36:05 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
2022-11-02 10:47:02 -04:00
|
|
|
self
|
|
|
|
.maybe_jsx_import_source_module
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE)
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2023-01-24 08:23:19 -05:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-02-23 17:20:23 -05:00
|
|
|
// attempt to resolve with the import map first
|
|
|
|
let maybe_import_map_err = match self
|
|
|
|
.maybe_import_map
|
|
|
|
.as_ref()
|
|
|
|
.map(|import_map| import_map.resolve(specifier, referrer))
|
|
|
|
{
|
|
|
|
Some(Ok(value)) => return Ok(value),
|
|
|
|
Some(Err(err)) => Some(err),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// then with package.json
|
2023-02-24 19:35:43 -05:00
|
|
|
if let Some(deps) = self.package_json_deps_installer.package_deps().as_ref()
|
|
|
|
{
|
2023-02-23 12:33:23 -05:00
|
|
|
if let Some(specifier) = resolve_package_json_dep(specifier, deps)? {
|
|
|
|
return Ok(specifier);
|
|
|
|
}
|
2022-11-02 10:47:02 -04:00
|
|
|
}
|
2023-02-22 17:21:05 -05:00
|
|
|
|
2023-02-23 17:20:23 -05:00
|
|
|
// otherwise, surface the import map error or try resolving when has no import map
|
|
|
|
if let Some(err) = maybe_import_map_err {
|
|
|
|
Err(err.into())
|
|
|
|
} else {
|
|
|
|
deno_graph::resolve_import(specifier, referrer).map_err(|err| err.into())
|
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
|
2023-02-23 12:33:23 -05:00
|
|
|
fn resolve_package_json_dep(
|
|
|
|
specifier: &str,
|
2023-03-03 17:27:05 -05:00
|
|
|
deps: &PackageJsonDeps,
|
|
|
|
) -> Result<Option<ModuleSpecifier>, AnyError> {
|
|
|
|
for (bare_specifier, req_result) in deps {
|
2023-02-23 12:33:23 -05:00
|
|
|
if specifier.starts_with(bare_specifier) {
|
|
|
|
let path = &specifier[bare_specifier.len()..];
|
2023-03-03 17:27:05 -05:00
|
|
|
if path.is_empty() || path.starts_with('/') {
|
|
|
|
let req = req_result.as_ref().map_err(|err| {
|
|
|
|
anyhow!(
|
|
|
|
"Parsing version constraints in the application-level package.json is more strict at the moment.\n\n{:#}",
|
|
|
|
err.clone()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
return Ok(Some(ModuleSpecifier::parse(&format!("npm:{req}{path}"))?));
|
2023-02-23 12:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
impl NpmResolver for CliGraphResolver {
|
|
|
|
fn resolve_builtin_node_module(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Option<String>, UnknownBuiltInNodeModuleError> {
|
|
|
|
if specifier.scheme() != "node" {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let module_name = specifier.path().to_string();
|
|
|
|
if is_builtin_node_module(&module_name) {
|
|
|
|
Ok(Some(module_name))
|
|
|
|
} else {
|
|
|
|
Err(UnknownBuiltInNodeModuleError { module_name })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_and_cache_npm_package_info(
|
|
|
|
&self,
|
|
|
|
package_name: &str,
|
|
|
|
) -> LocalBoxFuture<'static, Result<(), String>> {
|
|
|
|
if self.no_npm {
|
|
|
|
// return it succeeded and error at the import site below
|
|
|
|
return Box::pin(future::ready(Ok(())));
|
|
|
|
}
|
|
|
|
// this will internally cache the package information
|
|
|
|
let package_name = package_name.to_string();
|
|
|
|
let api = self.npm_registry_api.clone();
|
2023-02-24 19:35:43 -05:00
|
|
|
let deps_installer = self.package_json_deps_installer.clone();
|
2023-03-04 20:07:11 -05:00
|
|
|
let maybe_sync_download_queue = self.sync_download_queue.clone();
|
2023-02-22 14:15:25 -05:00
|
|
|
async move {
|
2023-03-04 20:07:11 -05:00
|
|
|
let permit = if let Some(task_queue) = &maybe_sync_download_queue {
|
|
|
|
Some(task_queue.acquire().await)
|
2023-02-22 14:15:25 -05:00
|
|
|
} else {
|
2023-02-24 19:35:43 -05:00
|
|
|
None
|
2023-02-22 14:15:25 -05:00
|
|
|
};
|
2023-02-24 19:35:43 -05:00
|
|
|
|
|
|
|
// trigger an npm install if the package name matches
|
|
|
|
// a package in the package.json
|
|
|
|
//
|
|
|
|
// todo(dsherret): ideally this would only download if a bare
|
|
|
|
// specifiy matched in the package.json, but deno_graph only
|
|
|
|
// calls this once per package name and we might resolve an
|
|
|
|
// npm specifier first which calls this, then a bare specifier
|
|
|
|
// second and that would cause this not to occur.
|
|
|
|
if deps_installer.has_package_name(&package_name) {
|
|
|
|
deps_installer
|
|
|
|
.ensure_top_level_install()
|
|
|
|
.await
|
|
|
|
.map_err(|err| format!("{err:#}"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = api
|
|
|
|
.package_info(&package_name)
|
|
|
|
.await
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|err| format!("{err:#}"));
|
|
|
|
drop(permit);
|
|
|
|
result
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_npm(
|
|
|
|
&self,
|
|
|
|
package_req: &NpmPackageReq,
|
|
|
|
) -> Result<NpmPackageNv, AnyError> {
|
|
|
|
if self.no_npm {
|
|
|
|
bail!("npm specifiers were requested; but --no-npm is specified")
|
|
|
|
}
|
|
|
|
self
|
|
|
|
.npm_resolution
|
2023-02-24 19:35:43 -05:00
|
|
|
.resolve_package_req_as_pending(package_req)
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 12:33:23 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-03-03 17:27:05 -05:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2023-02-23 12:33:23 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_package_json_dep() {
|
|
|
|
fn resolve(
|
|
|
|
specifier: &str,
|
|
|
|
deps: &BTreeMap<String, NpmPackageReq>,
|
|
|
|
) -> Result<Option<String>, String> {
|
2023-03-03 17:27:05 -05:00
|
|
|
let deps = deps
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| (key.to_string(), Ok(value.clone())))
|
|
|
|
.collect();
|
|
|
|
resolve_package_json_dep(specifier, &deps)
|
2023-02-23 12:33:23 -05:00
|
|
|
.map(|s| s.map(|s| s.to_string()))
|
|
|
|
.map_err(|err| err.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
let deps = BTreeMap::from([
|
|
|
|
(
|
|
|
|
"package".to_string(),
|
|
|
|
NpmPackageReq::from_str("package@1.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-alias".to_string(),
|
|
|
|
NpmPackageReq::from_str("package@^1.2").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"@deno/test".to_string(),
|
|
|
|
NpmPackageReq::from_str("@deno/test@~0.2").unwrap(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolve("package", &deps).unwrap(),
|
|
|
|
Some("npm:package@1.0".to_string()),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resolve("package/some_path.ts", &deps).unwrap(),
|
2023-03-03 17:27:05 -05:00
|
|
|
Some("npm:package@1.0/some_path.ts".to_string()),
|
2023-02-23 12:33:23 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolve("@deno/test", &deps).unwrap(),
|
|
|
|
Some("npm:@deno/test@~0.2".to_string()),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resolve("@deno/test/some_path.ts", &deps).unwrap(),
|
2023-03-03 17:27:05 -05:00
|
|
|
Some("npm:@deno/test@~0.2/some_path.ts".to_string()),
|
2023-02-23 12:33:23 -05:00
|
|
|
);
|
|
|
|
// matches the start, but doesn't have the same length or a path
|
|
|
|
assert_eq!(resolve("@deno/testing", &deps).unwrap(), None,);
|
|
|
|
|
|
|
|
// alias
|
|
|
|
assert_eq!(
|
|
|
|
resolve("package-alias", &deps).unwrap(),
|
|
|
|
Some("npm:package@^1.2".to_string()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// non-existent bare specifier
|
|
|
|
assert_eq!(resolve("non-existent", &deps).unwrap(), None);
|
|
|
|
}
|
|
|
|
}
|