mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Support --python=<symlink to executable>
(#18827)
## Summary Fixes https://github.com/astral-sh/ty/issues/640. If a user passes `--python=<some-virtual-environment>/bin/python`, we must avoid canonicalizing the path until we've traversed upwards to find the `sys.prefix` directory (`<some-virtual-environment>`). On Unix systems, `<sys.prefix>/bin/python` is often a symlink to a system interpreter; if we resolve the symlink too easily then we'll add the system interpreter's `site-packages` directory as a search path rather than the virtual environment's directory. ## Test Plan I added an integration test to `crates/ty/tests/cli/python_environment.rs` which fails on `main`. I also manually tested locally that running `cargo run -p ty check foo.py --python=.venv/bin/python -vv` now prints this log to the terminal ``` 2025-06-20 18:35:24.57702 DEBUG Resolved site-packages directories for this virtual environment are: SitePackagesPaths({"/Users/alexw/dev/ruff/.venv/lib/python3.13/site-packages"}) ``` Whereas it previously resolved `site-packages` to my system intallation's `site-packages` directory
This commit is contained in:
parent
f32ae94bc3
commit
f24e650dfd
3 changed files with 123 additions and 57 deletions
|
@ -292,6 +292,59 @@ fn python_version_inferred_from_system_installation() -> anyhow::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// On Unix systems, it's common for a Python installation at `.venv/bin/python` to only be a symlink
|
||||
/// to a system Python installation. We must be careful not to resolve the symlink too soon!
|
||||
/// If we do, we will incorrectly add the system installation's `site-packages` as a search path,
|
||||
/// when we should be adding the virtual environment's `site-packages` directory as a search path instead.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn python_argument_points_to_symlinked_executable() -> anyhow::Result<()> {
|
||||
let case = CliTest::with_files([
|
||||
(
|
||||
"system-installation/lib/python3.13/site-packages/foo.py",
|
||||
"",
|
||||
),
|
||||
("system-installation/bin/python", ""),
|
||||
(
|
||||
"strange-venv-location/lib/python3.13/site-packages/bar.py",
|
||||
"",
|
||||
),
|
||||
(
|
||||
"test.py",
|
||||
"\
|
||||
import foo
|
||||
import bar",
|
||||
),
|
||||
])?;
|
||||
|
||||
case.write_symlink(
|
||||
"system-installation/bin/python",
|
||||
"strange-venv-location/bin/python",
|
||||
)?;
|
||||
|
||||
assert_cmd_snapshot!(case.command().arg("--python").arg("strange-venv-location/bin/python"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Cannot resolve imported module `foo`
|
||||
--> test.py:1:8
|
||||
|
|
||||
1 | import foo
|
||||
| ^^^
|
||||
2 | import bar
|
||||
|
|
||||
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
|
||||
info: rule `unresolved-import` is enabled by default
|
||||
|
||||
Found 1 diagnostic
|
||||
|
||||
----- stderr -----
|
||||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pyvenv_cfg_file_annotation_showing_where_python_version_set() -> anyhow::Result<()> {
|
||||
let case = CliTest::with_files([
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue