Skip to content

Commit

Permalink
Wrap env collection workspace proposed APIs in try...catch block (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj authored Aug 18, 2023
1 parent c979455 commit 8407e9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"theme": "dark"
},
"engines": {
"vscode": "^1.82.0-20230809"
"vscode": "^1.81.0-20230809"
},
"enableTelemetry": false,
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../../common/types';
import { Deferred, createDeferred } from '../../common/utils/async';
import { Interpreters } from '../../common/utils/localize';
import { traceDecoratorVerbose, traceVerbose } from '../../logging';
import { traceDecoratorVerbose, traceVerbose, traceWarn } from '../../logging';
import { IInterpreterService } from '../contracts';
import { defaultShells } from './service';
import { IEnvironmentActivationService, ITerminalEnvVarCollectionService } from './types';
Expand Down Expand Up @@ -62,8 +62,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ

public async activate(resource: Resource): Promise<void> {
if (!inTerminalEnvVarExperiment(this.experimentService)) {
const workspaceFolder = this.getWorkspaceFolder(resource);
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
this.context.environmentVariableCollection.clear();
await this.handleMicroVenv(resource);
if (!this.registeredOnce) {
this.interpreterService.onDidChangeInterpreter(
Expand Down Expand Up @@ -227,22 +226,26 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
}

private async handleMicroVenv(resource: Resource) {
const workspaceFolder = this.getWorkspaceFolder(resource);
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
if (interpreter?.envType === EnvironmentType.Venv) {
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
if (!(await pathExists(activatePath))) {
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
const pathVarName = getSearchPathEnvVarNames()[0];
envVarCollection.replace(
'PATH',
`${path.dirname(interpreter.path)}${path.delimiter}${process.env[pathVarName]}`,
{ applyAtShellIntegration: true, applyAtProcessCreation: true },
);
return;
try {
const workspaceFolder = this.getWorkspaceFolder(resource);
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
if (interpreter?.envType === EnvironmentType.Venv) {
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
if (!(await pathExists(activatePath))) {
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
const pathVarName = getSearchPathEnvVarNames()[0];
envVarCollection.replace(
'PATH',
`${path.dirname(interpreter.path)}${path.delimiter}${process.env[pathVarName]}`,
{ applyAtShellIntegration: true, applyAtProcessCreation: true },
);
return;
}
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
}
} catch (ex) {
traceWarn(`Microvenv failed as it is using proposed API which is constantly changing`, ex);
}
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
}

private getWorkspaceFolder(resource: Resource): WorkspaceFolder | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ suite('Terminal Environment Variable Collection Service', () => {

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

0 comments on commit 8407e9d

Please sign in to comment.