Skip to content

Commit 51d80fe

Browse files
karthiknadigeleanorjboyd
authored andcommitted
1 parent fc4b0d5 commit 51d80fe

23 files changed

+891
-529
lines changed

pythonFiles/create_conda.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def install_packages(env_path: str) -> None:
8585
],
8686
"CREATE_CONDA.FAILED_INSTALL_YML",
8787
)
88+
print("CREATE_CONDA.INSTALLED_YML")
8889

8990

9091
def add_gitignore(name: str) -> None:
@@ -100,7 +101,10 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
100101
argv = []
101102
args = parse_args(argv)
102103

103-
if not conda_env_exists(args.name):
104+
if conda_env_exists(args.name):
105+
env_path = get_conda_env_path(args.name)
106+
print(f"EXISTING_CONDA_ENV:{env_path}")
107+
else:
104108
run_process(
105109
[
106110
sys.executable,
@@ -114,12 +118,11 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
114118
],
115119
"CREATE_CONDA.ENV_FAILED_CREATION",
116120
)
121+
env_path = get_conda_env_path(args.name)
122+
print(f"CREATED_CONDA_ENV:{env_path}")
117123
if args.git_ignore:
118124
add_gitignore(args.name)
119125

120-
env_path = get_conda_env_path(args.name)
121-
print(f"CREATED_CONDA_ENV:{env_path}")
122-
123126
if args.install:
124127
install_packages(env_path)
125128

pythonFiles/create_venv.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
5151

5252

5353
def venv_exists(name: str) -> bool:
54-
return os.path.exists(CWD / name)
54+
return os.path.exists(CWD / name) and file_exists(get_venv_path(name))
5555

5656

5757
def run_process(args: Sequence[str], error_message: str) -> None:
@@ -72,9 +72,6 @@ def get_venv_path(name: str) -> str:
7272

7373

7474
def install_packages(venv_path: str) -> None:
75-
if not is_installed("pip"):
76-
raise VenvError("CREATE_VENV.PIP_NOT_FOUND")
77-
7875
requirements = os.fspath(CWD / "requirements.txt")
7976
pyproject = os.fspath(CWD / "pyproject.toml")
8077

@@ -89,12 +86,14 @@ def install_packages(venv_path: str) -> None:
8986
[venv_path, "-m", "pip", "install", "-r", requirements],
9087
"CREATE_VENV.PIP_FAILED_INSTALL_REQUIREMENTS",
9188
)
89+
print("CREATE_VENV.PIP_INSTALLED_REQUIREMENTS")
9290
elif file_exists(pyproject):
9391
print(f"VENV_INSTALLING_PYPROJECT: {pyproject}")
9492
run_process(
9593
[venv_path, "-m", "pip", "install", "-e", ".[extras]"],
9694
"CREATE_VENV.PIP_FAILED_INSTALL_PYPROJECT",
9795
)
96+
print("CREATE_VENV.PIP_INSTALLED_PYPROJECT")
9897

9998

10099
def add_gitignore(name: str) -> None:
@@ -110,20 +109,27 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
110109
argv = []
111110
args = parse_args(argv)
112111

113-
if is_installed("venv"):
114-
if not venv_exists(args.name):
115-
run_process(
116-
[sys.executable, "-m", "venv", args.name],
117-
"CREATE_VENV.VENV_FAILED_CREATION",
118-
)
119-
if args.git_ignore:
120-
add_gitignore(args.name)
112+
if not is_installed("venv"):
113+
raise VenvError("CREATE_VENV.VENV_NOT_FOUND")
114+
115+
if args.install and not is_installed("pip"):
116+
raise VenvError("CREATE_VENV.PIP_NOT_FOUND")
117+
118+
if venv_exists(args.name):
121119
venv_path = get_venv_path(args.name)
122-
print(f"CREATED_VENV:{venv_path}")
123-
if args.install:
124-
install_packages(venv_path)
120+
print(f"EXISTING_VENV:{venv_path}")
125121
else:
126-
raise VenvError("CREATE_VENV.VENV_NOT_FOUND")
122+
run_process(
123+
[sys.executable, "-m", "venv", args.name],
124+
"CREATE_VENV.VENV_FAILED_CREATION",
125+
)
126+
venv_path = get_venv_path(args.name)
127+
print(f"CREATED_VENV:{venv_path}")
128+
if args.git_ignore:
129+
add_gitignore(args.name)
130+
131+
if args.install:
132+
install_packages(venv_path)
127133

