Skip to content

Commit f622704

Browse files
authored
bug: update quickCreateVenv to avoid name collisions for virtual environments (#531)
fixes #477
1 parent 9ecfd3c commit f622704

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

src/managers/builtin/venvUtils.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,22 @@ export async function quickCreateVenv(
451451
if (additionalPackages) {
452452
allPackages.push(...additionalPackages);
453453
}
454-
return await createWithProgress(
455-
nativeFinder,
456-
api,
457-
log,
458-
manager,
459-
baseEnv,
460-
venvRoot,
461-
path.join(venvRoot.fsPath, '.venv'),
462-
{ install: allPackages, uninstall: [] },
463-
);
454+
455+
// Check if .venv already exists
456+
let venvPath = path.join(venvRoot.fsPath, '.venv');
457+
if (await fsapi.pathExists(venvPath)) {
458+
// increment to create a unique name, e.g. .venv-1
459+
let i = 1;
460+
while (await fsapi.pathExists(`${venvPath}-${i}`)) {
461+
i++;
462+
}
463+
venvPath = `${venvPath}-${i}`;
464+
}
465+
466+
return await createWithProgress(nativeFinder, api, log, manager, baseEnv, venvRoot, venvPath, {
467+
install: allPackages,
468+
uninstall: [],
469+
});
464470
}
465471

466472
export async function createPythonVenv(
@@ -473,7 +479,6 @@ export async function createPythonVenv(
473479
options: { showQuickAndCustomOptions: boolean; additionalPackages?: string[] },
474480
): Promise<PythonEnvironment | undefined> {
475481
const sortedEnvs = ensureGlobalEnv(basePythons, log);
476-
const project = api.getPythonProject(venvRoot);
477482

478483
let customize: boolean | undefined = true;
479484
if (options.showQuickAndCustomOptions) {
@@ -483,26 +488,11 @@ export async function createPythonVenv(
483488
if (customize === undefined) {
484489
return;
485490
} else if (customize === false) {
486-
sendTelemetryEvent(EventNames.VENV_CREATION, undefined, { creationType: 'quick' });
487-
const installables = await getProjectInstallable(api, project ? [project] : undefined);
488-
const allPackages = [];
489-
allPackages.push(...(installables?.flatMap((i) => i.args ?? []) ?? []));
490-
if (options.additionalPackages) {
491-
allPackages.push(...options.additionalPackages);
492-
}
493-
return await createWithProgress(
494-
nativeFinder,
495-
api,
496-
log,
497-
manager,
498-
sortedEnvs[0],
499-
venvRoot,
500-
path.join(venvRoot.fsPath, '.venv'),
501-
{ install: allPackages, uninstall: [] },
502-
);
491+
return quickCreateVenv(nativeFinder, api, log, manager, sortedEnvs[0], venvRoot, options.additionalPackages);
503492
} else {
504493
sendTelemetryEvent(EventNames.VENV_CREATION, undefined, { creationType: 'custom' });
505494
}
495+
const project = api.getPythonProject(venvRoot);
506496

507497
const basePython = await pickEnvironmentFrom(sortedEnvs);
508498
if (!basePython || !basePython.execInfo) {

0 commit comments

Comments
 (0)