From c21f42c825b19d7309d883513d21f62395996f78 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 9 Aug 2024 09:50:37 +0200 Subject: [PATCH] fix(lsp): don't always use byonm resolver when DENO_FUTURE=1 (#24865) Co-authored-by: Nayeem Rahman --- cli/lsp/config.rs | 26 ++++++++++++++++++++++++-- cli/lsp/resolver.rs | 14 +++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/cli/lsp/config.rs b/cli/lsp/config.rs index bbfe3a6dfd..1d6601933b 100644 --- a/cli/lsp/config.rs +++ b/cli/lsp/config.rs @@ -1115,6 +1115,7 @@ pub enum ConfigWatchedFileType { #[derive(Debug, Clone)] pub struct ConfigData { pub scope: Arc, + pub canonicalized_scope: Option>, pub member_dir: Arc, pub fmt_config: Arc, pub lint_config: Arc, @@ -1253,6 +1254,16 @@ impl ConfigData { watched_files.entry(specifier).or_insert(file_type); }; + let canonicalized_scope = (|| { + let path = scope.to_file_path().ok()?; + let path = canonicalize_path_maybe_not_exists(&path).ok()?; + let specifier = ModuleSpecifier::from_directory_path(path).ok()?; + if specifier == *scope { + return None; + } + Some(Arc::new(specifier)) + })(); + if let Some(deno_json) = member_dir.maybe_deno_json() { lsp_log!( " Resolved Deno configuration file: \"{}\"", @@ -1559,6 +1570,7 @@ impl ConfigData { ConfigData { scope, + canonicalized_scope, member_dir, resolver, sloppy_imports_resolver, @@ -1587,6 +1599,15 @@ impl ConfigData { pub fn maybe_pkg_json(&self) -> Option<&Arc> { self.member_dir.maybe_pkg_json() } + + pub fn scope_contains_specifier(&self, specifier: &ModuleSpecifier) -> bool { + specifier.as_str().starts_with(self.scope.as_str()) + || self + .canonicalized_scope + .as_ref() + .map(|s| specifier.as_str().starts_with(s.as_str())) + .unwrap_or(false) + } } #[derive(Clone, Debug, Default)] @@ -1601,8 +1622,9 @@ impl ConfigTree { ) -> Option<&ModuleSpecifier> { self .scopes - .keys() - .rfind(|s| specifier.as_str().starts_with(s.as_str())) + .iter() + .rfind(|(_, d)| d.scope_contains_specifier(specifier)) + .map(|(s, _)| s) } pub fn data_for_specifier( diff --git a/cli/lsp/resolver.rs b/cli/lsp/resolver.rs index dec46566fd..d279031e32 100644 --- a/cli/lsp/resolver.rs +++ b/cli/lsp/resolver.rs @@ -4,7 +4,6 @@ use crate::args::create_default_npmrc; use crate::args::CacheSetting; use crate::args::CliLockfile; use crate::args::PackageJsonInstallDepsProvider; -use crate::args::DENO_FUTURE; use crate::graph_util::CliJsrUrlProvider; use crate::http_util::HttpClientProvider; use crate::lsp::config::Config; @@ -421,9 +420,14 @@ impl LspResolver { }; self .by_scope - .iter() - .rfind(|(s, _)| file_referrer.as_str().starts_with(s.as_str())) - .map(|(_, r)| r.as_ref()) + .values() + .rfind(|r| { + r.config_data + .as_ref() + .map(|d| d.scope_contains_specifier(file_referrer)) + .unwrap_or(false) + }) + .map(|r| r.as_ref()) .unwrap_or(self.unscoped.as_ref()) } } @@ -433,7 +437,7 @@ async fn create_npm_resolver( cache: &LspCache, http_client_provider: &Arc, ) -> Option> { - let enable_byonm = config_data.map(|d| d.byonm).unwrap_or(*DENO_FUTURE); + let enable_byonm = config_data.map(|d| d.byonm).unwrap_or(false); let options = if enable_byonm { CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions { fs: Arc::new(deno_fs::RealFs),