Skip to content

Commit f942dd7

Browse files
committed
fix: set env correctly on package creation
1 parent f622704 commit f942dd7

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

src/features/creators/newPackageProject.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs-extra';
22
import * as path from 'path';
33
import { commands, l10n, MarkdownString, QuickInputButtons, Uri, window, workspace } from 'vscode';
4-
import { PythonProject, PythonProjectCreator, PythonProjectCreatorOptions } from '../../api';
4+
import { PythonEnvironment, PythonProject, PythonProjectCreator, PythonProjectCreatorOptions } from '../../api';
55
import { NEW_PROJECT_TEMPLATES_FOLDER } from '../../common/constants';
66
import { traceError } from '../../common/logging';
77
import { showInputBoxWithButtons } from '../../common/window.apis';
@@ -11,7 +11,6 @@ import {
1111
manageCopilotInstructionsFile,
1212
manageLaunchJsonFile,
1313
promptForVenv,
14-
quickCreateNewVenv,
1514
replaceInFilesAndNames,
1615
} from './creationHelpers';
1716

@@ -115,6 +114,7 @@ export class NewPackageProject implements PythonProjectCreator {
115114

116115
// 4. Create virtual environment if requested
117116
let createdPackage: PythonProject | undefined;
117+
let createdEnv: PythonEnvironment | undefined;
118118
if (createVenv) {
119119
createdPackage = {
120120
name: packageName,
@@ -123,19 +123,41 @@ export class NewPackageProject implements PythonProjectCreator {
123123

124124
// add package to list of packages before creating the venv
125125
this.projectManager.add(createdPackage);
126-
await quickCreateNewVenv(this.envManagers, projectDestinationFolder);
126+
// gets default environment manager
127+
const en = this.envManagers.getEnvironmentManager(undefined);
128+
if (en?.supportsQuickCreate) {
129+
// opt to use quickCreate if available
130+
createdEnv = await en.create(Uri.file(projectDestinationFolder), { quickCreate: true });
131+
} else if (!options?.quickCreate && en?.supportsCreate) {
132+
// if quickCreate unavailable, use create method only if project is not quickCreate
133+
createdEnv = await en.create(Uri.file(projectDestinationFolder), {});
134+
} else {
135+
// get venv manager or any manager that supports quick creating environments
136+
const venvManager = this.envManagers.managers.find(
137+
(m) => m.id === 'ms-python.python:venv' || m.supportsQuickCreate,
138+
);
139+
if (venvManager) {
140+
createdEnv = await venvManager.create(Uri.file(projectDestinationFolder), {
141+
quickCreate: true,
142+
});
143+
} else {
144+
window.showErrorMessage(l10n.t('Creating virtual environment failed during package creation.'));
145+
}
146+
}
127147
}
128-
129-
// 5. Get the Python environment for the destination folder
130-
// could be either the one created in an early step or an existing one
131-
const pythonEnvironment = await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder));
132-
133-
if (!pythonEnvironment) {
134-
window.showErrorMessage(l10n.t('Python environment not found.'));
148+
// 5. Get the Python environment for the destination folder if not already created
149+
createdEnv = createdEnv || (await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder)));
150+
if (!createdEnv) {
151+
window.showErrorMessage(
152+
l10n.t('Project created but unable to be correlated to correct Python environment.'),
153+
);
135154
return undefined;
136155
}
137156

138-
// add custom github copilot instructions
157+
// 6. Set the Python environment for the package
158+
this.envManagers.setEnvironment(createdPackage?.uri, createdEnv);
159+
160+
// 7. add custom github copilot instructions
139161
if (createCopilotInstructions) {
140162
const packageInstructionsPath = path.join(
141163
NEW_PROJECT_TEMPLATES_FOLDER,

0 commit comments

Comments
 (0)