* Detects the virtual environment root for a given Python executable path. * * Checks for `pyvenv.cfg` which is the standard marker for Python venvs. * Handles both `/path/to/venv/bin/python` and `/path/to/venv/Scripts/python.exe`. * * @returns The venv root directory, or null if the Python is
(pythonExecutable: string)
| 151 | * @returns The venv root directory, or null if the Python is not in a venv |
| 152 | */ |
| 153 | async function detectVenvRoot(pythonExecutable: string): Promise<string | null> { |
| 154 | const binDir = dirname(pythonExecutable) |
| 155 | const possibleVenvRoot = dirname(binDir) |
| 156 | |
| 157 | const pyvenvCfg = join(possibleVenvRoot, 'pyvenv.cfg') |
| 158 | const cfgStat = await stat(pyvenvCfg).catch(() => null) |
| 159 | if (cfgStat?.isFile()) { |
| 160 | return possibleVenvRoot |
| 161 | } |
| 162 | |
| 163 | return null |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Builds environment variables appropriate for the resolved Python executable. |