Skip to content

Commit 1bc8bdd

Browse files
Kartik Rajanthonykim1
Kartik Raj
authored andcommitted
Use updated API to fetch scoped env collection (microsoft#21788)
For microsoft/vscode#171173 microsoft#20822 To be merged tomorrow when latest insiders is released. Blocked on microsoft/vscode#189979.
1 parent cdfd58e commit 1bc8bdd

File tree

5 files changed

+46
-61
lines changed

5 files changed

+46
-61
lines changed

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"theme": "dark"
4747
},
4848
"engines": {
49-
"vscode": "^1.79.0-20230526"
49+
"vscode": "^1.81.0-20230809"
5050
},
5151
"enableTelemetry": false,
5252
"keywords": [
@@ -2155,7 +2155,7 @@
21552155
"@types/stack-trace": "0.0.29",
21562156
"@types/tmp": "^0.0.33",
21572157
"@types/uuid": "^8.3.4",
2158-
"@types/vscode": "^1.75.0",
2158+
"@types/vscode": "^1.81.0",
21592159
"@types/which": "^2.0.1",
21602160
"@types/winreg": "^1.2.30",
21612161
"@types/xml2js": "^0.4.2",

src/client/interpreter/activation/terminalEnvVarCollectionService.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@
33

44
import * as path from 'path';
55
import { inject, injectable } from 'inversify';
6-
import {
7-
ProgressOptions,
8-
ProgressLocation,
9-
MarkdownString,
10-
WorkspaceFolder,
11-
EnvironmentVariableCollection,
12-
EnvironmentVariableScope,
13-
} from 'vscode';
6+
import { ProgressOptions, ProgressLocation, MarkdownString, WorkspaceFolder } from 'vscode';
147
import { pathExists } from 'fs-extra';
158
import { IExtensionActivationService } from '../../activation/types';
169
import { IApplicationShell, IApplicationEnvironment, IWorkspaceService } from '../../common/application/types';
@@ -67,7 +60,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
6760

6861
public async activate(resource: Resource): Promise<void> {
6962
if (!inTerminalEnvVarExperiment(this.experimentService)) {
70-
this.context.environmentVariableCollection.clear();
63+
const workspaceFolder = this.getWorkspaceFolder(resource);
64+
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
7165
await this.handleMicroVenv(resource);
7266
if (!this.registeredOnce) {
7367
this.interpreterService.onDidChangeInterpreter(
@@ -111,8 +105,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
111105
public async _applyCollection(resource: Resource, shell = this.applicationEnvironment.shell): Promise<void> {
112106
const workspaceFolder = this.getWorkspaceFolder(resource);
113107
const settings = this.configurationService.getSettings(resource);
114-
const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder);
115-
// Clear any previously set env vars from collection.
108+
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
109+
// Clear any previously set env vars from collection
116110
envVarCollection.clear();
117111
if (!settings.terminal.activateEnvironment) {
118112
traceVerbose('Activating environments in terminal is disabled for', resource?.fsPath);
@@ -160,7 +154,10 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
160154
return;
161155
}
162156
traceVerbose(`Setting environment variable ${key} in collection to ${value}`);
163-
envVarCollection.replace(key, value, { applyAtShellIntegration: true });
157+
envVarCollection.replace(key, value, {
158+
applyAtShellIntegration: true,
159+
applyAtProcessCreation: true,
160+
});
164161
}
165162
}
166163
});
@@ -170,22 +167,13 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
170167
envVarCollection.description = description;
171168
}
172169

173-
private getEnvironmentVariableCollection(workspaceFolder?: WorkspaceFolder) {
174-
const envVarCollection = this.context.environmentVariableCollection as EnvironmentVariableCollection & {
175-
getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection;
176-
};
177-
return workspaceFolder
178-
? envVarCollection.getScopedEnvironmentVariableCollection({ workspaceFolder })
179-
: envVarCollection;
180-
}
181-
182170
private async handleMicroVenv(resource: Resource) {
183171
const workspaceFolder = this.getWorkspaceFolder(resource);
184172
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
185173
if (interpreter?.envType === EnvironmentType.Venv) {
186174
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
187175
if (!(await pathExists(activatePath))) {
188-
const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder);
176+
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
189177
const pathVarName = getSearchPathEnvVarNames()[0];
190178
envVarCollection.replace(
191179
'PATH',
@@ -195,7 +183,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
195183
return;
196184
}
197185
}
198-
this.context.environmentVariableCollection.clear();
186+
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
199187
}
200188

