Skip to content

Commit 8a80ebe

Browse files
author
Kartik Raj
authored
Add experiment to implicitly use environment variables for environment activation (#20651)
1 parent 7aac96a commit 8a80ebe

File tree

33 files changed

+688
-154
lines changed

33 files changed

+688
-154
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ src/client/interpreter/configuration/services/workspaceUpdaterService.ts
146146
src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts
147147
src/client/interpreter/helpers.ts
148148
src/client/interpreter/virtualEnvs/condaInheritEnvPrompt.ts
149-
src/client/interpreter/activation/service.ts
150149
src/client/interpreter/display/index.ts
151150

152151
src/client/api.ts

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"publisher": "ms-python",
2020
"enabledApiProposals": [
2121
"quickPickSortByLabel",
22+
"envShellEvent",
2223
"testObserver"
2324
],
2425
"author": {
@@ -40,7 +41,7 @@
4041
"theme": "dark"
4142
},
4243
"engines": {
43-
"vscode": "^1.75.0-20230123"
44+
"vscode": "^1.76.0"
4445
},
4546
"keywords": [
4647
"python",
@@ -426,12 +427,14 @@
426427
"enum": [
427428
"All",
428429
"pythonSurveyNotification",
429-
"pythonPromptNewToolsExt"
430+
"pythonPromptNewToolsExt",
431+
"pythonTerminalEnvVarActivation"
430432
],
431433
"enumDescriptions": [
432434
"%python.experiments.All.description%",
433435
"%python.experiments.pythonSurveyNotification.description%",
434-
"%python.experiments.pythonPromptNewToolsExt.description%"
436+
"%python.experiments.pythonPromptNewToolsExt.description%",
437+
"%python.experiments.pythonTerminalEnvVarActivation.description%"
435438
]
436439
},
437440
"scope": "machine",
@@ -445,12 +448,14 @@
445448
"enum": [
446449
"All",
447450
"pythonSurveyNotification",
448-
"pythonPromptNewToolsExt"
451+
"pythonPromptNewToolsExt",
452+
"pythonTerminalEnvVarActivation"
449453
],
450454
"enumDescriptions": [
451455
"%python.experiments.All.description%",
452456
"%python.experiments.pythonSurveyNotification.description%",
453-
"%python.experiments.pythonPromptNewToolsExt.description%"
457+
"%python.experiments.pythonPromptNewToolsExt.description%",
458+
"%python.experiments.pythonTerminalEnvVarActivation.description%"
454459
]
455460
},
456461
"scope": "machine",
@@ -1868,7 +1873,7 @@
18681873
"@types/stack-trace": "0.0.29",
18691874
"@types/tmp": "^0.0.33",
18701875
"@types/uuid": "^8.3.4",
1871-
"@types/vscode": "^1.74.0",
1876+
"@types/vscode": "^1.75.0",
18721877
"@types/which": "^2.0.1",
18731878
"@types/winreg": "^1.2.30",
18741879
"@types/xml2js": "^0.4.2",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"python.experiments.All.description": "Combined list of all experiments.",
3838
"python.experiments.pythonSurveyNotification.description": "Denotes the Python Survey Notification experiment.",
3939
"python.experiments.pythonPromptNewToolsExt.description": "Denotes the Python Prompt New Tools Extension experiment.",
40+
"python.experiments.pythonTerminalEnvVarActivation.description": "Enables use of environment variables to activate terminals instead of sending activation commands.",
4041
"python.formatting.autopep8Args.description": "Arguments passed in. Each argument is a separate item in the array.",
4142
"python.formatting.autopep8Path.description": "Path to autopep8, you can use a custom version of autopep8 by modifying this setting to include the full path.",
4243
"python.formatting.blackArgs.description": "Arguments passed in. Each argument is a separate item in the array.",

