Skip to content

Commit e4bf75b

Browse files
Kartik Rajanthonykim1
Kartik Raj
authored andcommitted
Wrap env collection workspace proposed APIs in try...catch block (microsoft#21846)
Closes microsoft#21831
1 parent de32e58 commit e4bf75b

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"theme": "dark"
4747
},
4848
"engines": {
49-
"vscode": "^1.82.0-20230809"
49+
"vscode": "^1.81.0-20230809"
5050
},
5151
"enableTelemetry": false,
5252
"keywords": [

src/client/interpreter/activation/terminalEnvVarCollectionService.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from '../../common/types';
2121
import { Deferred, createDeferred } from '../../common/utils/async';
2222
import { Interpreters } from '../../common/utils/localize';
23-
import { traceDecoratorVerbose, traceVerbose } from '../../logging';
23+
import { traceDecoratorVerbose, traceVerbose, traceWarn } from '../../logging';
2424
import { IInterpreterService } from '../contracts';
2525
import { defaultShells } from './service';
2626
import { IEnvironmentActivationService, ITerminalEnvVarCollectionService } from './types';
@@ -62,8 +62,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
6262

6363
public async activate(resource: Resource): Promise<void> {
6464
if (!inTerminalEnvVarExperiment(this.experimentService)) {
65-
const workspaceFolder = this.getWorkspaceFolder(resource);
66-
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
65+
this.context.environmentVariableCollection.clear();
6766
await this.handleMicroVenv(resource);
6867
if (!this.registeredOnce) {
6968
this.interpreterService.onDidChangeInterpreter(
@@ -227,22 +226,26 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
227226
}
228227

229228
private async handleMicroVenv(resource: Resource) {
230-
const workspaceFolder = this.getWorkspaceFolder(resource);
231-
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
232-
if (interpreter?.envType === EnvironmentType.Venv) {
233-
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
234-
if (!(await pathExists(activatePath))) {
235-
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
236-
const pathVarName = getSearchPathEnvVarNames()[0];
237-
envVarCollection.replace(
238-
'PATH',
239-
`${path.dirname(interpreter.path)}${path.delimiter}${process.env[pathVarName]}`,
240-
{ applyAtShellIntegration: true, applyAtProcessCreation: true },
241-
);
242-
return;
229+
try {
230+
const workspaceFolder = this.getWorkspaceFolder(resource);
231+
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
232+
if (interpreter?.envType === EnvironmentType.Venv) {
233+
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
234+
if (!(await pathExists(activatePath))) {
235+
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
236+
const pathVarName = getSearchPathEnvVarNames()[0];
237+
envVarCollection.replace(
238+
'PATH',
239+
`${path.dirname(interpreter.path)}${path.delimiter}${process.env[pathVarName]}`,
240+
{ applyAtShellIntegration: true, applyAtProcessCreation: true },
241+
);
242+
return;
243+
}
244+
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
243245
}
246+
} catch (ex) {
247+
traceWarn(`Microvenv failed as it is using proposed API which is constantly changing`, ex);
244248
}
245-
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
246249
}
247250

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ suite('Terminal Environment Variable Collection Service', () => {
124124

125125
test('When not in experiment, do not apply activated variables to the collection and clear it instead', async () => {
126126
reset(experimentService);
127+
when(context.environmentVariableCollection).thenReturn(instance(collection));
127128
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(false);
128129
const applyCollectionStub = sinon.stub(terminalEnvVarCollectionService, '_applyCollection');
129130
applyCollectionStub.resolves();

0 commit comments

Comments
 (0)