summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-06-13 09:24:22 -0400
committerGitHub <noreply@github.com>2023-06-13 09:24:22 -0400
commit39bf1d2fd50a6921eef9e8f9a3f28c3c2b86d0fe (patch)
treeddd025dc3aee29e870c99a172fa3c3b1eeef39cf
parent07cbec4a822ea1b535511195217138d6e8b679ef (diff)
fix(lsp): update import map config when deno.json changes (#19476)
Half of #19468
-rw-r--r--cli/lsp/language_server.rs19
-rw-r--r--cli/tests/integration/lsp_tests.rs73
-rw-r--r--test_util/src/lsp.rs4
3 files changed, 90 insertions, 6 deletions
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index cf7179670..2ac9ec3c0 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 bd5bc409a..b6ed08e30 100644
--- a/cli/tests/integration/lsp_tests.rs
+++ b/cli/tests/integration/lsp_tests.rs
@@ -360,6 +360,79 @@ fn lsp_import_map_embedded_in_config_file() {
}
#[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();
let temp_dir = context.temp_dir();
diff --git a/test_util/src/lsp.rs b/test_util/src/lsp.rs
index 462779b3a..cd546bd6c 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));