Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 8, 2026

  • Fix nativeToPythonEnv in pyenvUtils.ts - extract getPyenvDir helper
  • Address reviewer feedback: use PYENV_ROOT env var as primary source, revert 3-levels-up to 2-levels-up (old behavior was correct for pyenv-win since versions live under ~/.pyenv/pyenv-win/versions/)
  • Fix POSIX test to use absolute path with path.sep
  • Add test for PYENV_ROOT preference
  • Validate changes compile and all 583 tests pass (no regressions)
  • Run code review and security scan
Original prompt

This section details on the original issue you should resolve

<issue_title>PyEnv: Windows path calculation bug causes environment grouping to fail</issue_title>
<issue_description>## Problem

In pyenvUtils.ts, the calculation of versionsPath and envsPaths is incorrect on Windows due to the pyenv-win directory structure.

Current Code

// pyenvUtils.ts L128-130
const versionsPath = normalizePath(path.join(path.dirname(path.dirname(pyenv)), 'versions'));
const envsPaths = normalizePath(path.join(path.dirname(versionsPath), 'envs'));

Issue

On Windows, pyenv is located at ~/.pyenv/pyenv-win/bin/pyenv.bat. The current logic calculates:

  • path.dirname(path.dirname(pyenv))~/.pyenv/pyenv-win
  • Should be ~/.pyenv to find versions folder properly

This causes:

  1. Environment grouping (PYENV_VERSIONS vs PYENV_ENVIRONMENTS) to fail
  2. Environments not being associated with the correct manager

PET Server Reference

The PET server handles this correctly with platform-specific code in pet-pyenv/src/environment_locations.rs:

#[cfg(windows)]
pub fn get_home_pyenv_dir(env_vars: &EnvVariables) -> Option<PathBuf> {
    let home = env_vars.home.clone()?;
    Some(norm_case(home.join(".pyenv").join("pyenv-win")))
}

Suggested Fix

Add Windows-specific handling when calculating the versions and envs paths:

const pyenvDir = isWindows() 
    ? path.dirname(path.dirname(path.dirname(pyenv))) // Go up 3 levels on Windows
    : path.dirname(path.dirname(pyenv));
const versionsPath = normalizePath(path.join(pyenvDir, 'versions'));

Affected File

src/managers/pyenv/pyenvUtils.ts</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 8, 2026 00:04
On Windows, pyenv-win is at ~/.pyenv/pyenv-win/bin/pyenv.bat, so we
need to go up 3 directory levels (not 2) to reach the pyenv root
directory (~/.pyenv). This fixes environment grouping (PYENV_VERSIONS
vs PYENV_ENVIRONMENTS) which was broken because versionsPath and
envsPaths were calculated from the wrong parent directory.

Co-authored-by: karthiknadig <3840081+karthiknadig@users.noreply.github.com>
Co-authored-by: karthiknadig <3840081+karthiknadig@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Windows path calculation bug in pyenvUtils Fix PyEnv Windows path calculation for environment grouping Feb 8, 2026
Copilot AI requested a review from karthiknadig February 8, 2026 00:08
@karthiknadig karthiknadig added the bug Issue identified by VS Code Team member as probable bug label Feb 8, 2026
Copy link
Member

@karthiknadig karthiknadig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core fix (extracting getPyenvDir with platform-aware logic) is a good idea, but I'm concerned the 3-levels-up calculation on Windows may produce the wrong root directory for environment grouping. Please verify what info.prefix the PET server returns for pyenv-win environments — if prefixes are under ~/.pyenv/pyenv-win/versions/, then versionsPath should also be computed relative to ~/.pyenv/pyenv-win, not ~/.pyenv.

…ests

- Use PYENV_ROOT env var as primary source for pyenv root directory
- Revert 3-levels-up Windows logic to 2-levels-up (pyenv-win versions
  are at ~/.pyenv/pyenv-win/versions/, not ~/.pyenv/versions/)
- Add test for PYENV_ROOT preference
- Fix POSIX test to use absolute paths with path.sep
- Fix Windows test expectation to reflect correct 2-levels-up behavior

Co-authored-by: karthiknadig <3840081+karthiknadig@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 8, 2026

The core fix (extracting getPyenvDir with platform-aware logic) is a good idea, but I'm concerned the 3-levels-up calculation on Windows may produce the wrong root directory for environment grouping...

You were right — pyenv-win prefixes are under ~/.pyenv/pyenv-win/versions/, so the 2-levels-up calculation is correct. I've reverted that and now use PYENV_ROOT as the primary source, falling back to 2-levels-up. Fixed in 12033b0.

Copy link
Member

@karthiknadig karthiknadig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The updated approach is much better — using PYENV_ROOT as the primary source and keeping a consistent 2-levels-up fallback avoids the fragile platform-specific directory counting. Tests cover all three cases well.

@karthiknadig karthiknadig marked this pull request as ready for review February 9, 2026 04:49
@karthiknadig karthiknadig enabled auto-merge (squash) February 9, 2026 04:49
@vs-code-engineering vs-code-engineering bot added this to the February 2026 milestone Feb 9, 2026
@karthiknadig karthiknadig merged commit 25d851c into main Feb 9, 2026
11 checks passed
@karthiknadig karthiknadig deleted the copilot/fix-windows-path-calculation branch February 9, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PyEnv: Windows path calculation bug causes environment grouping to fail

3 participants