From 3b59689df6dd1c230cb26be07c51b79e56f95042 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 17 Jul 2025 19:09:16 -0400 Subject: [PATCH] fix(lsp): do not error for bytes or text import of json module (#30137) Closes https://github.com/denoland/deno/issues/30101 --- cli/lsp/diagnostics.rs | 2 +- tests/integration/lsp_tests.rs | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 5f904062c9..060622adc2 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -1576,7 +1576,7 @@ fn diagnose_resolution( if module.media_type == MediaType::Json { match maybe_assert_type { // The module has the correct assertion type, no diagnostic - Some("json") => (), + Some("json" | "text" | "bytes") => (), // The dynamic import statement is missing an attribute type, which // we might not be able to statically detect, therefore we will // not provide a potentially incorrect diagnostic. diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 6f3ef93adf..c26507c916 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -18902,3 +18902,54 @@ fn do_not_auto_import_from_definitely_typed() { client.shutdown(); } } + +#[test] +#[timeout(300_000)] +fn lsp_import_json_module_import_attribute_variations() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir().path(); + temp_dir.join("deno.json").write_json(&json!({ + "unstable": ["raw-imports"] + })); + temp_dir.join("file.json").write_json(&json!({})); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + for type_text in ["bytes", "text", "json"] { + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": temp_dir.join(format!("{}.ts", type_text)).uri_file(), + "languageId": "typescript", + "version": 1, + "text": format!("import data from './file.json' with {{ type: '{}' }};\nconsole.log(data);\n", type_text) + } + })); + assert_eq!(json!(diagnostics.all()), json!([])); + } + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": temp_dir.join("missing.ts").uri_file(), + "languageId": "typescript", + "version": 1, + "text": "import data from './file.json';\nconsole.log(data);\n", + } + })); + assert_eq!( + json!(diagnostics.all()), + json!([{ + "range": { + "start": { + "line": 0, + "character": 17, + }, + "end": { + "line": 0, + "character": 30, + }, + }, + "severity": 1, + "code": "no-attribute-type", + "source": "deno", + "message": "The module is a JSON module and not being imported with an import attribute. Consider adding `with { type: \"json\" }` to the import statement." + }]) + ); +}