128134

129135
if __name__ == "__main__":

src/client/common/utils/localize.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,12 @@ export namespace SwitchToDefaultLS {
552552
}
553553

554554
export namespace CreateEnv {
555+
export const informEnvCreation = localize(
556+
'createEnv.informEnvCreation',
557+
'We have selected the following environment:',
558+
);
555559
export const statusTitle = localize('createEnv.statusTitle', 'Creating environment');
556560
export const statusStarting = localize('createEnv.statusStarting', 'Starting...');
557-
export const statusError = localize('createEnv.statusError', 'Error.');
558-
export const statusDone = localize('createEnv.statusDone', 'Done.');
559561

560562
export const hasVirtualEnv = localize('createEnv.hasVirtualEnv', 'Workspace folder contains a virtual environment');
561563

@@ -564,21 +566,23 @@ export namespace CreateEnv {
564566
'Please open a directory when creating an environment using venv.',
565567
);
566568

567-
export const pickWorkspaceTitle = localize(
568-
'createEnv.workspaceQuickPick.title',
569+
export const pickWorkspacePlaceholder = localize(
570+
'createEnv.workspaceQuickPick.placeholder',
569571
'Select a workspace to create environment',
570572
);
571573

572-
export const providersQuickPickTitle = localize('createEnv.providersQuickPick.title', 'Select an environment type');
574+
export const providersQuickPickPlaceholder = localize(
575+
'createEnv.providersQuickPick.placeholder',
576+
'Select an environment type',
577+
);
573578

574579
export namespace Venv {
575580
export const creating = localize('createEnv.venv.creating', 'Creating venv...');
576581
export const created = localize('createEnv.venv.created', 'Environment created...');
577582
export const installingPackages = localize('createEnv.venv.installingPackages', 'Installing packages...');
578-
export const waitingForPython = localize('createEnv.venv.waitingForPython', 'Waiting on Python selection...');
579-
export const waitingForWorkspace = localize(
580-
'createEnv.venv.waitingForWorkspace',
581-
'Waiting on workspace selection...',
583+
export const errorCreatingEnvironment = localize(
584+
'createEnv.venv.errorCreatingEnvironment',
585+
'Error while creating virtual environment.',
582586
);
583587
export const selectPythonQuickPickTitle = localize(
584588
'createEnv.venv.basePython.title',
@@ -588,6 +592,7 @@ export namespace CreateEnv {
588592
'createEnv.venv.description',
589593
'Creates a `.venv` virtual environment in the current workspace',
590594
);
595+
export const error = localize('createEnv.venv.error', 'Creating virtual environment failed with error.');
591596
}
592597

593598
export namespace Conda {
@@ -601,20 +606,11 @@ export namespace CreateEnv {
601606
'createEnv.conda.errorCreatingEnvironment',
602607
'Error while creating conda environment.',
603608
);
604-
export const waitingForWorkspace = localize(
605-
'createEnv.conda.waitingForWorkspace',
606-
'Waiting on workspace selection...',
607-
);
608-
export const waitingForPython = localize(
609-
'createEnv.conda.waitingForPython',
610-
'Waiting on Python version selection...',
611-
);
612-
export const selectPythonQuickPickTitle = localize(
613-
'createEnv.conda.pythonSelection.title',
609+
export const selectPythonQuickPickPlaceholder = localize(
610+
'createEnv.conda.pythonSelection.placeholder',
614611
'Please select the version of Python to install in the environment',
615612
);
616-
export const searching = localize('createEnv.conda.searching', 'Searching for conda (base)...');
617-
export const creating = localize('createEnv.venv.creating', 'Running conda create...');
613+
export const creating = localize('createEnv.conda.creating', 'Creating conda environment...');
618614
export const providerDescription = localize(
619615
'createEnv.conda.description',
620616
'Creates a `.conda` Conda environment in the current workspace',

src/client/common/vscodeApis/windowApis.ts

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ export function showErrorMessage<T>(message: string, ...items: any[]): Thenable<
3838
return window.showErrorMessage(message, ...items);
3939
}
4040

41+
export function showInformationMessage<T extends string>(message: string, ...items: T[]): Thenable<T | undefined>;
42+
export function showInformationMessage<T extends string>(
43+
message: string,
44+
options: MessageOptions,
45+
...items: T[]
46+
): Thenable<T | undefined>;
47+
export function showInformationMessage<T extends MessageItem>(message: string, ...items: T[]): Thenable<T | undefined>;
48+
export function showInformationMessage<T extends MessageItem>(
49+
message: string,
50+
options: MessageOptions,
51+
...items: T[]
52+
): Thenable<T | undefined>;
53+
54+
export function showInformationMessage<T>(message: string, ...items: any[]): Thenable<T | undefined> {
55+
return window.showInformationMessage(message, ...items);
56+
}
57+
4158
export function withProgress<R>(
4259
options: ProgressOptions,
4360
task: (progress: Progress<{ message?: string; increment?: number }>, token: CancellationToken) => Thenable<R>,

src/client/extensionActivation.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import { IApplicationEnvironment, ICommandManager, IWorkspaceService } from './c
2121
import { Commands, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL, UseProposedApi } from './common/constants';
2222
import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry';
2323
import { IFileSystem } from './common/platform/types';
24-
import { IConfigurationService, IDisposableRegistry, IExtensions, IOutputChannel } from './common/types';
24+
import {
25+
IConfigurationService,
26+
IDisposableRegistry,
27+
IExtensions,
28+
IInterpreterPathService,
29+
IOutputChannel,
30+
} from './common/types';
2531
import { noop } from './common/utils/misc';
2632
import { DebuggerTypeName } from './debugger/constants';
2733
import { registerTypes as debugConfigurationRegisterTypes } from './debugger/extension/serviceRegistry';
@@ -97,11 +103,14 @@ export async function activateComponents(
97103
return Promise.all([legacyActivationResult, ...promises]);
98104
}
99105

100-
export function activateFeatures(ext: ExtensionState, components: Components): void {
106+
export function activateFeatures(ext: ExtensionState, _components: Components): void {
101107
const interpreterQuickPick: IInterpreterQuickPick = ext.legacyIOC.serviceContainer.get<IInterpreterQuickPick>(
102108
IInterpreterQuickPick,
103109
);
104-
registerCreateEnvironmentFeatures(ext.disposables, components.pythonEnvs, interpreterQuickPick);
110+
const interpreterPathService: IInterpreterPathService = ext.legacyIOC.serviceContainer.get<IInterpreterPathService>(
111+
IInterpreterPathService,
112+
);
113+
registerCreateEnvironmentFeatures(ext.disposables, interpreterQuickPick, interpreterPathService);
105114
}
106115

107116
/// //////////////////////////

src/client/interpreter/virtualEnvs/virtualEnvPrompt.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { ConfigurationTarget, Disposable, Uri } from 'vscode';
66
import { IExtensionActivationService } from '../../activation/types';
77
import { IApplicationShell } from '../../common/application/types';
88
import { IDisposableRegistry, IPersistentStateFactory } from '../../common/types';
9-
import { sleep } from '../../common/utils/async';
109
import { Common, Interpreters } from '../../common/utils/localize';
1110
import { traceDecoratorError, traceVerbose } from '../../logging';
11+
import { isCreatingEnvironment } from '../../pythonEnvironments/creation/createEnvApi';
1212
import { PythonEnvironment } from '../../pythonEnvironments/info';
1313
import { sendTelemetryEvent } from '../../telemetry';
1414
import { EventName } from '../../telemetry/constants';
@@ -38,8 +38,9 @@ export class VirtualEnvironmentPrompt implements IExtensionActivationService {
3838

3939
@traceDecoratorError('Error in event handler for detection of new environment')
4040
protected async handleNewEnvironment(resource: Uri): Promise<void> {
41-
// Wait for a while, to ensure environment gets created and is accessible (as this is slow on Windows)
42-
await sleep(1000);
41+
if (isCreatingEnvironment()) {
42+
return;
43+
}
4344
const interpreters = await this.pyenvs.getWorkspaceVirtualEnvInterpreters(resource);
4445
const interpreter =
4546
Array.isArray(interpreters) && interpreters.length > 0

src/client/pythonEnvironments/creation/common/workspaceSelection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function pickWorkspaceFolder(
5151
const selected = await showQuickPick(
5252
getWorkspacesForQuickPick(workspaces),
5353
{
54-
title: CreateEnv.pickWorkspaceTitle,
54+
placeHolder: CreateEnv.pickWorkspacePlaceholder,
5555
ignoreFocusOut: true,
5656
canPickMany: options?.allowMultiSelect,
5757
},

src/client/pythonEnvironments/creation/createEnvApi.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { Disposable } from 'vscode';
4+
import { ConfigurationTarget, Disposable } from 'vscode';
55
import { Commands } from '../../common/constants';
6-
import { IDisposableRegistry } from '../../common/types';
6+
import { IDisposableRegistry, IInterpreterPathService } from '../../common/types';
77
import { registerCommand } from '../../common/vscodeApis/commandApis';
88
import { IInterpreterQuickPick } from '../../interpreter/configuration/types';
9-
import { IDiscoveryAPI } from '../base/locator';
10-
import { handleCreateEnvironmentCommand } from './createEnvQuickPick';
9+
import { getCreationEvents, handleCreateEnvironmentCommand } from './createEnvironment';
1110
import { condaCreationProvider } from './provider/condaCreationProvider';
1211
import { VenvCreationProvider } from './provider/venvCreationProvider';
13-
import { CreateEnvironmentOptions, CreateEnvironmentProvider } from './types';
12+
import { CreateEnvironmentOptions, CreateEnvironmentProvider, CreateEnvironmentResult } from './types';
13+
import { showInformationMessage } from '../../common/vscodeApis/windowApis';
14+
import { CreateEnv } from '../../common/utils/localize';
1415

1516
class CreateEnvironmentProviders {
1617
private _createEnvProviders: CreateEnvironmentProvider[] = [];
@@ -41,20 +42,30 @@ export function registerCreateEnvironmentProvider(provider: CreateEnvironmentPro
4142
});
4243
}
4344

45+
export const { onCreateEnvironmentStarted, onCreateEnvironmentExited, isCreatingEnvironment } = getCreationEvents();
46+
4447
export function registerCreateEnvironmentFeatures(
4548
disposables: IDisposableRegistry,
46-
discoveryApi: IDiscoveryAPI,
4749
interpreterQuickPick: IInterpreterQuickPick,
50+
interpreterPathService: IInterpreterPathService,
4851
): void {
4952
disposables.push(
5053
registerCommand(
5154
Commands.Create_Environment,
52-
(options?: CreateEnvironmentOptions): Promise<string | undefined> => {
55+
(options?: CreateEnvironmentOptions): Promise<CreateEnvironmentResult | undefined> => {
5356
const providers = _createEnvironmentProviders.getAll();
5457
return handleCreateEnvironmentCommand(providers, options);
5558
},
5659
),
5760
);
58-
disposables.push(registerCreateEnvironmentProvider(new VenvCreationProvider(discoveryApi, interpreterQuickPick)));
61+
disposables.push(registerCreateEnvironmentProvider(new VenvCreationProvider(interpreterQuickPick)));
5962
disposables.push(registerCreateEnvironmentProvider(condaCreationProvider()));
63+
disposables.push(
64+
onCreateEnvironmentExited(async (e: CreateEnvironmentResult | undefined) => {
65+
if (e && e.path) {
66+
await interpreterPathService.update(e.uri, ConfigurationTarget.WorkspaceFolder, e.path);
67+
showInformationMessage(`${CreateEnv.informEnvCreation} ${e.path}`);
68+
}
69+
}),
70+
);
6071
}

src/client/pythonEnvironments/creation/createEnvQuickPick.ts

-54
This file was deleted.

0 commit comments

Comments
 (0)