diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-01-18 15:57:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-18 15:57:30 -0500 |
commit | 35c1652f56f2a83380080b2c9942c595f80bd14d (patch) | |
tree | 4867dacfcbcbb01e7e1766d2fd8ce5e0a62ddf2b /cli/lsp | |
parent | 4e3aff8400fe1e6854a27687d14a07dc837c88d9 (diff) |
fix(lsp): regression - formatting was broken on windows (#21972)
~~Waiting on: https://github.com/denoland/deno_config/pull/31~~
Closes #21971
Closes https://github.com/denoland/vscode_deno/issues/1029
Diffstat (limited to 'cli/lsp')
-rw-r--r-- | cli/lsp/diagnostics.rs | 10 | ||||
-rw-r--r-- | cli/lsp/documents.rs | 131 | ||||
-rw-r--r-- | cli/lsp/language_server.rs | 37 |
3 files changed, 97 insertions, 81 deletions
diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index fb1849186..a09f7169d 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -1540,6 +1540,7 @@ mod tests { use crate::lsp::documents::Documents; use crate::lsp::documents::LanguageId; use crate::lsp::language_server::StateSnapshot; + use deno_config::glob::FilePatterns; use pretty_assertions::assert_eq; use std::path::Path; use std::path::PathBuf; @@ -1640,6 +1641,11 @@ let c: number = "a"; Arc::new(GlobalHttpCache::new(cache_location, RealDenoCacheEnv)); let ts_server = TsServer::new(Default::default(), cache); ts_server.start(None); + let lint_options = LintOptions { + rules: Default::default(), + files: FilePatterns::new_with_base(temp_dir.path().to_path_buf()), + reporter_kind: Default::default(), + }; // test enabled { @@ -1647,7 +1653,7 @@ let c: number = "a"; let diagnostics = generate_lint_diagnostics( &snapshot, &enabled_config, - &Default::default(), + &lint_options, Default::default(), ); assert_eq!(get_diagnostics_for_single(diagnostics).len(), 6); @@ -1679,7 +1685,7 @@ let c: number = "a"; let diagnostics = generate_lint_diagnostics( &snapshot, &disabled_config, - &Default::default(), + &lint_options, Default::default(), ); assert_eq!(get_diagnostics_for_single(diagnostics).len(), 0); diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index a03c08387..611d1d07f 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -844,8 +844,7 @@ impl FileSystemDocuments { } pub struct UpdateDocumentConfigOptions<'a> { - pub enabled_paths: PathOrPatternSet, - pub disabled_paths: PathOrPatternSet, + pub file_patterns: FilePatterns, pub document_preload_limit: usize, pub maybe_import_map: Option<Arc<import_map::ImportMap>>, pub maybe_config_file: Option<&'a ConfigFile>, @@ -1321,8 +1320,7 @@ impl Documents { pub fn update_config(&mut self, options: UpdateDocumentConfigOptions) { #[allow(clippy::too_many_arguments)] fn calculate_resolver_config_hash( - enabled_paths: &PathOrPatternSet, - disabled_paths: &PathOrPatternSet, + file_patterns: &FilePatterns, document_preload_limit: usize, maybe_import_map: Option<&import_map::ImportMap>, maybe_jsx_config: Option<&JsxImportSourceConfig>, @@ -1349,8 +1347,10 @@ impl Documents { let mut hasher = FastInsecureHasher::default(); hasher.write_hashable(document_preload_limit); - hasher.write_hashable(&get_pattern_set_vec(enabled_paths)); - hasher.write_hashable(&get_pattern_set_vec(disabled_paths)); + hasher.write_hashable( + &file_patterns.include.as_ref().map(get_pattern_set_vec), + ); + hasher.write_hashable(&get_pattern_set_vec(&file_patterns.exclude)); if let Some(import_map) = maybe_import_map { hasher.write_str(&import_map.to_json()); hasher.write_str(import_map.base_url().as_str()); @@ -1387,8 +1387,7 @@ impl Documents { .maybe_config_file .and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten()); let new_resolver_config_hash = calculate_resolver_config_hash( - &options.enabled_paths, - &options.disabled_paths, + &options.file_patterns, options.document_preload_limit, options.maybe_import_map.as_deref(), maybe_jsx_config.as_ref(), @@ -1450,8 +1449,7 @@ impl Documents { // only refresh the dependencies if the underlying configuration has changed if self.resolver_config_hash != new_resolver_config_hash { self.refresh_dependencies( - options.enabled_paths, - options.disabled_paths, + options.file_patterns, options.document_preload_limit, ); self.resolver_config_hash = new_resolver_config_hash; @@ -1464,8 +1462,7 @@ impl Documents { fn refresh_dependencies( &mut self, - enabled_paths: PathOrPatternSet, - disabled_paths: PathOrPatternSet, + file_patterns: FilePatterns, document_preload_limit: usize, ) { let resolver = self.resolver.as_graph_resolver(); @@ -1487,8 +1484,7 @@ impl Documents { log::debug!("Preloading documents from enabled urls..."); let mut finder = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths, - disabled_paths, + file_patterns, limit: document_preload_limit, }); for specifier in finder.by_ref() { @@ -1900,8 +1896,7 @@ enum PendingEntry { } struct PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet, - disabled_paths: PathOrPatternSet, + file_patterns: FilePatterns, limit: usize, } @@ -1933,24 +1928,21 @@ impl PreloadDocumentFinder { visited_paths: Default::default(), }; - let file_patterns = FilePatterns { - include: Some(options.enabled_paths), - exclude: options.disabled_paths, - }; - let file_patterns_by_base = file_patterns.split_by_base(); + let file_patterns_by_base = options.file_patterns.split_by_base(); // initialize the finder with the initial paths - for (path, file_patterns) in file_patterns_by_base { + for file_patterns in file_patterns_by_base { + let path = &file_patterns.base; if path.is_dir() { - if is_allowed_root_dir(&path) { + if is_allowed_root_dir(path) { finder .root_dir_entries - .push(PendingEntry::Dir(path, Rc::new(file_patterns))); + .push(PendingEntry::Dir(path.clone(), Rc::new(file_patterns))); } } else { finder .pending_entries - .push_back(PendingEntry::SpecifiedRootFile(path)); + .push_back(PendingEntry::SpecifiedRootFile(path.clone())); } } finder @@ -2247,8 +2239,9 @@ console.log(b, "hello deno"); .unwrap(); documents.update_config(UpdateDocumentConfigOptions { - enabled_paths: Default::default(), - disabled_paths: Default::default(), + file_patterns: FilePatterns::new_with_base( + documents_path.to_path_buf(), + ), document_preload_limit: 1_000, maybe_import_map: Some(Arc::new(import_map)), maybe_config_file: None, @@ -2289,8 +2282,9 @@ console.log(b, "hello deno"); .unwrap(); documents.update_config(UpdateDocumentConfigOptions { - enabled_paths: Default::default(), - disabled_paths: Default::default(), + file_patterns: FilePatterns::new_with_base( + documents_path.to_path_buf(), + ), document_preload_limit: 1_000, maybe_import_map: Some(Arc::new(import_map)), maybe_config_file: None, @@ -2357,17 +2351,22 @@ console.log(b, "hello deno"); temp_dir.write("root3/mod.ts", ""); // no, not provided let mut urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet::from_relative_path_or_patterns( - temp_dir.path().as_path(), - &[ - "root1".to_string(), - "root2/file1.ts".to_string(), - "root2/main.min.ts".to_string(), - "root2/folder".to_string(), - ], - ) - .unwrap(), - disabled_paths: Default::default(), + file_patterns: FilePatterns { + base: temp_dir.path().to_path_buf(), + include: Some( + PathOrPatternSet::from_relative_path_or_patterns( + temp_dir.path().as_path(), + &[ + "root1".to_string(), + "root2/file1.ts".to_string(), + "root2/main.min.ts".to_string(), + "root2/folder".to_string(), + ], + ) + .unwrap(), + ), + exclude: Default::default(), + }, limit: 1_000, }) .collect::<Vec<_>>(); @@ -2397,10 +2396,11 @@ console.log(b, "hello deno"); // now try iterating with a low limit let urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path( - temp_dir.path().to_path_buf(), - )]), - disabled_paths: Default::default(), + file_patterns: FilePatterns { + base: temp_dir.path().to_path_buf(), + include: Default::default(), + exclude: Default::default(), + }, limit: 10, // entries and not results }) .collect::<Vec<_>>(); @@ -2412,18 +2412,19 @@ console.log(b, "hello deno"); // now try with certain directories and files disabled let mut urls = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path( - temp_dir.path().to_path_buf(), - )]), - disabled_paths: PathOrPatternSet::from_relative_path_or_patterns( - temp_dir.path().as_path(), - &[ - "root1".to_string(), - "root2/file1.ts".to_string(), - "**/*.js".to_string(), // ignore js files - ], - ) - .unwrap(), + file_patterns: FilePatterns { + base: temp_dir.path().to_path_buf(), + include: Default::default(), + exclude: PathOrPatternSet::from_relative_path_or_patterns( + temp_dir.path().as_path(), + &[ + "root1".to_string(), + "root2/file1.ts".to_string(), + "**/*.js".to_string(), // ignore js files + ], + ) + .unwrap(), + }, limit: 1_000, }) .collect::<Vec<_>>(); @@ -2443,20 +2444,22 @@ console.log(b, "hello deno"); pub fn test_pre_load_document_finder_disallowed_dirs() { if cfg!(windows) { let paths = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path( - PathBuf::from("C:\\"), - )]), - disabled_paths: Default::default(), + file_patterns: FilePatterns { + base: PathBuf::from("C:\\"), + include: Default::default(), + exclude: Default::default(), + }, limit: 1_000, }) .collect::<Vec<_>>(); assert_eq!(paths, vec![]); } else { let paths = PreloadDocumentFinder::new(PreloadDocumentFinderOptions { - enabled_paths: PathOrPatternSet::new(vec![PathOrPattern::Path( - PathBuf::from("/"), - )]), - disabled_paths: Default::default(), + file_patterns: FilePatterns { + base: PathBuf::from("/"), + include: Default::default(), + exclude: Default::default(), + }, limit: 1_000, }) .collect::<Vec<_>>(); diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 921e34bcd..8355b4fe2 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -2,8 +2,8 @@ use base64::Engine; use deno_ast::MediaType; +use deno_config::glob::FilePatterns; use deno_core::anyhow::anyhow; -use deno_core::anyhow::Context; use deno_core::error::AnyError; use deno_core::parking_lot::Mutex; use deno_core::resolve_url; @@ -29,6 +29,7 @@ use std::collections::HashMap; use std::collections::HashSet; use std::env; use std::fmt::Write as _; +use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use tokio::sync::mpsc::unbounded_channel; @@ -237,6 +238,7 @@ pub struct Inner { /// The collection of documents that the server is currently handling, either /// on disk or "open" within the client. pub documents: Documents, + initial_cwd: PathBuf, http_client: Arc<HttpClient>, task_queue: LanguageServerTaskQueue, /// Handles module registries, which allow discovery of modules @@ -527,6 +529,9 @@ impl Inner { diagnostics_state.clone(), ); let assets = Assets::new(ts_server.clone()); + let initial_cwd = std::env::current_dir().unwrap_or_else(|_| { + panic!("Could not resolve current working directory") + }); Self { assets, @@ -538,13 +543,14 @@ impl Inner { diagnostics_server, documents, http_client, + initial_cwd: initial_cwd.clone(), maybe_global_cache_path: None, maybe_import_map: None, maybe_import_map_uri: None, maybe_package_json: None, - fmt_options: Default::default(), + fmt_options: FmtOptions::new_with_base(initial_cwd.clone()), task_queue: Default::default(), - lint_options: Default::default(), + lint_options: LintOptions::new_with_base(initial_cwd), maybe_testing_server: None, module_registries, module_registries_location, @@ -874,6 +880,7 @@ impl Inner { let npm_resolver = create_npm_resolver( &deno_dir, + &self.initial_cwd, &self.http_client, self.config.maybe_config_file(), self.config.maybe_lockfile(), @@ -1043,15 +1050,13 @@ impl Inner { async fn update_config_file(&mut self) -> Result<(), AnyError> { self.config.clear_config_file(); - self.fmt_options = Default::default(); - self.lint_options = Default::default(); + self.fmt_options = FmtOptions::new_with_base(self.initial_cwd.clone()); + self.lint_options = LintOptions::new_with_base(self.initial_cwd.clone()); if let Some(config_file) = self.get_config_file()? { - // this doesn't need to be an actual directory because flags is specified as `None` - let dummy_args_cwd = PathBuf::from("/"); let lint_options = config_file .to_lint_config() .and_then(|maybe_lint_config| { - LintOptions::resolve(maybe_lint_config, None, &dummy_args_cwd) + LintOptions::resolve(maybe_lint_config, None, &self.initial_cwd) }) .map_err(|err| { anyhow!("Unable to update lint configuration: {:?}", err) @@ -1059,7 +1064,7 @@ impl Inner { let fmt_options = config_file .to_fmt_config() .and_then(|maybe_fmt_config| { - FmtOptions::resolve(maybe_fmt_config, None, &dummy_args_cwd) + FmtOptions::resolve(maybe_fmt_config, None, &self.initial_cwd) }) .map_err(|err| { anyhow!("Unable to update formatter configuration: {:?}", err) @@ -1148,6 +1153,7 @@ impl Inner { async fn create_npm_resolver( deno_dir: &DenoDir, + initial_cwd: &Path, http_client: &Arc<HttpClient>, maybe_config_file: Option<&ConfigFile>, maybe_lockfile: Option<&Arc<Mutex<Lockfile>>>, @@ -1161,9 +1167,7 @@ async fn create_npm_resolver( create_cli_npm_resolver_for_lsp(if is_byonm { CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions { fs: Arc::new(deno_fs::RealFs), - root_node_modules_dir: std::env::current_dir() - .unwrap() - .join("node_modules"), + root_node_modules_dir: initial_cwd.join("node_modules"), }) } else { CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions { @@ -1348,8 +1352,11 @@ impl Inner { async fn refresh_documents_config(&mut self) { self.documents.update_config(UpdateDocumentConfigOptions { - enabled_paths: self.config.get_enabled_paths(), - disabled_paths: self.config.get_disabled_paths(), + file_patterns: FilePatterns { + base: self.initial_cwd.clone(), + include: Some(self.config.get_enabled_paths()), + exclude: self.config.get_disabled_paths(), + }, document_preload_limit: self .config .workspace_settings() @@ -3722,7 +3729,7 @@ impl Inner { type_check_mode: crate::args::TypeCheckMode::Local, ..Default::default() }, - std::env::current_dir().with_context(|| "Failed getting cwd.")?, + self.initial_cwd.clone(), self.config.maybe_config_file().cloned(), self.config.maybe_lockfile().cloned(), self.maybe_package_json.clone(), |