Skip to content
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

Always prepend to PATH instead of replacing it #21906

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
if (shouldSkip(key)) {
return;
}
const value = env[key];
let value = env[key];
const prevValue = processEnv[key];
if (prevValue !== value) {
if (value !== undefined) {
Expand All @@ -180,6 +180,20 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
});
return;
}
if (key === 'PATH') {
if (processEnv.PATH && env.PATH?.endsWith(processEnv.PATH)) {
// Prefer prepending to PATH instead of replacing it, as we do not want to replace any
// changes to PATH users might have made it in their init scripts (~/.bashrc etc.)
const prependedPart = env.PATH.slice(0, -processEnv.PATH.length);
value = prependedPart;
traceVerbose(`Prepending environment variable ${key} in collection with ${value}`);
envVarCollection.prepend(key, value, {
applyAtShellIntegration: true,
applyAtProcessCreation: true,
});
return;
}
}
traceVerbose(`Setting environment variable ${key} in collection to ${value}`);
envVarCollection.replace(key, value, {
applyAtShellIntegration: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,38 @@ suite('Terminal Environment Variable Collection Service', () => {
assert.deepEqual(opts, { applyAtProcessCreation: false, applyAtShellIntegration: true });
});

test('Prepend PATH instead of replacing it, where possible', async () => {
const processEnv = { PATH: 'hello/1/2/3' };
reset(environmentActivationService);
when(environmentActivationService.getProcessEnvironmentVariables(anything(), anything())).thenResolve(
processEnv,
);
const prependedPart = 'path/to/activate/dir:';
const envVars: NodeJS.ProcessEnv = { PATH: `${prependedPart}${processEnv.PATH}` };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);

when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
let opts: EnvironmentVariableMutatorOptions | undefined;
when(collection.prepend('PATH', anything(), anything())).thenCall((_, _v, o) => {
opts = o;
});

await terminalEnvVarCollectionService._applyCollection(undefined, customShell);

verify(collection.clear()).once();
verify(collection.prepend('PATH', prependedPart, anything())).once();
verify(collection.replace('PATH', anything(), anything())).never();
assert.deepEqual(opts, { applyAtProcessCreation: true, applyAtShellIntegration: true });
});

test('Verify envs are not applied if env activation is disabled', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
when(
Expand Down