Skip to content

Commit 73720c7

Browse files
RenkaiAlexWaygood
andauthored
[ty] Add search paths info to unresolved import diagnostics (#20040)
Fixes astral-sh/ty#457 --------- Co-authored-by: Alex Waygood <[email protected]>
1 parent 136abac commit 73720c7

11 files changed

+111
-7
lines changed

crates/ty/tests/cli/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ fn cli_arguments_are_relative_to_the_current_directory() -> anyhow::Result<()> {
263263
3 |
264264
4 | stat = add(10, 15)
265265
|
266+
info: Searched in the following paths during module resolution:
267+
info: 1. <temp_dir>/ (first-party code)
268+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
266269
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
267270
info: rule `unresolved-import` is enabled by default
268271
@@ -477,7 +480,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
477480

478481
assert_cmd_snapshot!(
479482
case.command(),
480-
@r###"
483+
@r"
481484
success: false
482485
exit_code: 1
483486
----- stdout -----
@@ -489,6 +492,9 @@ fn check_specific_paths() -> anyhow::Result<()> {
489492
3 |
490493
4 | print(z)
491494
|
495+
info: Searched in the following paths during module resolution:
496+
info: 1. <temp_dir>/ (first-party code)
497+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
492498
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
493499
info: rule `unresolved-import` is enabled by default
494500
@@ -498,14 +504,17 @@ fn check_specific_paths() -> anyhow::Result<()> {
498504
2 | import does_not_exist # error: unresolved-import
499505
| ^^^^^^^^^^^^^^
500506
|
507+
info: Searched in the following paths during module resolution:
508+
info: 1. <temp_dir>/ (first-party code)
509+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
501510
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
502511
info: rule `unresolved-import` is enabled by default
503512
504513
Found 2 diagnostics
505514
506515
----- stderr -----
507516
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
508-
"###
517+
"
509518
);
510519

511520
// Now check only the `tests` and `other.py` files.
@@ -524,6 +533,9 @@ fn check_specific_paths() -> anyhow::Result<()> {
524533
3 |
525534
4 | print(z)
526535
|
536+
info: Searched in the following paths during module resolution:
537+
info: 1. <temp_dir>/ (first-party code)
538+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
527539
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
528540
info: rule `unresolved-import` is enabled by default
529541
@@ -533,6 +545,9 @@ fn check_specific_paths() -> anyhow::Result<()> {
533545
2 | import does_not_exist # error: unresolved-import
534546
| ^^^^^^^^^^^^^^
535547
|
548+
info: Searched in the following paths during module resolution:
549+
info: 1. <temp_dir>/ (first-party code)
550+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
536551
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
537552
info: rule `unresolved-import` is enabled by default
538553

crates/ty/tests/cli/python_environment.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ import bar",
333333
| ^^^
334334
2 | import bar
335335
|
336+
info: Searched in the following paths during module resolution:
337+
info: 1. <temp_dir>/ (first-party code)
338+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
339+
info: 3. <temp_dir>/strange-venv-location/lib/python3.13/site-packages (site-packages)
336340
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
337341
info: rule `unresolved-import` is enabled by default
338342
@@ -378,6 +382,11 @@ fn lib64_site_packages_directory_on_unix() -> anyhow::Result<()> {
378382
1 | import foo, bar, baz
379383
| ^^^
380384
|
385+
info: Searched in the following paths during module resolution:
386+
info: 1. <temp_dir>/ (first-party code)
387+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
388+
info: 3. <temp_dir>/.venv/lib/python3.13/site-packages (site-packages)
389+
info: 4. <temp_dir>/.venv/lib64/python3.13/site-packages (site-packages)
381390
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
382391
info: rule `unresolved-import` is enabled by default
383392
@@ -1273,6 +1282,9 @@ home = ./
12731282
3 | from package1 import ChildConda
12741283
4 | from package1 import WorkingVenv
12751284
|
1285+
info: Searched in the following paths during module resolution:
1286+
info: 1. <temp_dir>/project (first-party code)
1287+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
12761288
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
12771289
info: rule `unresolved-import` is enabled by default
12781290
@@ -1285,6 +1297,9 @@ home = ./
12851297
4 | from package1 import WorkingVenv
12861298
5 | from package1 import BaseConda
12871299
|
1300+
info: Searched in the following paths during module resolution:
1301+
info: 1. <temp_dir>/project (first-party code)
1302+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
12881303
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
12891304
info: rule `unresolved-import` is enabled by default
12901305
@@ -1297,6 +1312,9 @@ home = ./
12971312
| ^^^^^^^^
12981313
5 | from package1 import BaseConda
12991314
|
1315+
info: Searched in the following paths during module resolution:
1316+
info: 1. <temp_dir>/project (first-party code)
1317+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
13001318
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
13011319
info: rule `unresolved-import` is enabled by default
13021320
@@ -1308,6 +1326,9 @@ home = ./
13081326
5 | from package1 import BaseConda
13091327
| ^^^^^^^^
13101328
|
1329+
info: Searched in the following paths during module resolution:
1330+
info: 1. <temp_dir>/project (first-party code)
1331+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
13111332
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
13121333
info: rule `unresolved-import` is enabled by default
13131334
@@ -1716,6 +1737,10 @@ fn default_root_tests_package() -> anyhow::Result<()> {
17161737
4 |
17171738
5 | print(f"{foo} {bar}")
17181739
|
1740+
info: Searched in the following paths during module resolution:
1741+
info: 1. <temp_dir>/ (first-party code)
1742+
info: 2. <temp_dir>/src (first-party code)
1743+
info: 3. vendored://stdlib (stdlib typeshed stubs vendored by ty)
17191744
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
17201745
info: rule `unresolved-import` is enabled by default
17211746

crates/ty/tests/cli/rule_selection.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
8989

9090
// Assert that there's an `unresolved-reference` diagnostic (error)
9191
// and an unresolved-import (error) diagnostic by default.
92-
assert_cmd_snapshot!(case.command(), @r###"
92+
assert_cmd_snapshot!(case.command(), @r"
9393
success: false
9494
exit_code: 1
9595
----- stdout -----
@@ -101,6 +101,9 @@ fn cli_rule_severity() -> anyhow::Result<()> {
101101
3 |
102102
4 | y = 4 / 0
103103
|
104+
info: Searched in the following paths during module resolution:
105+
info: 1. <temp_dir>/ (first-party code)
106+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
104107
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
105108
info: rule `unresolved-import` is enabled by default
106109
@@ -118,7 +121,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
118121
119122
----- stderr -----
120123
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
121-
"###);
124+
");
122125

123126
assert_cmd_snapshot!(
124127
case
@@ -141,6 +144,9 @@ fn cli_rule_severity() -> anyhow::Result<()> {
141144
3 |
142145
4 | y = 4 / 0
143146
|
147+
info: Searched in the following paths during module resolution:
148+
info: 1. <temp_dir>/ (first-party code)
149+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
144150
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
145151
info: rule `unresolved-import` was selected on the command line
146152

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imp…_(cbfbf5ff94e6e104).snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2626
2 | from does_not_exist import foo, bar, baz
2727
| ^^^^^^^^^^^^^^
2828
|
29+
info: Searched in the following paths during module resolution:
30+
info: 1. /src (first-party code)
31+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
2932
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
3033
info: rule `unresolved-import` is enabled by default
3134

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_…_(846453deaca1071c).snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ error[unresolved-import]: Cannot resolve imported module `zqzqzqzqzqzqzq`
2424
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
2525
| ^^^^^^^^^^^^^^
2626
|
27+
info: Searched in the following paths during module resolution:
28+
info: 1. /src (first-party code)
29+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
2730
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
2831
info: rule `unresolved-import` is enabled by default
2932

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodu…_(4fad4be9778578b7).snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ error[unresolved-import]: Cannot resolve imported module `a.foo`
3636
3 |
3737
4 | # Topmost component unresolvable:
3838
|
39+
info: Searched in the following paths during module resolution:
40+
info: 1. /src (first-party code)
41+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
3942
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
4043
info: rule `unresolved-import` is enabled by default
4144
@@ -49,6 +52,9 @@ error[unresolved-import]: Cannot resolve imported module `b.foo`
4952
5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
5053
| ^^^^^
5154
|
55+
info: Searched in the following paths during module resolution:
56+
info: 1. /src (first-party code)
57+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
5258
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
5359
info: rule `unresolved-import` is enabled by default
5460

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_An_unresolvable_impo…_(72d090df51ea97b8).snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2828
2 |
2929
3 | x = does_not_exist.foo
3030
|
31+
info: Searched in the following paths during module resolution:
32+
info: 1. /src (first-party code)
33+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
3134
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
3235
info: rule `unresolved-import` is enabled by default
3336

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_Using_`from`_with_an…_(9fa713dfa17cc404).snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2828
2 |
2929
3 | stat = add(10, 15)
3030
|
31+
info: Searched in the following paths during module resolution:
32+
info: 1. /src (first-party code)
33+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
3134
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
3235
info: rule `unresolved-import` is enabled by default
3336

crates/ty_python_semantic/src/module_resolver/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ pub use resolver::{resolve_module, resolve_real_module};
1010
use ruff_db::system::SystemPath;
1111

1212
use crate::Db;
13-
use crate::module_resolver::resolver::{ModuleResolveMode, search_paths};
14-
use resolver::SearchPathIterator;
13+
pub(crate) use resolver::{ModuleResolveMode, SearchPathIterator, search_paths};
1514

1615
mod list;
1716
mod module;

crates/ty_python_semantic/src/module_resolver/path.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,25 @@ impl SearchPath {
700700
SearchPathInner::StandardLibraryVendored(_) => "std-vendored",
701701
}
702702
}
703+
704+
/// Returns a string suitable for describing what kind of search path this is
705+
/// in user-facing diagnostics.
706+
#[must_use]
707+
pub(crate) fn describe_kind(&self) -> &'static str {
708+
match *self.0 {
709+
SearchPathInner::Extra(_) => {
710+
"extra search path specified on the CLI or in your config file"
711+
}
712+
SearchPathInner::FirstParty(_) => "first-party code",
713+
SearchPathInner::StandardLibraryCustom(_) => {
714+
"custom stdlib stubs specified on the CLI or in your config file"
715+
}
716+
SearchPathInner::StandardLibraryReal(_) => "runtime stdlib source code",
717+
SearchPathInner::SitePackages(_) => "site-packages",
718+
SearchPathInner::Editable(_) => "editable install",
719+
SearchPathInner::StandardLibraryVendored(_) => "stdlib typeshed stubs vendored by ty",
720+
}
721+
}
703722
}
704723

705724
impl PartialEq<SystemPath> for SearchPath {

0 commit comments

Comments
 (0)