diff options
| author | Kitson Kelly <me@kitsonkelly.com> | 2021-07-28 07:25:09 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-28 07:25:09 +1000 |
| commit | 667b026798b5284e9ec8bf47baba80f343975d2e (patch) | |
| tree | 3e2d85160925222b7052645ffb5fa0c8a4aa476e /cli/lsp | |
| parent | fd0b24b246e3c6ed22e96f67361da654bbff8b48 (diff) | |
feat(lsp): ability to set DENO_DIR via settings (#11527)
Ref: denoland/vscode_deno#287
Diffstat (limited to 'cli/lsp')
| -rw-r--r-- | cli/lsp/README.md | 1 | ||||
| -rw-r--r-- | cli/lsp/config.rs | 5 | ||||
| -rw-r--r-- | cli/lsp/language_server.rs | 99 | ||||
| -rw-r--r-- | cli/lsp/registries.rs | 6 | ||||
| -rw-r--r-- | cli/lsp/sources.rs | 10 |
5 files changed, 103 insertions, 18 deletions
diff --git a/cli/lsp/README.md b/cli/lsp/README.md index 764998fb1..2256c4038 100644 --- a/cli/lsp/README.md +++ b/cli/lsp/README.md @@ -19,6 +19,7 @@ methods that the client calls via the Language Server RPC protocol. There are several settings that the language server supports for a workspace: - `deno.enable` +- `deno.cache` - `deno.config` - `deno.importMap` - `deno.codeLens.implementations` diff --git a/cli/lsp/config.rs b/cli/lsp/config.rs index aeb0e29b9..a58a8d1ae 100644 --- a/cli/lsp/config.rs +++ b/cli/lsp/config.rs @@ -148,6 +148,10 @@ pub struct WorkspaceSettings { #[serde(default)] pub enable: bool, + /// An option that points to a path string of the path to utilise as the + /// cache/DENO_DIR for the language server. + pub cache: Option<String>, + /// An option that points to a path string of the config file to apply to /// code within the workspace. pub config: Option<String>, @@ -475,6 +479,7 @@ mod tests { config.get_workspace_settings(), WorkspaceSettings { enable: false, + cache: None, config: None, import_map: None, code_lens: CodeLensSettings { diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 27d6e4204..ab17d03fb 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -95,6 +95,9 @@ pub(crate) struct Inner { module_registries: registries::ModuleRegistry, /// The path to the module registries cache module_registries_location: PathBuf, + /// An optional path to the DENO_DIR which has been specified in the client + /// options. + maybe_cache_path: Option<PathBuf>, /// An optional configuration file which has been specified in the client /// options. maybe_config_file: Option<ConfigFile>, @@ -144,10 +147,11 @@ impl Inner { config, diagnostics_server, documents: Default::default(), - maybe_config_file: Default::default(), - maybe_config_uri: Default::default(), - maybe_import_map: Default::default(), - maybe_import_map_uri: Default::default(), + maybe_cache_path: None, + maybe_config_file: None, + maybe_config_uri: None, + maybe_import_map: None, + maybe_import_map_uri: None, module_registries, module_registries_location, performance, @@ -358,7 +362,7 @@ impl Inner { self.maybe_config_uri = None; if let Some(config_str) = maybe_config { if !config_str.is_empty() { - info!("Updating TypeScript configuration from: \"{}\"", config_str); + info!("Setting TypeScript configuration from: \"{}\"", config_str); let config_url = if let Ok(url) = Url::from_file_path(config_str) { Ok(url) } else if let Some(root_uri) = maybe_root_uri { @@ -417,6 +421,62 @@ impl Inner { }) } + pub fn update_cache(&mut self) -> Result<(), AnyError> { + let mark = self.performance.mark("update_cache", None::<()>); + self.performance.measure(mark); + let (maybe_cache, maybe_root_uri) = { + let config = &self.config; + ( + config.get_workspace_settings().cache, + config.root_uri.clone(), + ) + }; + let maybe_cache_path = if let Some(cache_str) = &maybe_cache { + info!("Setting cache path from: \"{}\"", cache_str); + let cache_url = if let Ok(url) = Url::from_file_path(cache_str) { + Ok(url) + } else if let Some(root_uri) = &maybe_root_uri { + let root_path = root_uri + .to_file_path() + .map_err(|_| anyhow!("Bad root_uri: {}", root_uri))?; + let cache_path = root_path.join(cache_str); + Url::from_file_path(cache_path).map_err(|_| { + anyhow!("Bad file path for import path: {:?}", cache_str) + }) + } else { + Err(anyhow!( + "The path to the cache path (\"{}\") is not resolvable.", + cache_str + )) + }?; + let cache_path = cache_url.to_file_path().map_err(|_| { + anyhow!("Cannot convert \"{}\" into a file path.", cache_url) + })?; + info!( + " Resolved cache path: \"{}\"", + cache_path.to_string_lossy() + ); + Some(cache_path) + } else { + None + }; + if self.maybe_cache_path != maybe_cache_path { + let maybe_custom_root = maybe_cache_path + .clone() + .or_else(|| env::var("DENO_DIR").map(String::into).ok()); + let dir = deno_dir::DenoDir::new(maybe_custom_root) + .expect("could not access DENO_DIR"); + let module_registries_location = dir.root.join(REGISTRIES_PATH); + self.module_registries = + registries::ModuleRegistry::new(&module_registries_location); + self.module_registries_location = module_registries_location; + let sources_location = dir.root.join(SOURCES_PATH); + self.sources = Sources::new(&sources_location); + self.maybe_cache_path = maybe_cache_path; + } + Ok(()) + } + pub async fn update_import_map(&mut self) -> Result<(), AnyError> { let mark = self.performance.mark("update_import_map", None::<()>); let (maybe_import_map, maybe_root_uri) = { @@ -427,7 +487,7 @@ impl Inner { ) }; if let Some(import_map_str) = &maybe_import_map { - info!("Updating import map from: \"{}\"", import_map_str); + info!("Setting import map from: \"{}\"", import_map_str); let import_map_url = if let Ok(url) = Url::from_file_path(import_map_str) { Ok(url) @@ -620,8 +680,12 @@ impl Inner { } self.update_debug_flag(); + // Check to see if we need to change the cache path + if let Err(err) = self.update_cache() { + self.client.show_message(MessageType::Warning, err).await; + } if let Err(err) = self.update_tsconfig().await { - warn!("Updating tsconfig has errored: {}", err); + self.client.show_message(MessageType::Warning, err).await; } if capabilities.code_action_provider.is_some() { @@ -636,14 +700,6 @@ impl Inner { self.ts_fixable_diagnostics = fixable_diagnostics; } - self.performance.measure(mark); - Ok(InitializeResult { - capabilities, - server_info: Some(server_info), - }) - } - - async fn initialized(&mut self, _: InitializedParams) { // Check to see if we need to setup the import map if let Err(err) = self.update_import_map().await { self.client.show_message(MessageType::Warning, err).await; @@ -653,6 +709,14 @@ impl Inner { self.client.show_message(MessageType::Warning, err).await; } + self.performance.measure(mark); + Ok(InitializeResult { + capabilities, + server_info: Some(server_info), + }) + } + + async fn initialized(&mut self, _: InitializedParams) { if self .config .client_capabilities @@ -836,6 +900,9 @@ impl Inner { } self.update_debug_flag(); + if let Err(err) = self.update_cache() { + self.client.show_message(MessageType::Warning, err).await; + } if let Err(err) = self.update_import_map().await { self.client.show_message(MessageType::Warning, err).await; } @@ -2413,6 +2480,7 @@ impl Inner { &specifier, &self.maybe_import_map, &self.maybe_config_file, + &self.maybe_cache_path, ) .await .map_err(|err| { @@ -2425,6 +2493,7 @@ impl Inner { &referrer, &self.maybe_import_map, &self.maybe_config_file, + &self.maybe_cache_path, ) .await .map_err(|err| { diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index 96c24b43f..d044927bc 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -254,8 +254,10 @@ pub struct ModuleRegistry { impl Default for ModuleRegistry { fn default() -> Self { - let custom_root = std::env::var("DENO_DIR").map(String::into).ok(); - let dir = deno_dir::DenoDir::new(custom_root).unwrap(); + // This only gets used when creating the tsc runtime and for testing, and so + // it shouldn't ever actually access the DenoDir, so it doesn't support a + // custom root. + let dir = deno_dir::DenoDir::new(None).unwrap(); let location = dir.root.join("registries"); let http_cache = HttpCache::new(&location); let cache_setting = CacheSetting::Use; diff --git a/cli/lsp/sources.rs b/cli/lsp/sources.rs index 62c76260c..033a1e9d5 100644 --- a/cli/lsp/sources.rs +++ b/cli/lsp/sources.rs @@ -9,6 +9,7 @@ use crate::config_file::ConfigFile; use crate::file_fetcher::get_source_from_bytes; use crate::file_fetcher::map_content_type; use crate::file_fetcher::SUPPORTED_SCHEMES; +use crate::flags::Flags; use crate::http_cache; use crate::http_cache::HttpCache; use crate::import_map::ImportMap; @@ -36,8 +37,15 @@ pub async fn cache( specifier: &ModuleSpecifier, maybe_import_map: &Option<ImportMap>, maybe_config_file: &Option<ConfigFile>, + maybe_cache_path: &Option<PathBuf>, ) -> Result<(), AnyError> { - let program_state = Arc::new(ProgramState::build(Default::default()).await?); + let program_state = Arc::new( + ProgramState::build(Flags { + cache_path: maybe_cache_path.clone(), + ..Default::default() + }) + .await?, + ); let handler = Arc::new(Mutex::new(FetchHandler::new( &program_state, Permissions::allow_all(), |
