ruff/crates/ty
Alex Waygood f24e650dfd
[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
2025-06-21 20:28:47 +01:00
..
docs [ty] Homogeneous and mixed tuples (#18600) 2025-06-20 18:23:54 -04:00
src [ty] Anchor all exclude patterns (#18685) 2025-06-18 08:57:36 +00:00
tests [ty] Support --python=<symlink to executable> (#18827) 2025-06-21 20:28:47 +01:00
build.rs Parse dist-workspace.toml for version (#17868) 2025-05-06 12:18:17 +00:00
Cargo.toml [ty] Infer the Python version from the environment if feasible (#18057) 2025-05-30 21:22:51 +00:00
CONTRIBUTING.md [ty] contribution guide (#18061) 2025-05-13 10:55:01 +02:00
README.md [ty] contribution guide (#18061) 2025-05-13 10:55:01 +02:00

ty

ty is an extremely fast type checker. Currently, it is a work-in-progress and not ready for production use.

The Rust code for ty lives in this repository; see CONTRIBUTING.md for more information on contributing to ty.

See the ty repo for ty documentation and releases.