Skip to content

pyenv-win version detection fails when PYENV/PYENV_ROOT env vars not set #316

@karthiknadig

Description

@karthiknadig

Bug Description

On Windows, when pyenv-win is installed at the default home location (~/.pyenv/pyenv-win/) but the PYENV_ROOT or PYENV environment variables are not set, the version detection in get_pyenv_manager_version() fails because it only uses get_pyenv_dir(environment) which requires those env vars to be present.

Location

crates/pet-pyenv/src/manager.rs:

#[cfg(windows)]
fn get_pyenv_manager_version(
    _pyenv_binary_path: &Path,
    environment: &EnvVariables,
) -> Option<String> {
    // In windows, the version is stored in the `.pyenv/.version` file

    let pyenv_dir = get_pyenv_dir(environment)?;  // <-- Returns None if env vars not set
    let mut version_file = pyenv_dir.join(".version");
    // ...
}

The function get_pyenv_dir() in environment_locations.rs:

pub fn get_pyenv_dir(env_vars: &EnvVariables) -> Option<PathBuf> {
    match &env_vars.pyenv_root {
        Some(dir) => Some(PathBuf::from(dir)),
        None => env_vars.pyenv.as_ref().map(PathBuf::from),  // Returns None if neither env var is set
    }
}

Expected Behavior

If the env vars are not set, the version detection should fall back to checking the home-based default path ~/.pyenv/.version.

Suggested Fix

#[cfg(windows)]
fn get_pyenv_manager_version(
    _pyenv_binary_path: &Path,
    environment: &EnvVariables,
) -> Option<String> {
    // In windows, the version is stored in the `.pyenv/.version` file
    
    // Try env var path first, then fall back to home directory
    let pyenv_dir = get_pyenv_dir(environment)
        .or_else(|| get_home_pyenv_dir(environment)?.parent().map(PathBuf::from));
    
    let pyenv_dir = pyenv_dir?;
    let mut version_file = pyenv_dir.join(".version");
    if !version_file.exists() {
        // We might have got the path `~/.pyenv/pyenv-win`
        version_file = pyenv_dir.parent()?.join(".version");
        if !version_file.exists() {
            return None;
        }
    }
    let version = fs::read_to_string(version_file).ok()?;
    Some(
        PYENV_VERSION_FROM_VERSION_FILE
            .captures(&version)?
            .get(1)?
            .as_str()
            .to_string(),
    )
}

Related Issue

microsoft/vscode-python-environments#590

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue identified by VS Code Team member as probable bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions