Skip to content

Commit bb78c7b

Browse files
committed
feat: enhance getUvPythonPath to list only installed uv-managed Python versions
1 parent 6f116b0 commit bb78c7b

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/managers/builtin/uvPythonInstaller.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,40 @@ export async function installUv(_log?: LogOutputChannel): Promise<boolean> {
162162

163163
/**
164164
* Gets the path to the uv-managed Python installation.
165-
* @param version Optional Python version to find (e.g., "3.12")
165+
* Uses `uv python list --only-installed --managed-python` to find only uv-installed Pythons.
166+
* @param version Optional Python version to find (e.g., "3.12"). If not specified, returns the latest.
166167
* @returns Promise that resolves to the Python path, or undefined if not found
167168
*/
168169
export async function getUvPythonPath(version?: string): Promise<string | undefined> {
169170
return new Promise((resolve) => {
170171
const chunks: string[] = [];
171-
const args = ['python', 'find'];
172-
if (version) {
173-
args.push(version);
174-
}
172+
// Use --only-installed --managed-python to find only uv-managed Pythons
173+
const args = ['python', 'list', '--only-installed', '--managed-python', '--output-format', 'json'];
175174
const proc = spawnProcess('uv', args);
176175
proc.stdout?.on('data', (data) => chunks.push(data.toString()));
177176
proc.on('error', () => resolve(undefined));
178177
proc.on('exit', (code) => {
179178
if (code === 0 && chunks.length > 0) {
180-
const pythonPath = chunks.join('').trim();
181-
resolve(pythonPath || undefined);
179+
try {
180+
const versions = JSON.parse(chunks.join('')) as UvPythonVersion[];
181+
if (versions.length === 0) {
182+
resolve(undefined);
183+
return;
184+
}
185+
186+
// If version specified, find matching one (e.g., "3.12" matches "3.12.11")
187+
if (version) {
188+
const match = versions.find((v) => v.version.startsWith(version) && v.path);
189+
resolve(match?.path ?? undefined);
190+
} else {
191+
// Return the first (latest) installed Python
192+
const installed = versions.find((v) => v.path);
193+
resolve(installed?.path ?? undefined);
194+
}
195+
} catch {
196+
traceError('Failed to parse uv python list output');
197+
resolve(undefined);
198+
}
182199
} else {
183200
resolve(undefined);
184201
}
@@ -258,7 +275,7 @@ export async function selectPythonVersionToInstall(): Promise<string | undefined
258275
items.push({
259276
label: `Python ${v.version}`,
260277
description: isInstalled ? `$(check) ${UvInstallStrings.installed}` : undefined,
261-
detail: isInstalled ? v.path ?? undefined : undefined,
278+
detail: isInstalled ? (v.path ?? undefined) : undefined,
262279
version: v.version,
263280
isInstalled,
264281
});

0 commit comments

Comments
 (0)