mirror of https://github.com/astral-sh/ruff
Avoid walking past root when resolving imports (#6126)
## Summary Noticed in #5954: we walk _past_ the root rather than stopping _at_ the root when attempting to traverse along the parent path. It's effectively an off-by-one bug.
This commit is contained in:
parent
6e85e0010f
commit
0c2abf8316
|
|
@ -715,37 +715,28 @@ pub(crate) fn resolve_import<Host: host::Host>(
|
||||||
// importing file's directory, then the parent directory, and so on, until the
|
// importing file's directory, then the parent directory, and so on, until the
|
||||||
// import root is reached.
|
// import root is reached.
|
||||||
let root = execution_environment.root.as_path();
|
let root = execution_environment.root.as_path();
|
||||||
if source_file.starts_with(root) {
|
let mut current = source_file;
|
||||||
let mut current = source_file;
|
while let Some(parent) = current.parent() {
|
||||||
while let Some(parent) = current.parent() {
|
if !parent.starts_with(root) {
|
||||||
if parent == root {
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("Resolving absolute import in parent: {}", parent.display());
|
|
||||||
|
|
||||||
let mut result = resolve_absolute_import(
|
|
||||||
parent,
|
|
||||||
module_descriptor,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
if result.is_import_found {
|
|
||||||
if let Some(implicit_imports) = result
|
|
||||||
.implicit_imports
|
|
||||||
.filter(&module_descriptor.imported_symbols)
|
|
||||||
{
|
|
||||||
result.implicit_imports = implicit_imports;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
current = parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug!("Resolving absolute import in parent: {}", parent.display());
|
||||||
|
|
||||||
|
let mut result =
|
||||||
|
resolve_absolute_import(parent, module_descriptor, false, false, false, true, false);
|
||||||
|
|
||||||
|
if result.is_import_found {
|
||||||
|
if let Some(implicit_imports) = result
|
||||||
|
.implicit_imports
|
||||||
|
.filter(&module_descriptor.imported_symbols)
|
||||||
|
{
|
||||||
|
result.implicit_imports = implicit_imports;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImportResult::not_found()
|
ImportResult::not_found()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue