feat: add pet-hatch locator for Hatch environments (Fixes #450)#460
Open
karthiknadig wants to merge 3 commits intomainfrom
Open
feat: add pet-hatch locator for Hatch environments (Fixes #450)#460karthiknadig wants to merge 3 commits intomainfrom
karthiknadig wants to merge 3 commits intomainfrom
Conversation
Adds a new pet-hatch crate that detects Hatch-managed virtual environments so they are no longer misclassified as plain Venv by downstream consumers. Implementation matches Hatch's actual storage layout from src/hatch/env/virtual.py: - Default storage: <data_dir>/env/virtual/<project_name>/<project_id>/<venv_name> (3 levels deep) - HATCH_DATA_DIR env var honoured; never silently falls back to platform default when set \ example from the issue) - Locator inserted before Venv so Hatch claims its envs first - 15 unit tests covering layout depth (rejects 2 / 4 levels), HATCH_DATA_DIR semantics, project-local config, and platform defaults
Performance Report (Linux) ➖
Legend
|
Test Coverage Report (Linux)
Coverage increased! Great work! |
Performance Report (Windows) ➖
Legend
|
Test Coverage Report (Windows)
Coverage increased! Great work! |
Performance Report (macOS)
Legend
|
There was a problem hiding this comment.
Pull request overview
This PR adds first-class Hatch environment detection to PET by introducing a new pet-hatch locator and wiring it into the existing locator chain so Hatch-managed venvs are classified as Hatch rather than the generic Venv.
Changes:
- Add new
pet-hatchcrate implementing aHatchlocator with default-storage and workspace-configured discovery/identification. - Register
LocatorKind::HatchandPythonEnvironmentKind::Hatch, and insert Hatch into the locator chain beforeVenv. - Update refresh-state contract expectations to mark Hatch as
RefreshStatePersistence::ConfiguredOnly.
Show a summary per file
| File | Description |
|---|---|
| crates/pet/src/locators.rs | Adds the Hatch locator into the ordered locator chain before Venv. |
| crates/pet/src/jsonrpc.rs | Updates the refresh-state contract test to include LocatorKind::Hatch. |
| crates/pet/Cargo.toml | Adds pet-hatch as a dependency of the main pet crate. |
| crates/pet-hatch/src/lib.rs | Implements Hatch detection (default storage + workspace-configured) and adds unit tests. |
| crates/pet-hatch/Cargo.toml | Defines the new pet-hatch crate and its dependencies. |
| crates/pet-core/src/python_environment.rs | Adds PythonEnvironmentKind::Hatch. |
| crates/pet-core/src/lib.rs | Adds LocatorKind::Hatch. |
| Cargo.lock | Records the new pet-hatch crate and dependency graph updates. |
Copilot's findings
- Files reviewed: 7/8 changed files
- Comments generated: 2
- Cache resolved virtual dirs in configure() so try_from() does not re-parse pyproject.toml / hatch.toml on every executable identification attempt. - Expand ~ (and \C:\Users\kanadig/\) in configured dirs.env.virtual values via pet_fs::path::expand_path so values like '~/.virtualenvs' resolve against the user home. - Build the new cache outside the workspace_virtual_dirs lock to keep disk I/O out of the critical section. - Serialize env-var-mutating tests via a per-binary mutex so cargo's default multi-threaded harness cannot race.
dmitrivMS
previously approved these changes
May 8, 2026
A Hatch project can configure dirs.env.virtual = '~/.virtualenvs' (or any path that overlaps with WORKON_HOME). With Hatch placed after VirtualEnvWrapper, those envs would be claimed as VirtualEnvWrapper before Hatch ever saw them. Move Hatch ahead so it gets first claim on workspace-configured envs.
dmitrivMS
approved these changes
May 8, 2026
Comment on lines
+217
to
+239
| /// Returns `None` if the resulting directory does not exist on disk. | ||
| fn get_default_virtual_dir(environment: &dyn Environment) -> Option<PathBuf> { | ||
| // If HATCH_DATA_DIR is set and non-empty, Hatch *only* uses that location — | ||
| // it never falls back to the platform default. Mirror that behaviour: return | ||
| // the env/virtual subdir when it exists on disk, otherwise None. Do not | ||
| // fall through to platform defaults, or we'd risk attributing platform- | ||
| // default envs to Hatch when the user has redirected Hatch elsewhere. | ||
| if let Some(custom) = environment.get_env_var("HATCH_DATA_DIR".to_string()) { | ||
| if !custom.is_empty() { | ||
| let path = append_virtual_subdir(PathBuf::from(custom)); | ||
| return if path.is_dir() { | ||
| Some(norm_case(path)) | ||
| } else { | ||
| None | ||
| }; | ||
| } | ||
| } | ||
| let path = append_virtual_subdir(platform_default_data_dir(environment)?); | ||
| if path.is_dir() { | ||
| Some(norm_case(path)) | ||
| } else { | ||
| None | ||
| } |
Comment on lines
+131
to
+138
| // A pyvenv.cfg must be present — Hatch envs are always venvs. | ||
| let cfg = PyVenvCfg::find(&prefix)?; | ||
|
|
||
| // Case 1: prefix lives in the default `<data_dir>/env/virtual` storage, | ||
| // exactly three components deep: | ||
| // <storage>/<project_name>/<project_id>/<venv_name> | ||
| if let Some(storage) = self.default_virtual_dir.as_deref() { | ||
| if let Some(env_name) = match_default_storage_layout(&prefix, storage) { |
Comment on lines
+150
to
+175
| let workspaces = self | ||
| .workspace_virtual_dirs | ||
| .lock() | ||
| .expect("workspace_virtual_dirs mutex poisoned") | ||
| .clone(); | ||
| for (workspace, virtual_dirs) in &workspaces { | ||
| for virtual_dir in virtual_dirs { | ||
| if prefix_is_directly_under(&prefix, virtual_dir) { | ||
| let env_name = prefix | ||
| .file_name() | ||
| .map(|n| n.to_string_lossy().to_string()) | ||
| .unwrap_or_default(); | ||
| trace!( | ||
| "Hatch env (project-local) {} found at {}", | ||
| env_name, | ||
| env.executable.display() | ||
| ); | ||
| return Some(build_env( | ||
| &prefix, | ||
| &cfg, | ||
| env_name, | ||
| Some(workspace.clone()), | ||
| &env.executable, | ||
| )); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #450.
Adds a new
pet-hatchcrate so Hatch-managed virtual environments are no longer misclassified as plainVenvby downstream consumers.Implementation
Matches Hatch's actual storage layout from
src/hatch/env/virtual.py:<data_dir>/env/virtual/<project_name>/<project_id>/<venv_name>(3 levels deep, not 2).HATCH_DATA_DIRis honoured and never silently falls back to the platform default when set.[tool.hatch.dirs.env].virtualinpyproject.tomlor[dirs.env]inhatch.toml. Handles thevirtual = ".hatch"example called out in the issue.Venvso it claims envs first.LocatorKind::Hatchregistered asRefreshStatePersistence::ConfiguredOnly(workspace-driven discovery).Tests
15 unit tests covering:
HATCH_DATA_DIRsemantics — used when set, no fallback to platform default.pyproject.tomlandhatch.toml; rejected withoutdirs.env.virtualconfig.platformdirsdefaults (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.