201189
private getWorkspaceFolder(resource: Resource): WorkspaceFolder | undefined {

src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ suite('Terminal Environment Variable Collection Service', () => {
4545
let collection: EnvironmentVariableCollection & {
4646
getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection;
4747
};
48-
let scopedCollection: EnvironmentVariableCollection;
4948
let applicationEnvironment: IApplicationEnvironment;
5049
let environmentActivationService: IEnvironmentActivationService;
5150
let workspaceService: IWorkspaceService;
@@ -73,9 +72,7 @@ suite('Terminal Environment Variable Collection Service', () => {
7372
getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection;
7473
}
7574
>();
76-
scopedCollection = mock<EnvironmentVariableCollection>();
77-
when(collection.getScopedEnvironmentVariableCollection(anything())).thenReturn(instance(scopedCollection));
78-
when(context.environmentVariableCollection).thenReturn(instance(collection));
75+
when(context.getEnvironmentVariableCollection(anything())).thenReturn(instance(collection));
7976
experimentService = mock<IExperimentService>();
8077
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(true);
8178
applicationEnvironment = mock<IApplicationEnvironment>();
@@ -95,7 +92,6 @@ suite('Terminal Environment Variable Collection Service', () => {
9592
pythonPath: displayPath,
9693
} as unknown) as IPythonSettings);
9794
when(collection.clear()).thenResolve();
98-
when(scopedCollection.clear()).thenResolve();
9995
terminalEnvVarCollectionService = new TerminalEnvVarCollectionService(
10096
instance(platform),
10197
instance(interpreterService),
@@ -253,15 +249,17 @@ suite('Terminal Environment Variable Collection Service', () => {
253249
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, customShell),
254250
).thenResolve(envVars);
255251

256-
when(scopedCollection.replace(anything(), anything(), anything())).thenCall((_e, _v, options) => {
257-
assert.deepEqual(options, { applyAtShellIntegration: true });
258-
return Promise.resolve();
259-
});
252+
when(collection.replace(anything(), anything(), anything())).thenCall(
253+
(_e, _v, options: EnvironmentVariableMutatorOptions) => {
254+
assert.deepEqual(options, { applyAtShellIntegration: true, applyAtProcessCreation: true });
255+
return Promise.resolve();
256+
},
257+
);
260258

261259
await terminalEnvVarCollectionService._applyCollection(resource, customShell);
262260

263-
verify(scopedCollection.clear()).once();
264-
verify(scopedCollection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
261+
verify(collection.clear()).once();
262+
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
265263
});
266264

267265
test('If no activated variables are returned for custom shell, fallback to using default shell', async () => {

types/vscode.proposed.envCollectionWorkspace.d.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@ declare module 'vscode' {
77

88
// https://github.com/microsoft/vscode/issues/182069
99

10-
// export interface ExtensionContext {
11-
// /**
12-
// * Gets the extension's environment variable collection for this workspace, enabling changes
13-
// * to be applied to terminal environment variables.
14-
// *
15-
// * @param scope The scope to which the environment variable collection applies to.
16-
// */
17-
// readonly environmentVariableCollection: EnvironmentVariableCollection & { getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection };
18-
// }
10+
export interface ExtensionContext {
11+
/**
12+
* Gets the extension's environment variable collection for this workspace, enabling changes
13+
* to be applied to terminal environment variables.
14+
*
15+
* @deprecated Use {@link getEnvironmentVariableCollection} instead.
16+
*/
17+
readonly environmentVariableCollection: EnvironmentVariableCollection;
18+
/**
19+
* Gets the extension's environment variable collection for this workspace, enabling changes
20+
* to be applied to terminal environment variables.
21+
*
22+
* @param scope The scope to which the environment variable collection applies to.
23+
*/
24+
getEnvironmentVariableCollection(scope?: EnvironmentVariableScope): EnvironmentVariableCollection;
25+
}
1926

2027
export type EnvironmentVariableScope = {
2128
/**
2229
* Any specific workspace folder to get collection for. If unspecified, collection applicable to all workspace folders is returned.
2330
*/
2431
workspaceFolder?: WorkspaceFolder;
2532
};
26-
27-
export interface EnvironmentVariableCollection extends Iterable<[variable: string, mutator: EnvironmentVariableMutator]> {
28-
/**
29-
* A description for the environment variable collection, this will be used to describe the
30-
* changes in the UI.
31-
*/
32-
description: string | MarkdownString | undefined;
33-
}
3433
}

0 commit comments

Comments
 (0)