Skip to content

Implement UV virtual environment support in Python Environment Tools #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 16, 2025

This PR adds support for discovering virtual environments created by UV within the Python Environment Tools (PET) codebase.

Overview

UV is a fast Python package installer and resolver that creates virtual environments in cache directories. This implementation follows the existing architecture by creating a new pet-uv crate that discovers UV cache directories and enumerates virtual environments, treating them as standard virtual environments without introducing new environment types.

Changes Made

1. New pet-uv Crate

  • Created crates/pet-uv/ with UV-specific environment discovery logic
  • Implements functions to locate UV cache directories following UV's priority order:
    1. UV_CACHE_DIR environment variable
    2. XDG cache directories on Unix / %LOCALAPPDATA% on Windows
    3. Platform-specific cache directories (~/.cache/uv, ~/Library/Caches/uv, etc.)
  • Enumerates virtual environments from {cache_dir}/environments-v2/ subdirectories
  • Filters out conda environments to avoid conflicts using existing is_conda_env() helper

2. Integration with Existing Discovery

  • Modified crates/pet/src/find.rs to include UV paths in global virtual environment search
  • Added UV path discovery alongside existing global virtual environment paths
  • UV environments are automatically identified as VirtualEnv type by existing locators

3. Cross-Platform Support

  • Handles Windows (%LOCALAPPDATA%\uv)
  • Handles macOS (~/Library/Caches/uv)
  • Handles Linux and other Unix systems (~/.cache/uv)
  • Supports XDG Base Directory specification

4. Testing

  • Added comprehensive unit tests for UV cache directory discovery
  • Added integration tests to verify end-to-end functionality
  • All existing tests continue to pass

Example Usage

With UV environments in the cache directory, PET now automatically discovers them:

# Set UV cache directory (optional - PET discovers default locations)
export UV_CACHE_DIR=/path/to/uv/cache

# Discover environments including UV virtual environments
pet find

# Output will include UV environments classified as VirtualEnv type:
# Environment (VirtualEnv)
#    Executable  : /path/to/uv/cache/environments-v2/myproject-abc123/bin/python
#    Symlinks    : "/path/to/uv/cache/environments-v2/myproject-abc123/bin/python"

Architecture Benefits

  • Minimal Changes: Leverages existing virtual environment identification logic
  • No New Environment Types: UV environments are treated as standard VirtualEnv type
  • Modular Design: UV-specific logic is isolated in its own crate
  • Performance: UV discovery happens during existing global virtual environment search phase
  • Backward Compatibility: No impact on existing environment discovery

Testing

Verified that UV environments are properly discovered and identified:

  • Unit tests for UV cache directory discovery functions
  • Integration tests for end-to-end environment discovery
  • Manual testing with real UV cache directory structures
  • All existing CI tests continue to pass

This implementation follows the established patterns in the codebase and maintains clean separation of concerns while adding robust UV virtual environment support.

Created from VS Code via the GitHub Pull Request extension.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Implementation Plan for UV Virtual Environment Support in Python Environment Tools Implement UV virtual environment support in Python Environment Tools Jul 16, 2025
@Copilot Copilot AI requested a review from DonJayamanne July 16, 2025 00:27
Copilot finished work on behalf of DonJayamanne July 16, 2025 00:27
Base automatically changed from don/random-galliform to main July 16, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants