Skip to content

Commit d581272

Browse files
authored
feat: add pet-hatch locator for Hatch environments (Fixes #450) (#460)
Fixes #450. Adds a new `pet-hatch` crate so Hatch-managed virtual environments are no longer misclassified as plain `Venv` by downstream consumers. ### Implementation Matches Hatch's actual storage layout from [`src/hatch/env/virtual.py`](https://github.com/pypa/hatch/blob/master/src/hatch/env/virtual.py): - **Default storage:** `<data_dir>/env/virtual/<project_name>/<project_id>/<venv_name>` (3 levels deep, not 2). - **`HATCH_DATA_DIR`** is honoured and never silently falls back to the platform default when set. - **Project-local discovery** via `[tool.hatch.dirs.env].virtual` in `pyproject.toml` or `[dirs.env]` in `hatch.toml`. Handles the `virtual = ".hatch"` example called out in the issue. - Hatch locator is inserted **before `Venv`** so it claims envs first. - `LocatorKind::Hatch` registered as `RefreshStatePersistence::ConfiguredOnly` (workspace-driven discovery). ### Tests 15 unit tests covering: - Default-storage layout matches at exactly 3 levels deep; rejects 2-deep and 4-deep prefixes. - `HATCH_DATA_DIR` semantics — used when set, no fallback to platform default. - Project-local discovery via both `pyproject.toml` and `hatch.toml`; rejected without `dirs.env.virtual` config. - Per-platform `platformdirs` defaults (Linux/macOS/Windows). ### Replaces #451 This PR replaces #451, which had a fundamental layout bug: it assumed envs are stored 2 levels deep under `<data_dir>/env/virtual` (`<project-hash>/<env-name>`). Per Hatch's source, the actual layout is 3 levels deep (`<project_name>/<project_id>/<venv_name>`), so #451 would fail to detect any real Hatch env in the default storage location.
1 parent 93285a8 commit d581272

8 files changed

Lines changed: 1556 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pet-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct Configuration {
4343
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
4444
pub enum LocatorKind {
4545
Conda,
46+
Hatch,
4647
Homebrew,
4748
LinuxGlobal,
4849
MacCommandLineTools,

crates/pet-core/src/python_environment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum PythonEnvironmentKind {
1919
PyenvVirtualEnv, // Pyenv virtualenvs.
2020
Pipenv,
2121
Poetry,
22+
Hatch,
2223
MacPythonOrg,
2324
MacCommandLineTools,
2425
LinuxGlobal,

crates/pet-hatch/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "pet-hatch"
3+
version.workspace = true
4+
edition.workspace = true
5+
license.workspace = true
6+
7+
[dependencies]
8+
pet-core = { path = "../pet-core" }
9+
pet-fs = { path = "../pet-fs" }
10+
pet-python-utils = { path = "../pet-python-utils" }
11+
serde = { version = "1.0.152", features = ["derive"] }
12+
toml = "0.9.7"
13+
log = "0.4.21"
14+
15+
[dev-dependencies]
16+
tempfile = "3.13"

0 commit comments

Comments
 (0)