From b78a91d3ba028e4259b3c56c168c36c08d7d9c2c Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 13 Jun 2023 09:24:22 -0400 Subject: [PATCH] fix(lsp): update import map config when deno.json changes (#19476) Half of #19468 --- cli/lsp/language_server.rs | 19 +++++--- cli/tests/integration/lsp_tests.rs | 73 ++++++++++++++++++++++++++++++ test_util/src/lsp.rs | 4 ++ 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index cf71796700..2ac9ec3c08 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -1577,6 +1577,7 @@ impl Inner { touched = true; } } + if let Some(config_info) = self.maybe_config_file_info.as_mut() { if let Some(lockfile) = config_info.maybe_lockfile.as_ref() { let lockfile_path = lockfile.lock().filename.clone(); @@ -1595,6 +1596,7 @@ impl Inner { } } } + if let Some(package_json) = &self.maybe_package_json { // always update the package json if the deno config changes if touched || changes.contains(&package_json.specifier()) { @@ -1604,16 +1606,21 @@ impl Inner { touched = true; } } + // if the current import map, or config file has changed, we need to // reload the import map - if let Some(import_map_uri) = &self.maybe_import_map_uri { - if touched || changes.contains(import_map_uri) { - if let Err(err) = self.update_import_map().await { - self.client.show_message(MessageType::WARNING, err); - } - touched = true; + let import_map_changed = self + .maybe_import_map_uri + .as_ref() + .map(|uri| changes.contains(uri)) + .unwrap_or(false); + if touched || import_map_changed { + if let Err(err) = self.update_import_map().await { + self.client.show_message(MessageType::WARNING, err); } + touched = true; } + if touched { self.recreate_npm_services_if_necessary().await; self.refresh_documents_config(); diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index bd5bc409ad..b6ed08e304 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -359,6 +359,79 @@ fn lsp_import_map_embedded_in_config_file() { client.shutdown(); } +#[test] +fn lsp_import_map_embedded_in_config_file_after_initialize() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write("deno.embedded_import_map.jsonc", "{}"); + temp_dir.create_dir_all("lib"); + temp_dir.write("lib/b.ts", r#"export const b = "b";"#); + + let mut client = context.new_lsp_command().build(); + client.initialize(|builder| { + builder.set_config("./deno.embedded_import_map.jsonc"); + }); + + let uri = temp_dir.uri().join("a.ts").unwrap(); + + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": uri, + "languageId": "typescript", + "version": 1, + "text": "import { b } from \"/~/b.ts\";\n\nconsole.log(b);\n" + } + })); + + assert_eq!(diagnostics.all().len(), 1); + + // update the import map + temp_dir.write( + "deno.embedded_import_map.jsonc", + r#"{ + "imports": { + "/~/": "./lib/" + } +}"#, + ); + + client.did_change_watched_files(json!({ + "changes": [{ + "uri": temp_dir.uri().join("deno.embedded_import_map.jsonc").unwrap(), + "type": 2 + }] + })); + + assert_eq!(client.read_diagnostics().all().len(), 0); + + let res = client.write_request( + "textDocument/hover", + json!({ + "textDocument": { + "uri": uri + }, + "position": { "line": 2, "character": 12 } + }), + ); + assert_eq!( + res, + json!({ + "contents": [ + { + "language": "typescript", + "value":"(alias) const b: \"b\"\nimport b" + }, + "" + ], + "range": { + "start": { "line": 2, "character": 12 }, + "end": { "line": 2, "character": 13 } + } + }) + ); + client.shutdown(); +} + #[test] fn lsp_deno_task() { let context = TestContextBuilder::new().use_temp_cwd().build(); diff --git a/test_util/src/lsp.rs b/test_util/src/lsp.rs index 462779b3a4..cd546bd6cd 100644 --- a/test_util/src/lsp.rs +++ b/test_util/src/lsp.rs @@ -655,6 +655,10 @@ impl LspClient { self.write_response(id, result); } + pub fn did_change_watched_files(&mut self, params: Value) { + self.write_notification("workspace/didChangeWatchedFiles", params); + } + fn get_latest_diagnostic_batch_index(&mut self) -> usize { let result = self .write_request("deno/internalLatestDiagnosticBatchIndex", json!(null));