src/client/common/application/applicationEnvironment.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { inject, injectable } from 'inversify';
77
import * as path from 'path';
88
import { parse } from 'semver';
99
import * as vscode from 'vscode';
10+
import { traceError } from '../../logging';
1011
import { Channel } from '../constants';
1112
import { IPlatformService } from '../platform/types';
1213
import { ICurrentProcess, IPathUtils } from '../types';
@@ -70,19 +71,22 @@ export class ApplicationEnvironment implements IApplicationEnvironment {
7071
public get extensionName(): string {
7172
return this.packageJson.displayName;
7273
}
73-
/**
74-
* At the time of writing this API, the vscode.env.shell isn't officially released in stable version of VS Code.
75-
* Using this in stable version seems to throw errors in VSC with messages being displayed to the user about use of
76-
* unstable API.
77-
* Solution - log and suppress the errors.
78-
* @readonly
79-
* @type {(string)}
80-
* @memberof ApplicationEnvironment
81-
*/
74+
8275
public get shell(): string {
8376
return vscode.env.shell;
8477
}
8578

79+
public get onDidChangeShell(): vscode.Event<string> {
80+
try {
81+
return vscode.env.onDidChangeShell;
82+
} catch (ex) {
83+
traceError('Failed to get onDidChangeShell API', ex);
84+
// `onDidChangeShell` is a proposed API at the time of writing this, so wrap this in a try...catch
85+
// block in case the API is removed or changed.
86+
return new vscode.EventEmitter<string>().event;
87+
}
88+
}
89+
8690
public get packageJson(): any {
8791
return require('../../../../package.json');
8892
}

src/client/common/application/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,10 @@ export interface IApplicationEnvironment {
10481048
* @memberof IApplicationShell
10491049
*/
10501050
readonly shell: string;
1051+
/**
1052+
* An {@link Event} which fires when the default shell changes.
1053+
*/
1054+
readonly onDidChangeShell: Event<string>;
10511055
/**
10521056
* Gets the vscode channel (whether 'insiders' or 'stable').
10531057
*/

src/client/common/experiments/groups.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export enum ShowExtensionSurveyPrompt {
66
export enum ShowToolsExtensionPrompt {
77
experiment = 'pythonPromptNewToolsExt',
88
}
9+
10+
export enum TerminalEnvVarActivation {
11+
experiment = 'pythonTerminalEnvVarActivation',
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { workspace } from 'vscode';
7+
import { isTestExecution } from '../constants';
8+
import { IExperimentService } from '../types';
9+
import { TerminalEnvVarActivation } from './groups';
10+
11+
export function inTerminalEnvVarExperiment(experimentService: IExperimentService): boolean {
12+
if (workspace.workspaceFile && !isTestExecution()) {
13+
// Don't run experiment in multi-root workspaces for now, requires work on VSCode:
14+
// https://github.com/microsoft/vscode/issues/171173
15+
return false;
16+
}
17+
if (!experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)) {
18+
return false;
19+
}
20+
return true;
21+
}

src/client/common/process/logger.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { inject, injectable } from 'inversify';
77
import { traceLog } from '../../logging';
88
import { IWorkspaceService } from '../application/types';
99
import { isCI, isTestExecution } from '../constants';
10-
import { Logging } from '../utils/localize';
1110
import { getOSType, getUserHomeDir, OSType } from '../utils/platform';
1211
import { IProcessLogger, SpawnOptions } from './types';
1312
import { escapeRegExp } from 'lodash';
1413
import { replaceAll } from '../stringUtils';
14+
import { identifyShellFromShellPath } from '../terminal/shellDetectors/baseShellDetector';
1515

1616
@injectable()
1717
export class ProcessLogger implements IProcessLogger {
@@ -27,8 +27,11 @@ export class ProcessLogger implements IProcessLogger {
2727
? [fileOrCommand, ...args].map((e) => e.trimQuotes().toCommandArgumentForPythonExt()).join(' ')
2828
: fileOrCommand;
2929
const info = [`> ${this.getDisplayCommands(command)}`];
30-
if (options && options.cwd) {
31-
info.push(`${Logging.currentWorkingDirectory} ${this.getDisplayCommands(options.cwd)}`);
30+
if (options?.cwd) {
31+
info.push(`cwd: ${this.getDisplayCommands(options.cwd)}`);
32+
}
33+
if (typeof options?.shell === 'string') {
34+
info.push(`shell: ${identifyShellFromShellPath(options?.shell)}`);
3235
}
3336

3437
info.forEach((line) => {

src/client/common/terminal/activator/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import { inject, injectable, multiInject } from 'inversify';
77
import { Terminal } from 'vscode';
8-
import { IConfigurationService } from '../../types';
8+
import { inTerminalEnvVarExperiment } from '../../experiments/helpers';
9+
import { IConfigurationService, IExperimentService } from '../../types';
910
import { ITerminalActivationHandler, ITerminalActivator, ITerminalHelper, TerminalActivationOptions } from '../types';
1011
import { BaseTerminalActivator } from './base';
1112

@@ -17,6 +18,7 @@ export class TerminalActivator implements ITerminalActivator {
1718
@inject(ITerminalHelper) readonly helper: ITerminalHelper,
1819
@multiInject(ITerminalActivationHandler) private readonly handlers: ITerminalActivationHandler[],
1920
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
21+
@inject(IExperimentService) private readonly experimentService: IExperimentService,
2022
) {
2123
this.initialize();
2224
}
@@ -37,7 +39,8 @@ export class TerminalActivator implements ITerminalActivator {
3739
options?: TerminalActivationOptions,
3840
): Promise<boolean> {
3941
const settings = this.configurationService.getSettings(options?.resource);
40-
const activateEnvironment = settings.terminal.activateEnvironment;
42+
const activateEnvironment =
43+
settings.terminal.activateEnvironment && !inTerminalEnvVarExperiment(this.experimentService);
4144
if (!activateEnvironment || options?.hideFromUser) {
4245
return false;
4346
}

0 commit comments

Comments
 (0)