Skip to content

Commit b0ab10d

Browse files
author
Kartik Raj
authored
Only use activated environment from terminal if VSCode was launched via CLI (#20667)
Closes #20644
1 parent 02a92fc commit b0ab10d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IProcessServiceFactory } from '../../common/process/types';
99
import { sleep } from '../../common/utils/async';
1010
import { cache } from '../../common/utils/decorators';
1111
import { Common, Interpreters } from '../../common/utils/localize';
12-
import { traceError, traceLog, traceWarn } from '../../logging';
12+
import { traceError, traceLog, traceVerbose, traceWarn } from '../../logging';
1313
import { Conda } from '../../pythonEnvironments/common/environmentManagers/conda';
1414
import { sendTelemetryEvent } from '../../telemetry';
1515
import { EventName } from '../../telemetry/constants';
@@ -89,6 +89,11 @@ export class ActivatedEnvironmentLaunch implements IActivatedEnvironmentLaunch {
8989
// Assuming multiroot workspaces cannot be directly launched via `code .` command.
9090
return undefined;
9191
}
92+
if (process.env.VSCODE_CLI !== '1') {
93+
// We only want to select the interpreter if VS Code was launched from the command line.
94+
traceVerbose('VS Code was not launched from the command line, not selecting activated interpreter');
95+
return undefined;
96+
}
9297
const prefix = await this.getPrefixOfSelectedActivatedEnv();
9398
if (!prefix) {
9499
this._promptIfApplicable().ignoreErrors();

src/test/interpreters/virtualEnvs/activatedEnvLaunch.unit.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ suite('Activated Env Launch', async () => {
2929
let activatedEnvLaunch: ActivatedEnvironmentLaunch;
3030
let _promptIfApplicable: sinon.SinonStub;
3131

32-
suite('Method getPrefixOfSelectedActivatedEnv()', () => {
32+
suite('Method selectIfLaunchedViaActivatedEnv()', () => {
33+
const oldVSCodeCLI = process.env.VSCODE_CLI;
3334
const oldCondaPrefix = process.env.CONDA_PREFIX;
3435
const oldCondaShlvl = process.env.CONDA_SHLVL;
3536
const oldVirtualEnv = process.env.VIRTUAL_ENV;
@@ -41,6 +42,7 @@ suite('Activated Env Launch', async () => {
4142
processServiceFactory = TypeMoq.Mock.ofType<IProcessServiceFactory>();
4243
_promptIfApplicable = sinon.stub(ActivatedEnvironmentLaunch.prototype, '_promptIfApplicable');
4344
_promptIfApplicable.returns(Promise.resolve());
45+
process.env.VSCODE_CLI = '1';
4446
});
4547

4648
teardown(() => {
@@ -59,6 +61,11 @@ suite('Activated Env Launch', async () => {
5961
} else {
6062
delete process.env.VIRTUAL_ENV;
6163
}
64+
if (oldVSCodeCLI) {
65+
process.env.VSCODE_CLI = oldVSCodeCLI;
66+
} else {
67+
delete process.env.VSCODE_CLI;
68+
}
6269
sinon.restore();
6370
});
6471

@@ -94,6 +101,39 @@ suite('Activated Env Launch', async () => {
94101
pythonPathUpdaterService.verifyAll();
95102
});
96103

104+
test('Does not update interpreter path if VSCode is not launched via CLI', async () => {
105+
delete process.env.VSCODE_CLI;
106+
process.env.CONDA_PREFIX = condaPrefix;
107+
process.env.CONDA_SHLVL = '1';
108+
interpreterService
109+
.setup((i) => i.getInterpreterDetails(TypeMoq.It.isAny()))
110+
.returns(() => Promise.resolve(({ envName: 'env' } as unknown) as PythonEnvironment));
111+
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
112+
const workspaceFolder: WorkspaceFolder = { name: 'one', uri, index: 0 };
113+
workspaceService.setup((w) => w.workspaceFolders).returns(() => [workspaceFolder]);
114+
pythonPathUpdaterService
115+
.setup((p) =>
116+
p.updatePythonPath(
117+
TypeMoq.It.isValue(condaPrefix),
118+
TypeMoq.It.isValue(ConfigurationTarget.WorkspaceFolder),
119+
TypeMoq.It.isValue('load'),
120+
TypeMoq.It.isValue(uri),
121+
),
122+
)
123+
.returns(() => Promise.resolve())
124+
.verifiable(TypeMoq.Times.never());
125+
activatedEnvLaunch = new ActivatedEnvironmentLaunch(
126+
workspaceService.object,
127+
appShell.object,
128+
pythonPathUpdaterService.object,
129+
interpreterService.object,
130+
processServiceFactory.object,
131+
);
132+
const result = await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv();
133+
expect(result).to.be.equal(undefined, 'Incorrect value');
134+
pythonPathUpdaterService.verifyAll();
135+
});
136+
97137
test('Updates interpreter path with the base conda prefix if activated and environment var is configured to not auto activate it', async () => {
98138
process.env.CONDA_PREFIX = condaPrefix;
99139
process.env.CONDA_SHLVL = '1';

0 commit comments

Comments
 (0)