2024-04-26 16:39:33 -04:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
use dashmap::DashMap;
|
2024-05-13 12:55:31 -04:00
|
|
|
use deno_ast::MediaType;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_cache_dir::HttpCache;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::PackageJsonDepResolution;
|
|
|
|
use deno_config::workspace::WorkspaceResolver;
|
2024-05-09 15:22:27 -04:00
|
|
|
use deno_core::url::Url;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_graph::source::Resolver;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_graph::GraphImport;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_graph::ModuleSpecifier;
|
|
|
|
use deno_npm::NpmSystemInfo;
|
|
|
|
use deno_runtime::deno_fs;
|
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
|
|
|
use deno_runtime::deno_node::PackageJson;
|
|
|
|
use deno_runtime::fs_util::specifier_to_file_path;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_semver::jsr::JsrPackageReqReference;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2024-05-06 22:20:15 -04:00
|
|
|
use indexmap::IndexMap;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::errors::ClosestPkgJsonError;
|
|
|
|
use node_resolver::NodeResolution;
|
|
|
|
use node_resolver::NodeResolutionMode;
|
|
|
|
use node_resolver::NpmResolver;
|
2024-05-09 15:22:27 -04:00
|
|
|
use std::borrow::Cow;
|
2024-06-11 13:14:36 -04:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::BTreeSet;
|
2024-05-06 22:20:15 -04:00
|
|
|
use std::collections::HashMap;
|
2024-05-09 15:22:27 -04:00
|
|
|
use std::collections::HashSet;
|
2024-04-26 16:39:33 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
use super::cache::LspCache;
|
2024-06-10 12:03:17 -04:00
|
|
|
use super::jsr::JsrCacheResolver;
|
2024-09-18 09:51:39 -04:00
|
|
|
use crate::args::create_default_npmrc;
|
|
|
|
use crate::args::CacheSetting;
|
|
|
|
use crate::args::CliLockfile;
|
|
|
|
use crate::args::NpmInstallDepsProvider;
|
|
|
|
use crate::graph_util::CliJsrUrlProvider;
|
|
|
|
use crate::http_util::HttpClientProvider;
|
|
|
|
use crate::lsp::config::Config;
|
|
|
|
use crate::lsp::config::ConfigData;
|
|
|
|
use crate::lsp::logging::lsp_warn;
|
|
|
|
use crate::npm::create_cli_npm_resolver_for_lsp;
|
|
|
|
use crate::npm::CliNpmResolver;
|
|
|
|
use crate::npm::CliNpmResolverByonmCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
|
|
|
use crate::npm::ManagedCliNpmResolver;
|
|
|
|
use crate::resolver::CjsResolutionStore;
|
|
|
|
use crate::resolver::CliGraphResolver;
|
|
|
|
use crate::resolver::CliGraphResolverOptions;
|
|
|
|
use crate::resolver::CliNodeResolver;
|
|
|
|
use crate::resolver::WorkerCliNpmGraphResolver;
|
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
2024-05-09 15:22:27 -04:00
|
|
|
|
2024-04-26 16:39:33 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2024-06-17 16:54:23 -04:00
|
|
|
struct LspScopeResolver {
|
2024-04-26 16:39:33 -04:00
|
|
|
graph_resolver: Arc<CliGraphResolver>,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver: Option<Arc<JsrCacheResolver>>,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: Option<Arc<dyn CliNpmResolver>>,
|
|
|
|
node_resolver: Option<Arc<CliNodeResolver>>,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: Option<Arc<RedirectResolver>>,
|
|
|
|
graph_imports: Arc<IndexMap<ModuleSpecifier, GraphImport>>,
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data: Option<Arc<ConfigData>>,
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-06-17 16:54:23 -04:00
|
|
|
impl Default for LspScopeResolver {
|
2024-04-26 16:39:33 -04:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-05-06 22:20:15 -04:00
|
|
|
graph_resolver: create_graph_resolver(None, None, None),
|
|
|
|
jsr_resolver: None,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: None,
|
|
|
|
node_resolver: None,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: None,
|
|
|
|
graph_imports: Default::default(),
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data: None,
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 16:54:23 -04:00
|
|
|
impl LspScopeResolver {
|
|
|
|
async fn from_config_data(
|
|
|
|
config_data: Option<&Arc<ConfigData>>,
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: Option<&Arc<HttpClientProvider>>,
|
2024-05-28 20:26:43 -04:00
|
|
|
) -> Self {
|
2024-04-26 16:39:33 -04:00
|
|
|
let mut npm_resolver = None;
|
|
|
|
let mut node_resolver = None;
|
2024-06-17 16:54:23 -04:00
|
|
|
if let Some(http_client) = http_client_provider {
|
|
|
|
npm_resolver = create_npm_resolver(
|
|
|
|
config_data.map(|d| d.as_ref()),
|
|
|
|
cache,
|
|
|
|
http_client,
|
|
|
|
)
|
|
|
|
.await;
|
2024-05-28 20:26:43 -04:00
|
|
|
node_resolver = create_node_resolver(npm_resolver.as_ref());
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
let graph_resolver = create_graph_resolver(
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data.map(|d| d.as_ref()),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver.as_ref(),
|
|
|
|
);
|
2024-05-06 22:20:15 -04:00
|
|
|
let jsr_resolver = Some(Arc::new(JsrCacheResolver::new(
|
2024-07-18 18:16:35 -04:00
|
|
|
cache.for_specifier(config_data.map(|d| d.scope.as_ref())),
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data.map(|d| d.as_ref()),
|
2024-05-06 22:20:15 -04:00
|
|
|
)));
|
2024-05-09 15:22:27 -04:00
|
|
|
let redirect_resolver = Some(Arc::new(RedirectResolver::new(
|
2024-07-18 18:16:35 -04:00
|
|
|
cache.for_specifier(config_data.map(|d| d.scope.as_ref())),
|
2024-07-01 18:14:17 -04:00
|
|
|
config_data.and_then(|d| d.lockfile.clone()),
|
2024-05-09 15:22:27 -04:00
|
|
|
)));
|
2024-06-05 11:04:16 -04:00
|
|
|
let npm_graph_resolver = graph_resolver.create_graph_npm_resolver();
|
2024-05-06 22:20:15 -04:00
|
|
|
let graph_imports = config_data
|
2024-07-19 15:56:07 -04:00
|
|
|
.and_then(|d| d.member_dir.workspace.to_compiler_option_types().ok())
|
2024-05-06 22:20:15 -04:00
|
|
|
.map(|imports| {
|
|
|
|
Arc::new(
|
|
|
|
imports
|
|
|
|
.into_iter()
|
|
|
|
.map(|(referrer, imports)| {
|
|
|
|
let graph_import = GraphImport::new(
|
|
|
|
&referrer,
|
|
|
|
imports,
|
|
|
|
&CliJsrUrlProvider,
|
|
|
|
Some(graph_resolver.as_ref()),
|
2024-06-05 11:04:16 -04:00
|
|
|
Some(&npm_graph_resolver),
|
2024-05-06 22:20:15 -04:00
|
|
|
);
|
|
|
|
(referrer, graph_import)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2024-05-28 20:26:43 -04:00
|
|
|
Self {
|
2024-04-26 16:39:33 -04:00
|
|
|
graph_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver,
|
|
|
|
graph_imports,
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data: config_data.cloned(),
|
2024-05-28 20:26:43 -04:00
|
|
|
}
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-06-17 16:54:23 -04:00
|
|
|
fn snapshot(&self) -> Arc<Self> {
|
2024-04-26 16:39:33 -04:00
|
|
|
let npm_resolver =
|
|
|
|
self.npm_resolver.as_ref().map(|r| r.clone_snapshotted());
|
|
|
|
let node_resolver = create_node_resolver(npm_resolver.as_ref());
|
|
|
|
let graph_resolver = create_graph_resolver(
|
2024-06-17 16:54:23 -04:00
|
|
|
self.config_data.as_deref(),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver.as_ref(),
|
|
|
|
);
|
|
|
|
Arc::new(Self {
|
|
|
|
graph_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver: self.jsr_resolver.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: self.redirect_resolver.clone(),
|
|
|
|
graph_imports: self.graph_imports.clone(),
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data: self.config_data.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct LspResolver {
|
|
|
|
unscoped: Arc<LspScopeResolver>,
|
|
|
|
by_scope: BTreeMap<ModuleSpecifier, Arc<LspScopeResolver>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LspResolver {
|
|
|
|
pub async fn from_config(
|
|
|
|
config: &Config,
|
|
|
|
cache: &LspCache,
|
|
|
|
http_client_provider: Option<&Arc<HttpClientProvider>>,
|
|
|
|
) -> Self {
|
|
|
|
let mut by_scope = BTreeMap::new();
|
|
|
|
for (scope, config_data) in config.tree.data_by_scope().as_ref() {
|
|
|
|
by_scope.insert(
|
|
|
|
scope.clone(),
|
|
|
|
Arc::new(
|
|
|
|
LspScopeResolver::from_config_data(
|
|
|
|
Some(config_data),
|
|
|
|
cache,
|
|
|
|
http_client_provider,
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Self {
|
|
|
|
unscoped: Arc::new(
|
2024-07-18 18:16:35 -04:00
|
|
|
LspScopeResolver::from_config_data(None, cache, http_client_provider)
|
|
|
|
.await,
|
2024-06-17 16:54:23 -04:00
|
|
|
),
|
|
|
|
by_scope,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
unscoped: self.unscoped.snapshot(),
|
|
|
|
by_scope: self
|
|
|
|
.by_scope
|
|
|
|
.iter()
|
|
|
|
.map(|(s, r)| (s.clone(), r.snapshot()))
|
|
|
|
.collect(),
|
2024-04-26 16:39:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-06 22:20:15 -04:00
|
|
|
pub fn did_cache(&self) {
|
2024-06-17 16:54:23 -04:00
|
|
|
for resolver in
|
|
|
|
std::iter::once(&self.unscoped).chain(self.by_scope.values())
|
|
|
|
{
|
|
|
|
resolver.jsr_resolver.as_ref().inspect(|r| r.did_cache());
|
2024-07-01 17:08:32 -04:00
|
|
|
resolver
|
|
|
|
.redirect_resolver
|
|
|
|
.as_ref()
|
|
|
|
.inspect(|r| r.did_cache());
|
2024-06-17 16:54:23 -04:00
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-06-11 13:14:36 -04:00
|
|
|
pub async fn set_npm_reqs(
|
2024-04-26 16:39:33 -04:00
|
|
|
&self,
|
2024-06-11 13:14:36 -04:00
|
|
|
reqs: &BTreeMap<Option<ModuleSpecifier>, BTreeSet<PackageReq>>,
|
2024-06-17 16:54:23 -04:00
|
|
|
) {
|
|
|
|
for (scope, resolver) in [(None, &self.unscoped)]
|
2024-06-11 13:14:36 -04:00
|
|
|
.into_iter()
|
2024-06-17 16:54:23 -04:00
|
|
|
.chain(self.by_scope.iter().map(|(s, r)| (Some(s), r)))
|
|
|
|
{
|
|
|
|
if let Some(npm_resolver) = resolver.npm_resolver.as_ref() {
|
|
|
|
if let Some(npm_resolver) = npm_resolver.as_managed() {
|
|
|
|
let reqs = reqs
|
|
|
|
.get(&scope.cloned())
|
|
|
|
.map(|reqs| reqs.iter().cloned().collect::<Vec<_>>())
|
|
|
|
.unwrap_or_default();
|
|
|
|
if let Err(err) = npm_resolver.set_package_reqs(&reqs).await {
|
|
|
|
lsp_warn!("Could not set npm package requirements: {:#}", err);
|
|
|
|
}
|
|
|
|
}
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn as_graph_resolver(
|
|
|
|
&self,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-23 12:31:56 -04:00
|
|
|
) -> &dyn Resolver {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver.graph_resolver.as_ref()
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-06-05 11:04:16 -04:00
|
|
|
pub fn create_graph_npm_resolver(
|
2024-05-23 12:31:56 -04:00
|
|
|
&self,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-06-05 11:04:16 -04:00
|
|
|
) -> WorkerCliNpmGraphResolver {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver.graph_resolver.create_graph_npm_resolver()
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn maybe_managed_npm_resolver(
|
|
|
|
&self,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-23 12:31:56 -04:00
|
|
|
) -> Option<&ManagedCliNpmResolver> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver.npm_resolver.as_ref().and_then(|r| r.as_managed())
|
2024-05-13 12:55:31 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn graph_imports_by_referrer(
|
2024-05-13 12:55:31 -04:00
|
|
|
&self,
|
2024-06-26 18:47:01 -04:00
|
|
|
file_referrer: &ModuleSpecifier,
|
2024-05-23 12:31:56 -04:00
|
|
|
) -> IndexMap<&ModuleSpecifier, Vec<&ModuleSpecifier>> {
|
2024-06-26 18:47:01 -04:00
|
|
|
let resolver = self.get_scope_resolver(Some(file_referrer));
|
|
|
|
resolver
|
|
|
|
.graph_imports
|
2024-05-23 12:31:56 -04:00
|
|
|
.iter()
|
2024-06-26 18:47:01 -04:00
|
|
|
.map(|(s, i)| {
|
|
|
|
(
|
|
|
|
s,
|
|
|
|
i.dependencies
|
|
|
|
.values()
|
|
|
|
.flat_map(|d| d.get_type().or_else(|| d.get_code()))
|
|
|
|
.collect(),
|
|
|
|
)
|
2024-05-23 12:31:56 -04:00
|
|
|
})
|
|
|
|
.collect()
|
2024-05-13 12:55:31 -04:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:03:17 -04:00
|
|
|
pub fn jsr_to_resource_url(
|
2024-05-06 22:20:15 -04:00
|
|
|
&self,
|
|
|
|
req_ref: &JsrPackageReqReference,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<ModuleSpecifier> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver.jsr_resolver.as_ref()?.jsr_to_resource_url(req_ref)
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn jsr_lookup_export_for_path(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
|
|
|
path: &str,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<String> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver
|
|
|
|
.jsr_resolver
|
|
|
|
.as_ref()?
|
|
|
|
.lookup_export_for_path(nv, path)
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn jsr_lookup_req_for_nv(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-23 12:31:56 -04:00
|
|
|
) -> Option<PackageReq> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
resolver.jsr_resolver.as_ref()?.lookup_req_for_nv(nv)
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn npm_to_file_url(
|
2024-05-06 22:20:15 -04:00
|
|
|
&self,
|
2024-05-13 12:55:31 -04:00
|
|
|
req_ref: &NpmPackageReqReference,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-13 12:55:31 -04:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
let node_resolver = resolver.node_resolver.as_ref()?;
|
2024-05-13 12:55:31 -04:00
|
|
|
Some(NodeResolution::into_specifier_and_media_type(
|
|
|
|
node_resolver
|
2024-06-06 23:37:53 -04:00
|
|
|
.resolve_req_reference(req_ref, referrer, NodeResolutionMode::Types)
|
2024-05-13 12:55:31 -04:00
|
|
|
.ok(),
|
|
|
|
))
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn in_node_modules(&self, specifier: &ModuleSpecifier) -> bool {
|
2024-07-10 14:46:25 -04:00
|
|
|
fn has_node_modules_dir(specifier: &ModuleSpecifier) -> bool {
|
|
|
|
// consider any /node_modules/ directory as being in the node_modules
|
|
|
|
// folder for the LSP because it's pretty complicated to deal with multiple scopes
|
|
|
|
specifier.scheme() == "file"
|
|
|
|
&& specifier
|
|
|
|
.path()
|
|
|
|
.to_ascii_lowercase()
|
|
|
|
.contains("/node_modules/")
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
2024-07-10 14:46:25 -04:00
|
|
|
|
|
|
|
let global_npm_resolver = self
|
|
|
|
.get_scope_resolver(Some(specifier))
|
|
|
|
.npm_resolver
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|npm_resolver| npm_resolver.as_managed())
|
|
|
|
.filter(|r| r.root_node_modules_path().is_none());
|
|
|
|
if let Some(npm_resolver) = &global_npm_resolver {
|
|
|
|
if npm_resolver.in_npm_package(specifier) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
has_node_modules_dir(specifier)
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn node_media_type(
|
2024-04-26 16:39:33 -04:00
|
|
|
&self,
|
2024-05-13 12:55:31 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<MediaType> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(Some(specifier));
|
|
|
|
let node_resolver = resolver.node_resolver.as_ref()?;
|
2024-05-13 12:55:31 -04:00
|
|
|
let resolution = node_resolver
|
|
|
|
.url_to_node_resolution(specifier.clone())
|
|
|
|
.ok()?;
|
|
|
|
Some(NodeResolution::into_specifier_and_media_type(Some(resolution)).1)
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_closest_package_json(
|
|
|
|
&self,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<Arc<PackageJson>>, ClosestPkgJsonError> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(Some(referrer));
|
|
|
|
let Some(node_resolver) = resolver.node_resolver.as_ref() else {
|
2024-04-26 16:39:33 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2024-06-26 17:24:10 -04:00
|
|
|
node_resolver.get_closest_package_json(referrer)
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
|
|
|
|
pub fn resolve_redirects(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<ModuleSpecifier> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
let Some(redirect_resolver) = resolver.redirect_resolver.as_ref() else {
|
2024-05-06 22:20:15 -04:00
|
|
|
return Some(specifier.clone());
|
|
|
|
};
|
|
|
|
redirect_resolver.resolve(specifier)
|
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
|
|
|
|
pub fn redirect_chain_headers(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2024-05-09 15:22:27 -04:00
|
|
|
) -> Vec<(ModuleSpecifier, Arc<HashMap<String, String>>)> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let resolver = self.get_scope_resolver(file_referrer);
|
|
|
|
let Some(redirect_resolver) = resolver.redirect_resolver.as_ref() else {
|
2024-05-09 15:22:27 -04:00
|
|
|
return vec![];
|
|
|
|
};
|
|
|
|
redirect_resolver
|
|
|
|
.chain(specifier)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(s, e)| (s, e.headers.clone()))
|
|
|
|
.collect()
|
|
|
|
}
|
2024-06-17 16:54:23 -04:00
|
|
|
|
|
|
|
fn get_scope_resolver(
|
|
|
|
&self,
|
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> &LspScopeResolver {
|
|
|
|
let Some(file_referrer) = file_referrer else {
|
|
|
|
return self.unscoped.as_ref();
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.by_scope
|
2024-08-09 03:50:37 -04:00
|
|
|
.values()
|
|
|
|
.rfind(|r| {
|
|
|
|
r.config_data
|
|
|
|
.as_ref()
|
|
|
|
.map(|d| d.scope_contains_specifier(file_referrer))
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
.map(|r| r.as_ref())
|
2024-06-17 16:54:23 -04:00
|
|
|
.unwrap_or(self.unscoped.as_ref())
|
|
|
|
}
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_npm_resolver(
|
2024-06-17 16:54:23 -04:00
|
|
|
config_data: Option<&ConfigData>,
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: &Arc<HttpClientProvider>,
|
2024-04-26 16:39:33 -04:00
|
|
|
) -> Option<Arc<dyn CliNpmResolver>> {
|
2024-08-09 03:50:37 -04:00
|
|
|
let enable_byonm = config_data.map(|d| d.byonm).unwrap_or(false);
|
2024-07-10 14:46:25 -04:00
|
|
|
let options = if enable_byonm {
|
2024-04-26 16:39:33 -04:00
|
|
|
CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions {
|
|
|
|
fs: Arc::new(deno_fs::RealFs),
|
2024-07-10 14:46:25 -04:00
|
|
|
root_node_modules_dir: config_data.and_then(|config_data| {
|
|
|
|
config_data.node_modules_dir.clone().or_else(|| {
|
|
|
|
specifier_to_file_path(&config_data.scope)
|
|
|
|
.ok()
|
|
|
|
.map(|p| p.join("node_modules/"))
|
|
|
|
})
|
|
|
|
}),
|
2024-04-26 16:39:33 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions {
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: http_client_provider.clone(),
|
2024-06-17 16:54:23 -04:00
|
|
|
snapshot: match config_data.and_then(|d| d.lockfile.as_ref()) {
|
2024-04-26 16:39:33 -04:00
|
|
|
Some(lockfile) => {
|
|
|
|
CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(
|
|
|
|
lockfile.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => CliNpmResolverManagedSnapshotOption::Specified(None),
|
|
|
|
},
|
|
|
|
// Don't provide the lockfile. We don't want these resolvers
|
|
|
|
// updating it. Only the cache request should update the lockfile.
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: Arc::new(deno_fs::RealFs),
|
2024-05-09 15:22:27 -04:00
|
|
|
npm_global_cache_dir: cache.deno_dir().npm_folder_path(),
|
2024-04-26 16:39:33 -04:00
|
|
|
// Use an "only" cache setting in order to make the
|
|
|
|
// user do an explicit "cache" command and prevent
|
|
|
|
// the cache from being filled with lots of packages while
|
|
|
|
// the user is typing.
|
|
|
|
cache_setting: CacheSetting::Only,
|
|
|
|
text_only_progress_bar: ProgressBar::new(ProgressBarStyle::TextOnly),
|
2024-06-17 16:54:23 -04:00
|
|
|
maybe_node_modules_path: config_data
|
|
|
|
.and_then(|d| d.node_modules_dir.clone()),
|
2024-07-03 20:54:33 -04:00
|
|
|
// only used for top level install, so we can ignore this
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider: Arc::new(NpmInstallDepsProvider::empty()),
|
2024-05-23 17:26:23 -04:00
|
|
|
npmrc: config_data
|
2024-06-17 16:54:23 -04:00
|
|
|
.and_then(|d| d.npmrc.clone())
|
2024-05-23 17:26:23 -04:00
|
|
|
.unwrap_or_else(create_default_npmrc),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_system_info: NpmSystemInfo::default(),
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: Default::default(),
|
2024-04-26 16:39:33 -04:00
|
|
|
})
|
|
|
|
};
|
|
|
|
Some(create_cli_npm_resolver_for_lsp(options).await)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_node_resolver(
|
|
|
|
npm_resolver: Option<&Arc<dyn CliNpmResolver>>,
|
|
|
|
) -> Option<Arc<CliNodeResolver>> {
|
2024-07-10 14:46:25 -04:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
// it's not ideal to share this across all scopes and to
|
|
|
|
// never clear it, but it's fine for the time being
|
|
|
|
static CJS_RESOLUTIONS: Lazy<Arc<CjsResolutionStore>> =
|
|
|
|
Lazy::new(Default::default);
|
|
|
|
|
2024-04-26 16:39:33 -04:00
|
|
|
let npm_resolver = npm_resolver?;
|
|
|
|
let fs = Arc::new(deno_fs::RealFs);
|
|
|
|
let node_resolver_inner = Arc::new(NodeResolver::new(
|
2024-07-25 19:08:14 -04:00
|
|
|
deno_runtime::deno_node::DenoFsNodeResolverEnv::new(fs.clone()),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver.clone().into_npm_resolver(),
|
|
|
|
));
|
|
|
|
Some(Arc::new(CliNodeResolver::new(
|
2024-07-10 14:46:25 -04:00
|
|
|
CJS_RESOLUTIONS.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
fs,
|
|
|
|
node_resolver_inner,
|
|
|
|
npm_resolver.clone(),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_graph_resolver(
|
2024-05-06 22:20:15 -04:00
|
|
|
config_data: Option<&ConfigData>,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: Option<&Arc<dyn CliNpmResolver>>,
|
|
|
|
node_resolver: Option<&Arc<CliNodeResolver>>,
|
|
|
|
) -> Arc<CliGraphResolver> {
|
2024-07-19 15:56:07 -04:00
|
|
|
let workspace = config_data.map(|d| &d.member_dir.workspace);
|
2024-04-26 16:39:33 -04:00
|
|
|
Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
|
|
|
node_resolver: node_resolver.cloned(),
|
|
|
|
npm_resolver: npm_resolver.cloned(),
|
2024-07-18 18:16:35 -04:00
|
|
|
workspace_resolver: config_data.map(|d| d.resolver.clone()).unwrap_or_else(
|
|
|
|
|| {
|
|
|
|
Arc::new(WorkspaceResolver::new_raw(
|
|
|
|
// this is fine because this is only used before initialization
|
|
|
|
Arc::new(ModuleSpecifier::parse("file:///").unwrap()),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2024-08-07 03:43:05 -04:00
|
|
|
Vec::new(),
|
2024-07-18 18:16:35 -04:00
|
|
|
PackageJsonDepResolution::Disabled,
|
|
|
|
))
|
2024-07-03 20:54:33 -04:00
|
|
|
},
|
2024-07-18 18:16:35 -04:00
|
|
|
),
|
2024-07-19 15:56:07 -04:00
|
|
|
maybe_jsx_import_source_config: workspace.and_then(|workspace| {
|
|
|
|
workspace.to_maybe_jsx_import_source_config().ok().flatten()
|
|
|
|
}),
|
2024-04-26 16:39:33 -04:00
|
|
|
maybe_vendor_dir: config_data.and_then(|d| d.vendor_dir.as_ref()),
|
2024-07-18 18:16:35 -04:00
|
|
|
bare_node_builtins_enabled: workspace
|
2024-07-19 15:56:07 -04:00
|
|
|
.is_some_and(|workspace| workspace.has_unstable("bare-node-builtins")),
|
2024-07-25 09:07:59 -04:00
|
|
|
sloppy_imports_resolver: config_data
|
|
|
|
.and_then(|d| d.sloppy_imports_resolver.clone()),
|
2024-04-26 16:39:33 -04:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
struct RedirectEntry {
|
|
|
|
headers: Arc<HashMap<String, String>>,
|
|
|
|
target: Url,
|
|
|
|
destination: Option<Url>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetHeadersFn =
|
|
|
|
Box<dyn Fn(&Url) -> Option<HashMap<String, String>> + Send + Sync>;
|
|
|
|
|
2024-05-06 22:20:15 -04:00
|
|
|
struct RedirectResolver {
|
2024-05-09 15:22:27 -04:00
|
|
|
get_headers: GetHeadersFn,
|
|
|
|
entries: DashMap<Url, Option<Arc<RedirectEntry>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for RedirectResolver {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("RedirectResolver")
|
|
|
|
.field("get_headers", &"Box(|_| { ... })")
|
|
|
|
.field("entries", &self.entries)
|
|
|
|
.finish()
|
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedirectResolver {
|
2024-07-01 17:08:32 -04:00
|
|
|
fn new(
|
|
|
|
cache: Arc<dyn HttpCache>,
|
2024-07-01 18:14:17 -04:00
|
|
|
lockfile: Option<Arc<CliLockfile>>,
|
2024-07-01 17:08:32 -04:00
|
|
|
) -> Self {
|
|
|
|
let entries = DashMap::new();
|
|
|
|
if let Some(lockfile) = lockfile {
|
|
|
|
for (source, destination) in &lockfile.lock().content.redirects {
|
|
|
|
let Ok(source) = ModuleSpecifier::parse(source) else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let Ok(destination) = ModuleSpecifier::parse(destination) else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
entries.insert(
|
|
|
|
source,
|
|
|
|
Some(Arc::new(RedirectEntry {
|
|
|
|
headers: Default::default(),
|
|
|
|
target: destination.clone(),
|
|
|
|
destination: Some(destination.clone()),
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
entries.insert(destination, None);
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
Self {
|
2024-05-09 15:22:27 -04:00
|
|
|
get_headers: Box::new(move |specifier| {
|
|
|
|
let cache_key = cache.cache_item_key(specifier).ok()?;
|
|
|
|
cache.read_headers(&cache_key).ok().flatten()
|
|
|
|
}),
|
2024-07-01 17:08:32 -04:00
|
|
|
entries,
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
fn mock(get_headers: GetHeadersFn) -> Self {
|
|
|
|
Self {
|
|
|
|
get_headers,
|
|
|
|
entries: Default::default(),
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
fn resolve(&self, specifier: &Url) -> Option<Url> {
|
|
|
|
if !matches!(specifier.scheme(), "http" | "https") {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
}
|
|
|
|
let mut current = specifier.clone();
|
|
|
|
let mut chain = vec![];
|
|
|
|
let destination = loop {
|
|
|
|
if let Some(maybe_entry) = self.entries.get(¤t) {
|
|
|
|
break match maybe_entry.as_ref() {
|
|
|
|
Some(entry) => entry.destination.clone(),
|
|
|
|
None => Some(current),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let Some(headers) = (self.get_headers)(¤t) else {
|
|
|
|
break None;
|
|
|
|
};
|
|
|
|
let headers = Arc::new(headers);
|
2024-05-06 22:20:15 -04:00
|
|
|
if let Some(location) = headers.get("location") {
|
2024-05-09 15:22:27 -04:00
|
|
|
if chain.len() > 10 {
|
|
|
|
break None;
|
|
|
|
}
|
|
|
|
let Ok(target) =
|
|
|
|
deno_core::resolve_import(location, specifier.as_str())
|
|
|
|
else {
|
|
|
|
break None;
|
|
|
|
};
|
|
|
|
chain.push((
|
|
|
|
current.clone(),
|
|
|
|
RedirectEntry {
|
|
|
|
headers,
|
|
|
|
target: target.clone(),
|
|
|
|
destination: None,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
current = target;
|
2024-05-06 22:20:15 -04:00
|
|
|
} else {
|
2024-05-09 15:22:27 -04:00
|
|
|
self.entries.insert(current.clone(), None);
|
|
|
|
break Some(current);
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
};
|
|
|
|
for (specifier, mut entry) in chain {
|
2024-06-14 07:40:57 -04:00
|
|
|
entry.destination.clone_from(&destination);
|
2024-05-09 15:22:27 -04:00
|
|
|
self.entries.insert(specifier, Some(Arc::new(entry)));
|
|
|
|
}
|
|
|
|
destination
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chain(&self, specifier: &Url) -> Vec<(Url, Arc<RedirectEntry>)> {
|
|
|
|
self.resolve(specifier);
|
|
|
|
let mut result = vec![];
|
|
|
|
let mut seen = HashSet::new();
|
|
|
|
let mut current = Cow::Borrowed(specifier);
|
|
|
|
loop {
|
|
|
|
let Some(maybe_entry) = self.entries.get(¤t) else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
let Some(entry) = maybe_entry.as_ref() else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
result.push((current.as_ref().clone(), entry.clone()));
|
|
|
|
seen.insert(current.as_ref().clone());
|
|
|
|
if seen.contains(&entry.target) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current = Cow::Owned(entry.target.clone())
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
result
|
|
|
|
}
|
2024-07-01 17:08:32 -04:00
|
|
|
|
|
|
|
fn did_cache(&self) {
|
|
|
|
self.entries.retain(|_, entry| entry.is_some());
|
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirect_resolver() {
|
|
|
|
let redirect_resolver =
|
|
|
|
RedirectResolver::mock(Box::new(|specifier| match specifier.as_str() {
|
|
|
|
"https://foo/redirect_2.js" => Some(
|
|
|
|
[("location".to_string(), "./redirect_1.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
"https://foo/redirect_1.js" => Some(
|
|
|
|
[("location".to_string(), "./file.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
"https://foo/file.js" => Some([].into_iter().collect()),
|
|
|
|
_ => None,
|
|
|
|
}));
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver.resolve(&Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.resolve(&Url::parse("https://foo/redirect_1.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.resolve(&Url::parse("https://foo/redirect_2.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver.resolve(&Url::parse("https://foo/unknown").unwrap()),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.chain(&Url::parse("https://foo/redirect_2.js").unwrap()),
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
Url::parse("https://foo/redirect_2.js").unwrap(),
|
|
|
|
Arc::new(RedirectEntry {
|
|
|
|
headers: Arc::new(
|
|
|
|
[("location".to_string(), "./redirect_1.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
target: Url::parse("https://foo/redirect_1.js").unwrap(),
|
|
|
|
destination: Some(Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Url::parse("https://foo/redirect_1.js").unwrap(),
|
|
|
|
Arc::new(RedirectEntry {
|
|
|
|
headers: Arc::new(
|
|
|
|
[("location".to_string(), "./file.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
target: Url::parse("https://foo/file.js").unwrap(),
|
|
|
|
destination: Some(Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|