Skip to content

Commit 6a29d04

Browse files
authored
fix: set env correctly on package creation (#543)
fixes #544
1 parent dbc867f commit 6a29d04

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/features/creators/newPackageProject.ts

Lines changed: 40 additions & 17 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

@@ -113,22 +112,51 @@ export class NewPackageProject implements PythonProjectCreator {
113112
// 2. Replace 'package_name' in all files and file/folder names using a helper
114113
await replaceInFilesAndNames(projectDestinationFolder, 'package_name', packageName);
115114

115+
const createdPackage: PythonProject | undefined = {
116+
name: packageName,
117+
uri: Uri.file(projectDestinationFolder),
118+
};
119+
// add package to list of packages
120+
this.projectManager.add(createdPackage);
121+
116122
// 4. Create virtual environment if requested
123+
let createdEnv: PythonEnvironment | undefined;
117124
if (createVenv) {
118-
// add package to list of packages before creating the venv
119-
await quickCreateNewVenv(this.envManagers, projectDestinationFolder);
125+
// gets default environment manager
126+
const en = this.envManagers.getEnvironmentManager(undefined);
127+
if (en?.supportsQuickCreate) {
128+
// opt to use quickCreate if available
129+
createdEnv = await en.create(Uri.file(projectDestinationFolder), { quickCreate: true });
130+
} else if (!options?.quickCreate && en?.supportsCreate) {
131+
// if quickCreate unavailable, use create method only if project is not quickCreate
132+
createdEnv = await en.create(Uri.file(projectDestinationFolder), {});
133+
} else {
134+
// get venv manager or any manager that supports quick creating environments
135+
const venvManager = this.envManagers.managers.find(
136+
(m) => m.id === 'ms-python.python:venv' || m.supportsQuickCreate,
137+
);
138+
if (venvManager) {
139+
createdEnv = await venvManager.create(Uri.file(projectDestinationFolder), {
140+
quickCreate: true,
141+
});
142+
} else {
143+
window.showErrorMessage(l10n.t('Creating virtual environment failed during package creation.'));
144+
}
145+
}
120146
}
121-
122-
// 5. Get the Python environment for the destination folder
123-
// could be either the one created in an early step or an existing one
124-
const pythonEnvironment = await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder));
125-
126-
if (!pythonEnvironment) {
127-
window.showErrorMessage(l10n.t('Python environment not found.'));
147+
// 5. Get the Python environment for the destination folder if not already created
148+
createdEnv = createdEnv || (await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder)));
149+
if (!createdEnv) {
150+
window.showErrorMessage(
151+
l10n.t('Project created but unable to be correlated to correct Python environment.'),
152+
);
128153
return undefined;
129154
}
130155

131-
// add custom github copilot instructions
156+
// 6. Set the Python environment for the package
157+
this.envManagers.setEnvironment(createdPackage?.uri, createdEnv);
158+
159+
// 7. add custom github copilot instructions
132160
if (createCopilotInstructions) {
133161
const packageInstructionsPath = path.join(
134162
NEW_PROJECT_TEMPLATES_FOLDER,
@@ -149,11 +177,6 @@ export class NewPackageProject implements PythonProjectCreator {
149177
};
150178
await manageLaunchJsonFile(destRoot, JSON.stringify(launchJsonConfig));
151179

152-
const createdPackage: PythonProject | undefined = {
153-
name: packageName,
154-
uri: Uri.file(projectDestinationFolder),
155-
};
156-
this.projectManager.add(createdPackage);
157180
return createdPackage;
158181
}
159182
return undefined;

0 commit comments

Comments
 (0)