ruff/crates
Alex Waygood a1eb834a5f
Fix relative import resolution in site-packages packages when the site-packages search path is a subdirectory of the first-party search path (#17178)
## Summary

If a package in `site-packages` had this directory structure:

```py
# bar/__init__.py
from .a import A

# bar/a.py
class A: ...
```

then we would fail to resolve the `from .a import A` import _if_ (as is
usually the case!) the `site-packages` search path was located inside a
`.venv` directory that was a subdirectory of the project's first-party
search path. The reason for this is a bug in `file_to_module` in the
module resolver. In this loop, we would identify that
`/project_root/.venv/lib/python3.13/site-packages/foo/__init__.py` can
be turned into a path relative to the first-party search path
(`/project_root`):


6e2b8f9696/crates/red_knot_python_semantic/src/module_resolver/resolver.rs (L101-L110)

but we'd then try to turn the relative path
(.venv/lib/python3.13/site-packages/foo/__init__.py`) into a module
path, realise that it wasn't a valid module path... and therefore
immediately `break` out of the loop before trying any other search paths
(such as the `site-packages` search path).

This bug was originally reported on Discord by @MatthewMckee4.

## Test Plan

I added a unit test for `file_to_module` in `resolver.rs`, and an
integration test that shows we can now resolve the import correctly in
`infer.rs`.
2025-04-03 15:48:05 +01:00
..
red_knot ruff_db: switch diagnostic rendering over to std::fmt::Display 2025-04-02 11:01:16 -04:00
red_knot_ide ruff_db: switch diagnostic rendering over to std::fmt::Display 2025-04-02 11:01:16 -04:00
red_knot_project red_knot: use Diagnostic inside of red knot 2025-04-02 10:10:01 -04:00
red_knot_python_semantic Fix relative import resolution in site-packages packages when the site-packages search path is a subdirectory of the first-party search path (#17178) 2025-04-03 15:48:05 +01:00
red_knot_server red_knot: use Diagnostic inside of red knot 2025-04-02 10:10:01 -04:00
red_knot_test [red-knot] Add GitHub PR annotations when mdtests fail in CI (#17150) 2025-04-02 21:51:52 +01:00
red_knot_vendored Sync vendored typeshed stubs (#17106) 2025-04-01 17:44:27 +01:00
red_knot_wasm ruff_db: switch diagnostic rendering over to std::fmt::Display 2025-04-02 11:01:16 -04:00
ruff Bump 0.11.3 (#17173) 2025-04-03 09:05:40 -04:00
ruff_annotate_snippets Update pre-commit dependencies (#17073) 2025-03-31 07:42:15 +00:00
ruff_benchmark red_knot: use Diagnostic inside of red knot 2025-04-02 10:10:01 -04:00
ruff_cache Fix cache key collisions for paths with separators (#12159) 2024-07-03 07:36:46 -05:00
ruff_db ruff_db: simplify lifetimes on DiagnosticDisplay 2025-04-02 12:47:02 -04:00
ruff_dev Pass ParserOptions to the parser (#16220) 2025-02-19 10:50:50 -05:00
ruff_diagnostics Show errors for attempted fixes only when passed --verbose (#15237) 2025-01-03 08:50:13 -06:00
ruff_formatter Fixing more spelling errors (#16926) 2025-03-23 10:55:14 -07:00
ruff_graph Pass ParserOptions to the parser (#16220) 2025-02-19 10:50:50 -05:00
ruff_index [red-knot] Don't use separate ID types for each alist (#16415) 2025-02-28 14:55:55 -05:00
ruff_linter [syntax-errors] Detect duplicate keys in match mapping patterns (#17129) 2025-04-03 10:22:37 -04:00
ruff_macros Add knot.toml schema (#15735) 2025-02-07 10:59:40 +01:00
ruff_notebook bump MSRV to 1.83 (#16294) 2025-02-26 06:12:43 -08:00
ruff_python_ast Visit Identifier node as part of the SourceOrderVisitor (#17110) 2025-04-01 16:58:09 +02:00
ruff_python_ast_integration_tests Visit Identifier node as part of the SourceOrderVisitor (#17110) 2025-04-01 16:58:09 +02:00
ruff_python_codegen Pass ParserOptions to the parser (#16220) 2025-02-19 10:50:50 -05:00
ruff_python_formatter Visit Identifier node as part of the SourceOrderVisitor (#17110) 2025-04-01 16:58:09 +02:00
ruff_python_index Extract LineIndex independent methods from Locator (#13938) 2024-10-28 07:53:41 +00:00
ruff_python_literal Preserve triple quotes and prefixes for strings (#15818) 2025-02-04 08:41:06 -05:00
ruff_python_parser [syntax-errors] Detect duplicate keys in match mapping patterns (#17129) 2025-04-03 10:22:37 -04:00
ruff_python_resolver Update pre-commit dependencies (#17073) 2025-03-31 07:42:15 +00:00
ruff_python_semantic Control flow: return and raise (#17121) 2025-04-03 08:30:29 -05:00
ruff_python_stdlib Revert "Add all PEP-585 names to UP006 rule" (#15250) 2025-01-04 12:23:53 +01:00
ruff_python_trivia [red-knot] Ignore surrounding whitespace when looking for <!-- snapshot-diagnostics --> directives in mdtests (#16380) 2025-02-27 13:25:31 +00:00
ruff_python_trivia_integration_tests Pass ParserOptions to the parser (#16220) 2025-02-19 10:50:50 -05:00
ruff_server Update pre-commit dependencies (#17073) 2025-03-31 07:42:15 +00:00
ruff_source_file [pyupgrade] Do not report when a UTF-8 comment is followed by a non-UTF-8 one (UP009) (#14728) 2024-12-11 10:30:41 +00:00
ruff_text_size [ruff] itertools.starmap(..., zip(...)) (RUF058) (#15483) 2025-01-16 15:18:12 +01:00
ruff_wasm Bump 0.11.3 (#17173) 2025-04-03 09:05:40 -04:00
ruff_workspace [flake8-import-conventions] Add import numpy.typing as npt to default flake8-import-conventions.aliases (#17133) 2025-04-02 09:25:46 +02:00