Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ error[unresolved-import]: Cannot resolve imported module `....foo`
2 |
3 | stat = add(10, 15)
|
help: The module can be resolved if the number of leading dots is reduced
help: Did you mean `...foo`?
info: Searched in the following paths during module resolution:
info: 1. /src (first-party code)
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
Expand Down
32 changes: 28 additions & 4 deletions crates/ty_python_semantic/src/types/infer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5799,9 +5799,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
return;
};
let mut diagnostic = builder.into_diagnostic(format_args!(
"Cannot resolve imported module `{}{}`",
".".repeat(level as usize),
module.unwrap_or_default()
"Cannot resolve imported module `{}`",
format_import_from_module(level, module)
));

if level == 0 {
Expand Down Expand Up @@ -5832,6 +5831,30 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
}
}
}
} else {
if let Some(better_level) = (0..level).rev().find(|reduced_level| {
let Ok(module_name) = ModuleName::from_identifier_parts(
self.db(),
self.file(),
module,
*reduced_level,
) else {
return false;
};
resolve_module(self.db(), &module_name).is_some()
}) {
diagnostic
.help("The module can be resolved if the number of leading dots is reduced");
diagnostic.help(format_args!(
"Did you mean `{}`?",
format_import_from_module(better_level, module)
));
diagnostic.set_concise_message(format_args!(
"Cannot resolve imported module `{}` - did you mean `{}`?",
format_import_from_module(level, module),
format_import_from_module(better_level, module)
));
}
}

// Add search paths information to the diagnostic
Expand Down Expand Up @@ -6039,7 +6062,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
}
Err(ModuleNameResolutionError::UnknownCurrentModule) => {
tracing::debug!(
"Relative module resolution `{}` failed; could not resolve file `{}` to a module",
"Relative module resolution `{}` failed: could not resolve file `{}` to a module \
(try adjusting configured search paths?)",
format_import_from_module(*level, module),
self.file().path(self.db())
);
Expand Down
Loading