Skip to content

Apply environment variables after shell initialization scripts are run in pythonTerminalEnvVarActivation experiment #21290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 26, 2023
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
Kartik Raj committed May 23, 2023
commit 6f6a1211db5cb495b73a796a048453d1d76ea90d
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
undefined,
shell,
);
const envVarCollection = this.getEnvironmentVariableCollection({ workspaceFolder });
const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder);
if (!env) {
const shellType = identifyShellFromShellPath(shell);
const defaultShell = defaultShells[this.platform.osType];
Expand Down Expand Up @@ -156,11 +156,13 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
envVarCollection.description = description;
}

private getEnvironmentVariableCollection(scope?: EnvironmentVariableScope) {
private getEnvironmentVariableCollection(workspaceFolder?: WorkspaceFolder) {
const envVarCollection = this.context.environmentVariableCollection as EnvironmentVariableCollection & {
getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection;
};
return scope ? envVarCollection.getScopedEnvironmentVariableCollection(scope) : envVarCollection;
return workspaceFolder
? envVarCollection.getScopedEnvironmentVariableCollection({ workspaceFolder })
: envVarCollection;
}

private async handleMicroVenv(resource: Resource) {
Expand All @@ -169,7 +171,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
if (interpreter?.envType === EnvironmentType.Venv) {
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
if (!(await pathExists(activatePath))) {
const envVarCollection = this.getEnvironmentVariableCollection({ workspaceFolder });
const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder);
envVarCollection.replace(
'PATH',
`${path.dirname(interpreter.path)}${path.delimiter}${process.env.Path}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ suite('Terminal Environment Variable Collection Service', () => {
verify(collection.delete('PATH')).never();
});

test('Verify correct scope is used when applying envs and setting description', async () => {
test('Verify correct options are used when applying envs and setting description', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ..._normCaseKeys(process.env) };
delete envVars.PATH;
const resource = Uri.file('a');
Expand All @@ -229,14 +229,11 @@ suite('Terminal Environment Variable Collection Service', () => {
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, customShell),
).thenResolve(envVars);

when(scopedCollection.replace(anything(), anything(), anything())).thenCall((_e, _v, scope) => {
assert.deepEqual(scope, { workspaceFolder });
return Promise.resolve();
});
when(scopedCollection.delete(anything())).thenCall((_e, scope) => {
assert.deepEqual(scope, { workspaceFolder });
when(scopedCollection.replace(anything(), anything(), anything())).thenCall((_e, _v, options) => {
assert.deepEqual(options, { applyAtShellIntegration: true });
return Promise.resolve();
});
when(scopedCollection.delete(anything())).thenResolve();

await terminalEnvVarCollectionService._applyCollection(resource, customShell);

Expand Down