From 2f9992b6efa39ebbdcae006df1c93c9834297ec4 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 6 May 2025 12:37:10 +0100 Subject: [PATCH] [ty] Fix duplicate diagnostics for unresolved module when an `import from` statement imports multiple members (#17886) --- crates/ty/tests/cli.rs | 14 +++--- .../resources/mdtest/call/union.md | 2 +- .../resources/mdtest/import/basic.md | 18 +++++-- .../resources/mdtest/import/errors.md | 6 +-- .../resources/mdtest/import/star.md | 2 +- .../mdtest/mdtest_custom_typeshed.md | 2 +- .../resources/mdtest/mro.md | 1 - ...ts_imported_from_an_unresolved_module.snap | 30 ++++++++++++ ...ructures_-_Unresolvable_module_import.snap | 6 +-- ...ures_-_Unresolvable_submodule_imports.snap | 12 ++--- ...vable_import_that_does_not_use_`from`.snap | 2 +- ...`from`_with_an_unknown_current_module.snap | 2 +- ..._`from`_with_an_unknown_nested_module.snap | 2 +- ...ng_`from`_with_an_unresolvable_module.snap | 2 +- ...ing_`from`_with_too_many_leading_dots.snap | 2 +- crates/ty_python_semantic/src/types/infer.rs | 47 ++++++++----------- crates/ty_wasm/tests/api.rs | 5 +- 17 files changed, 95 insertions(+), 60 deletions(-) create mode 100644 crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imported_from_an_unresolved_module.snap diff --git a/crates/ty/tests/cli.rs b/crates/ty/tests/cli.rs index 51cf009ea9..78a07e8183 100644 --- a/crates/ty/tests/cli.rs +++ b/crates/ty/tests/cli.rs @@ -276,7 +276,7 @@ fn cli_arguments_are_relative_to_the_current_directory() -> anyhow::Result<()> { success: false exit_code: 1 ----- stdout ----- - error: lint:unresolved-import: Cannot resolve import `utils` + error: lint:unresolved-import: Cannot resolve imported module `utils` --> test.py:2:6 | 2 | from utils import add @@ -452,7 +452,7 @@ fn cli_rule_severity() -> anyhow::Result<()> { success: false exit_code: 1 ----- stdout ----- - error: lint:unresolved-import: Cannot resolve import `does_not_exit` + error: lint:unresolved-import: Cannot resolve imported module `does_not_exit` --> test.py:2:8 | 2 | import does_not_exit @@ -499,7 +499,7 @@ fn cli_rule_severity() -> anyhow::Result<()> { success: true exit_code: 0 ----- stdout ----- - warning: lint:unresolved-import: Cannot resolve import `does_not_exit` + warning: lint:unresolved-import: Cannot resolve imported module `does_not_exit` --> test.py:2:8 | 2 | import does_not_exit @@ -1053,7 +1053,7 @@ fn check_specific_paths() -> anyhow::Result<()> { | ^^^^^ | - error: lint:unresolved-import: Cannot resolve import `main2` + error: lint:unresolved-import: Cannot resolve imported module `main2` --> project/other.py:2:6 | 2 | from main2 import z # error: unresolved-import @@ -1062,7 +1062,7 @@ fn check_specific_paths() -> anyhow::Result<()> { 4 | print(z) | - error: lint:unresolved-import: Cannot resolve import `does_not_exist` + error: lint:unresolved-import: Cannot resolve imported module `does_not_exist` --> project/tests/test_main.py:2:8 | 2 | import does_not_exist # error: unresolved-import @@ -1083,7 +1083,7 @@ fn check_specific_paths() -> anyhow::Result<()> { success: false exit_code: 1 ----- stdout ----- - error: lint:unresolved-import: Cannot resolve import `main2` + error: lint:unresolved-import: Cannot resolve imported module `main2` --> project/other.py:2:6 | 2 | from main2 import z # error: unresolved-import @@ -1092,7 +1092,7 @@ fn check_specific_paths() -> anyhow::Result<()> { 4 | print(z) | - error: lint:unresolved-import: Cannot resolve import `does_not_exist` + error: lint:unresolved-import: Cannot resolve imported module `does_not_exist` --> project/tests/test_main.py:2:8 | 2 | import does_not_exist # error: unresolved-import diff --git a/crates/ty_python_semantic/resources/mdtest/call/union.md b/crates/ty_python_semantic/resources/mdtest/call/union.md index 8c34c83bb8..0b773fbb70 100644 --- a/crates/ty_python_semantic/resources/mdtest/call/union.md +++ b/crates/ty_python_semantic/resources/mdtest/call/union.md @@ -16,7 +16,7 @@ def _(flag: bool): ## Calling with an unknown union ```py -from nonexistent import f # error: [unresolved-import] "Cannot resolve import `nonexistent`" +from nonexistent import f # error: [unresolved-import] "Cannot resolve imported module `nonexistent`" def coinflip() -> bool: return True diff --git a/crates/ty_python_semantic/resources/mdtest/import/basic.md b/crates/ty_python_semantic/resources/mdtest/import/basic.md index 48fa5fe4bd..53fcd705ea 100644 --- a/crates/ty_python_semantic/resources/mdtest/import/basic.md +++ b/crates/ty_python_semantic/resources/mdtest/import/basic.md @@ -121,7 +121,7 @@ class C: ... ```py -import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`" +import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`" ``` ## Unresolvable submodule imports @@ -130,10 +130,10 @@ import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzq ```py # Topmost component resolvable, submodule not resolvable: -import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`" +import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`" # Topmost component unresolvable: -import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`" +import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`" ``` `a/__init__.py`: @@ -164,3 +164,15 @@ from AveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongP reveal_type(Foo()) # revealed: Foo ``` + +## Multiple objects imported from an unresolved module + + + +If multiple members are imported from a module that cannot be resolved, only a single diagnostic is +emitted for the `import from` statement: + +```py +# error: [unresolved-import] +from does_not_exist import foo, bar, baz +``` diff --git a/crates/ty_python_semantic/resources/mdtest/import/errors.md b/crates/ty_python_semantic/resources/mdtest/import/errors.md index 22e747d351..97716b5f18 100644 --- a/crates/ty_python_semantic/resources/mdtest/import/errors.md +++ b/crates/ty_python_semantic/resources/mdtest/import/errors.md @@ -3,7 +3,7 @@ ## Unresolved import statement ```py -import bar # error: "Cannot resolve import `bar`" +import bar # error: "Cannot resolve imported module `bar`" reveal_type(bar) # revealed: Unknown ``` @@ -11,7 +11,7 @@ reveal_type(bar) # revealed: Unknown ## Unresolved import from statement ```py -from bar import baz # error: "Cannot resolve import `bar`" +from bar import baz # error: "Cannot resolve imported module `bar`" reveal_type(baz) # revealed: Unknown ``` @@ -34,7 +34,7 @@ reveal_type(thing) # revealed: Unknown `a.py`: ```py -import foo as foo # error: "Cannot resolve import `foo`" +import foo as foo # error: "Cannot resolve imported module `foo`" reveal_type(foo) # revealed: Unknown ``` diff --git a/crates/ty_python_semantic/resources/mdtest/import/star.md b/crates/ty_python_semantic/resources/mdtest/import/star.md index 0b91b3d35f..87726190d8 100644 --- a/crates/ty_python_semantic/resources/mdtest/import/star.md +++ b/crates/ty_python_semantic/resources/mdtest/import/star.md @@ -1369,7 +1369,7 @@ If the module is unresolved, we emit a diagnostic just like for any other unreso ```py # TODO: not a great error message -from foo import * # error: [unresolved-import] "Cannot resolve import `foo`" +from foo import * # error: [unresolved-import] "Cannot resolve imported module `foo`" ``` ### Nested scope diff --git a/crates/ty_python_semantic/resources/mdtest/mdtest_custom_typeshed.md b/crates/ty_python_semantic/resources/mdtest/mdtest_custom_typeshed.md index 7c4a0abc3b..e436f401fb 100644 --- a/crates/ty_python_semantic/resources/mdtest/mdtest_custom_typeshed.md +++ b/crates/ty_python_semantic/resources/mdtest/mdtest_custom_typeshed.md @@ -81,7 +81,7 @@ new_module: 3.11- ```py from old_module import OldClass -# error: [unresolved-import] "Cannot resolve import `new_module`" +# error: [unresolved-import] "Cannot resolve imported module `new_module`" from new_module import NewClass ``` diff --git a/crates/ty_python_semantic/resources/mdtest/mro.md b/crates/ty_python_semantic/resources/mdtest/mro.md index 9d0a926469..5239849766 100644 --- a/crates/ty_python_semantic/resources/mdtest/mro.md +++ b/crates/ty_python_semantic/resources/mdtest/mro.md @@ -313,7 +313,6 @@ reveal_type(Omelette.__mro__) # revealed: tuple[Literal[Omelette], Unknown, Lit ```py # error: [unresolved-import] -# error: [unresolved-import] from does_not_exist import unknown_object_1, unknown_object_2 reveal_type(unknown_object_1) # revealed: Unknown diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imported_from_an_unresolved_module.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imported_from_an_unresolved_module.snap new file mode 100644 index 0000000000..db40afece3 --- /dev/null +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imported_from_an_unresolved_module.snap @@ -0,0 +1,30 @@ +--- +source: crates/ty_test/src/lib.rs +expression: snapshot +--- +--- +mdtest name: basic.md - Structures - Multiple objects imported from an unresolved module +mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md +--- + +# Python source files + +## mdtest_snippet.py + +``` +1 | # error: [unresolved-import] +2 | from does_not_exist import foo, bar, baz +``` + +# Diagnostics + +``` +error: lint:unresolved-import: Cannot resolve imported module `does_not_exist` + --> src/mdtest_snippet.py:2:6 + | +1 | # error: [unresolved-import] +2 | from does_not_exist import foo, bar, baz + | ^^^^^^^^^^^^^^ + | + +``` diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_import.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_import.snap index ac235bc237..98fc348906 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_import.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_import.snap @@ -12,16 +12,16 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md ## mdtest_snippet.py ``` -1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`" +1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`" ``` # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `zqzqzqzqzqzqzq` +error: lint:unresolved-import: Cannot resolve imported module `zqzqzqzqzqzqzq` --> src/mdtest_snippet.py:1:8 | -1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`" +1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`" | ^^^^^^^^^^^^^^ | diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodule_imports.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodule_imports.snap index b6f94e9f1f..22649b4348 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodule_imports.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodule_imports.snap @@ -13,10 +13,10 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md ``` 1 | # Topmost component resolvable, submodule not resolvable: -2 | import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`" +2 | import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`" 3 | 4 | # Topmost component unresolvable: -5 | import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`" +5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`" ``` ## a/__init__.py @@ -27,11 +27,11 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `a.foo` +error: lint:unresolved-import: Cannot resolve imported module `a.foo` --> src/mdtest_snippet.py:2:8 | 1 | # Topmost component resolvable, submodule not resolvable: -2 | import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`" +2 | import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`" | ^^^^^ 3 | 4 | # Topmost component unresolvable: @@ -40,11 +40,11 @@ error: lint:unresolved-import: Cannot resolve import `a.foo` ``` ``` -error: lint:unresolved-import: Cannot resolve import `b.foo` +error: lint:unresolved-import: Cannot resolve imported module `b.foo` --> src/mdtest_snippet.py:5:8 | 4 | # Topmost component unresolvable: -5 | import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`" +5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`" | ^^^^^ | diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_An_unresolvable_import_that_does_not_use_`from`.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_An_unresolvable_import_that_does_not_use_`from`.snap index efcb90ec6e..6db2dcda87 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_An_unresolvable_import_that_does_not_use_`from`.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_An_unresolvable_import_that_does_not_use_`from`.snap @@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `does_not_exist` +error: lint:unresolved-import: Cannot resolve imported module `does_not_exist` --> src/mdtest_snippet.py:1:8 | 1 | import does_not_exist # error: [unresolved-import] diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_current_module.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_current_module.snap index 090cba8056..e603954a36 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_current_module.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_current_module.snap @@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `.does_not_exist` +error: lint:unresolved-import: Cannot resolve imported module `.does_not_exist` --> src/mdtest_snippet.py:1:7 | 1 | from .does_not_exist import add # error: [unresolved-import] diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_nested_module.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_nested_module.snap index 0123ff0edf..64cf88f310 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_nested_module.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unknown_nested_module.snap @@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `.does_not_exist.foo.bar` +error: lint:unresolved-import: Cannot resolve imported module `.does_not_exist.foo.bar` --> src/mdtest_snippet.py:1:7 | 1 | from .does_not_exist.foo.bar import add # error: [unresolved-import] diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unresolvable_module.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unresolvable_module.snap index 999a1379a9..f92f814de3 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unresolvable_module.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_an_unresolvable_module.snap @@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `does_not_exist` +error: lint:unresolved-import: Cannot resolve imported module `does_not_exist` --> src/mdtest_snippet.py:1:6 | 1 | from does_not_exist import add # error: [unresolved-import] diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_too_many_leading_dots.snap b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_too_many_leading_dots.snap index 4b7e84df00..4ace7f3b60 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_too_many_leading_dots.snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_diagnostics_-_Using_`from`_with_too_many_leading_dots.snap @@ -32,7 +32,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i # Diagnostics ``` -error: lint:unresolved-import: Cannot resolve import `....foo` +error: lint:unresolved-import: Cannot resolve imported module `....foo` --> src/package/subpackage/subsubpackage/__init__.py:1:10 | 1 | from ....foo import add # error: [unresolved-import] diff --git a/crates/ty_python_semantic/src/types/infer.rs b/crates/ty_python_semantic/src/types/infer.rs index 2f8ed6bcfb..72b31ada1c 100644 --- a/crates/ty_python_semantic/src/types/infer.rs +++ b/crates/ty_python_semantic/src/types/infer.rs @@ -3612,7 +3612,7 @@ impl<'db> TypeInferenceBuilder<'db> { return; }; builder.into_diagnostic(format_args!( - "Cannot resolve import `{}{}`", + "Cannot resolve imported module `{}{}`", ".".repeat(level as usize), module.unwrap_or_default() )); @@ -3680,18 +3680,11 @@ impl<'db> TypeInferenceBuilder<'db> { level: _, } = import; + self.check_import_from_module_is_resolvable(import); + for alias in names { - let definitions = self.index.definitions(alias); - if definitions.is_empty() { - // If the module couldn't be resolved while constructing the semantic index, - // this node won't have any definitions associated with it -- but we need to - // make sure that we still emit the diagnostic for the unresolvable module, - // since this will cause the import to fail at runtime. - self.resolve_import_from_module(import, alias); - } else { - for definition in definitions { - self.extend(infer_definition_types(self.db(), *definition)); - } + for definition in self.index.definitions(alias) { + self.extend(infer_definition_types(self.db(), *definition)); } } } @@ -3746,11 +3739,7 @@ impl<'db> TypeInferenceBuilder<'db> { /// Resolve the [`ModuleName`], and the type of the module, being referred to by an /// [`ast::StmtImportFrom`] node. Emit a diagnostic if the module cannot be resolved. - fn resolve_import_from_module( - &mut self, - import_from: &ast::StmtImportFrom, - alias: &ast::Alias, - ) -> Option<(ModuleName, Type<'db>)> { + fn check_import_from_module_is_resolvable(&mut self, import_from: &ast::StmtImportFrom) { let ast::StmtImportFrom { module, level, .. } = import_from; // For diagnostics, we want to highlight the unresolvable @@ -3762,8 +3751,7 @@ impl<'db> TypeInferenceBuilder<'db> { let module = module.as_deref(); tracing::trace!( - "Resolving imported object `{}` from module `{}` into file `{}`", - alias.name, + "Resolving import statement from module `{}` into file `{}`", format_import_from_module(*level, module), self.file().path(self.db()), ); @@ -3774,7 +3762,7 @@ impl<'db> TypeInferenceBuilder<'db> { Err(ModuleNameResolutionError::InvalidSyntax) => { tracing::debug!("Failed to resolve import due to invalid syntax"); // Invalid syntax diagnostics are emitted elsewhere. - return None; + return; } Err(ModuleNameResolutionError::TooManyDots) => { tracing::debug!( @@ -3787,7 +3775,7 @@ impl<'db> TypeInferenceBuilder<'db> { *level, module, ); - return None; + return; } Err(ModuleNameResolutionError::UnknownCurrentModule) => { tracing::debug!( @@ -3801,16 +3789,13 @@ impl<'db> TypeInferenceBuilder<'db> { *level, module, ); - return None; + return; } }; - let Some(module_ty) = self.module_type_from_name(&module_name) else { + if resolve_module(self.db(), &module_name).is_none() { self.report_unresolved_import(import_from.into(), module_ref.range(), *level, module); - return None; - }; - - Some((module_name, module_ty)) + } } fn infer_import_from_definition( @@ -3819,12 +3804,18 @@ impl<'db> TypeInferenceBuilder<'db> { alias: &ast::Alias, definition: Definition<'db>, ) { - let Some((module_name, module_ty)) = self.resolve_import_from_module(import_from, alias) + let Ok(module_name) = + ModuleName::from_import_statement(self.db(), self.file(), import_from) else { self.add_unknown_declaration_with_binding(alias.into(), definition); return; }; + let Some(module_ty) = self.module_type_from_name(&module_name) else { + self.add_unknown_declaration_with_binding(alias.into(), definition); + return; + }; + // The indirection of having `star_import_info` as a separate variable // is required in order to make the borrow checker happy. let star_import_info = definition diff --git a/crates/ty_wasm/tests/api.rs b/crates/ty_wasm/tests/api.rs index e58d0871c8..1d46fe8e38 100644 --- a/crates/ty_wasm/tests/api.rs +++ b/crates/ty_wasm/tests/api.rs @@ -27,5 +27,8 @@ fn check() { diagnostic.to_range(&workspace).unwrap().start, Position { line: 1, column: 8 } ); - assert_eq!(diagnostic.message(), "Cannot resolve import `random22`"); + assert_eq!( + diagnostic.message(), + "Cannot resolve imported module `random22`" + ); }