From d472c48b388992e2e0b059492dddf50400520809 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Mon, 8 Jul 2024 22:31:27 +0100 Subject: fix(lsp): inherit workspace-root-only fields in members (#24440) --- tests/integration/lsp_tests.rs | 402 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 401 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index f66fc97be..2111c6f07 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -12366,7 +12366,7 @@ fn lsp_deno_json_scopes_import_map() { } #[test] -fn lsp_deno_json_scopes_vendor_dirs() { +fn lsp_deno_json_scopes_vendor_dir() { let context = TestContextBuilder::new() .use_http_server() .use_temp_cwd() @@ -12550,6 +12550,194 @@ fn lsp_deno_json_scopes_vendor_dirs() { client.shutdown(); } +#[test] +fn lsp_deno_json_scopes_node_modules_dir() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + temp_dir.create_dir_all("project1"); + temp_dir.create_dir_all("project2/project3"); + temp_dir.write( + "project1/deno.json", + json!({ + "nodeModulesDir": true, + }) + .to_string(), + ); + temp_dir.write( + "project2/deno.json", + json!({ + "nodeModulesDir": true, + }) + .to_string(), + ); + temp_dir.write( + "project2/project3/deno.json", + json!({ + "nodeModulesDir": true, + }) + .to_string(), + ); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"npm:@denotest/add@1\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project1/file.ts").unwrap()], + }), + ); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/file.ts").unwrap(), + }, + "position": { "line": 0, "character": 7 }, + }), + ); + // The temp dir is symlinked in macos, and `node_modules` is canonicalized. + let canon_temp_dir = + Url::from_directory_path(temp_dir.path().canonicalize()).unwrap(); + assert_eq!( + res, + json!([{ + "targetUri": canon_temp_dir.join("project1/node_modules/.deno/@denotest+add@1.0.0/node_modules/@denotest/add/index.d.ts").unwrap(), + "targetRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + }]), + ); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project2/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"npm:@denotest/add@1\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project2/file.ts").unwrap()], + }), + ); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("project2/file.ts").unwrap(), + }, + "position": { "line": 0, "character": 7 }, + }), + ); + assert_eq!( + res, + json!([{ + "targetUri": canon_temp_dir.join("project2/node_modules/.deno/@denotest+add@1.0.0/node_modules/@denotest/add/index.d.ts").unwrap(), + "targetRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + }]), + ); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project2/project3/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"npm:@denotest/add@1\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project2/project3/file.ts").unwrap()], + }), + ); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("project2/project3/file.ts").unwrap(), + }, + "position": { "line": 0, "character": 7 }, + }), + ); + assert_eq!( + res, + json!([{ + "targetUri": canon_temp_dir.join("project2/project3/node_modules/.deno/@denotest+add@1.0.0/node_modules/@denotest/add/index.d.ts").unwrap(), + "targetRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + }]), + ); + client.shutdown(); +} + #[test] fn lsp_deno_json_scopes_ts_config() { let context = TestContextBuilder::new().use_temp_cwd().build(); @@ -13369,6 +13557,218 @@ fn lsp_deno_json_workspace_import_map() { client.shutdown(); } +#[test] +fn lsp_workspace_lockfile() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + temp_dir.create_dir_all("project1/project2"); + temp_dir.write( + "project1/deno.json", + json!({ + "workspace": ["project2"], + }) + .to_string(), + ); + temp_dir.write("project1/deno.lock", json!({ + "version": "3", + "redirects": { + "http://localhost:4545/subdir/mod1.ts": "http://localhost:4545/subdir/mod2.ts", + }, + "remote": {}, + }).to_string()); + temp_dir.write("project1/project2/deno.json", json!({}).to_string()); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"http://localhost:4545/subdir/mod1.ts\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project1/project2/file.ts").unwrap()], + }), + ); + client.read_diagnostics(); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap() }, + "position": { "line": 0, "character": 7 }, + }), + ); + assert_eq!( + res, + json!([{ + "targetUri": "deno:/http/localhost%3A4545/subdir/mod2.ts", + "targetRange": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 1, "character": 0 }, + }, + "targetSelectionRange": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 1, "character": 0 }, + }, + }]), + ); + client.shutdown(); +} + +#[test] +fn lsp_deno_json_workspace_vendor_dir() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + temp_dir.create_dir_all("project1/project2"); + temp_dir.write( + "project1/deno.json", + json!({ + "workspace": ["project2"], + "vendor": true, + }) + .to_string(), + ); + temp_dir.write("project1/project2/deno.json", json!({}).to_string()); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"http://localhost:4545/subdir/mod1.ts\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project1/project2/file.ts").unwrap()], + }), + ); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap(), + }, + "position": { "line": 0, "character": 7 }, + }), + ); + assert_eq!( + res, + json!([{ + "targetUri": temp_dir.uri().join("project1/vendor/http_localhost_4545/subdir/mod1.ts").unwrap(), + "targetRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 17, + "character": 0, + }, + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 17, + "character": 0, + }, + }, + }]), + ); + client.shutdown(); +} + +#[test] +fn lsp_deno_json_workspace_node_modules_dir() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + temp_dir.create_dir_all("project1/project2"); + temp_dir.write( + "project1/deno.json", + json!({ + "workspace": ["project2"], + "nodeModulesDir": true, + }) + .to_string(), + ); + temp_dir.write("project1/project2/deno.json", json!({}).to_string()); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": "import \"npm:@denotest/add@1\";\n", + }, + })); + client.write_request( + "workspace/executeCommand", + json!({ + "command": "deno.cache", + "arguments": [[], temp_dir.uri().join("project1/project2/file.ts").unwrap()], + }), + ); + let res = client.write_request( + "textDocument/definition", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("project1/project2/file.ts").unwrap(), + }, + "position": { "line": 0, "character": 7 }, + }), + ); + // The temp dir is symlinked in macos, and `node_modules` is canonicalized. + let canon_temp_dir = + Url::from_directory_path(temp_dir.path().canonicalize()).unwrap(); + assert_eq!( + res, + json!([{ + "targetUri": canon_temp_dir.join("project1/node_modules/.deno/@denotest+add@1.0.0/node_modules/@denotest/add/index.d.ts").unwrap(), + "targetRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 0, + }, + "end": { + "line": 1, + "character": 0, + }, + }, + }]), + ); + client.shutdown(); +} + #[test] fn lsp_deno_json_workspace_jsr_resolution() { let context = TestContextBuilder::new().use_temp_cwd().build(); -- cgit v1.2.3