diff --git a/src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts b/src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts index 01b4829df4ac..468c2dc72a01 100644 --- a/src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts +++ b/src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts @@ -9,7 +9,7 @@ import { IProcessServiceFactory } from '../../common/process/types'; import { sleep } from '../../common/utils/async'; import { cache } from '../../common/utils/decorators'; import { Common, Interpreters } from '../../common/utils/localize'; -import { traceError, traceLog, traceWarn } from '../../logging'; +import { traceError, traceLog, traceVerbose, traceWarn } from '../../logging'; import { Conda } from '../../pythonEnvironments/common/environmentManagers/conda'; import { sendTelemetryEvent } from '../../telemetry'; import { EventName } from '../../telemetry/constants'; @@ -89,6 +89,11 @@ export class ActivatedEnvironmentLaunch implements IActivatedEnvironmentLaunch { // Assuming multiroot workspaces cannot be directly launched via `code .` command. return undefined; } + if (process.env.VSCODE_CLI !== '1') { + // We only want to select the interpreter if VS Code was launched from the command line. + traceVerbose('VS Code was not launched from the command line, not selecting activated interpreter'); + return undefined; + } const prefix = await this.getPrefixOfSelectedActivatedEnv(); if (!prefix) { this._promptIfApplicable().ignoreErrors(); diff --git a/src/test/interpreters/virtualEnvs/activatedEnvLaunch.unit.test.ts b/src/test/interpreters/virtualEnvs/activatedEnvLaunch.unit.test.ts index 04a5d3c95de1..2ebdecd000d1 100644 --- a/src/test/interpreters/virtualEnvs/activatedEnvLaunch.unit.test.ts +++ b/src/test/interpreters/virtualEnvs/activatedEnvLaunch.unit.test.ts @@ -29,7 +29,8 @@ suite('Activated Env Launch', async () => { let activatedEnvLaunch: ActivatedEnvironmentLaunch; let _promptIfApplicable: sinon.SinonStub; - suite('Method getPrefixOfSelectedActivatedEnv()', () => { + suite('Method selectIfLaunchedViaActivatedEnv()', () => { + const oldVSCodeCLI = process.env.VSCODE_CLI; const oldCondaPrefix = process.env.CONDA_PREFIX; const oldCondaShlvl = process.env.CONDA_SHLVL; const oldVirtualEnv = process.env.VIRTUAL_ENV; @@ -41,6 +42,7 @@ suite('Activated Env Launch', async () => { processServiceFactory = TypeMoq.Mock.ofType(); _promptIfApplicable = sinon.stub(ActivatedEnvironmentLaunch.prototype, '_promptIfApplicable'); _promptIfApplicable.returns(Promise.resolve()); + process.env.VSCODE_CLI = '1'; }); teardown(() => { @@ -59,6 +61,11 @@ suite('Activated Env Launch', async () => { } else { delete process.env.VIRTUAL_ENV; } + if (oldVSCodeCLI) { + process.env.VSCODE_CLI = oldVSCodeCLI; + } else { + delete process.env.VSCODE_CLI; + } sinon.restore(); }); @@ -94,6 +101,39 @@ suite('Activated Env Launch', async () => { pythonPathUpdaterService.verifyAll(); }); + test('Does not update interpreter path if VSCode is not launched via CLI', async () => { + delete process.env.VSCODE_CLI; + process.env.CONDA_PREFIX = condaPrefix; + process.env.CONDA_SHLVL = '1'; + interpreterService + .setup((i) => i.getInterpreterDetails(TypeMoq.It.isAny())) + .returns(() => Promise.resolve(({ envName: 'env' } as unknown) as PythonEnvironment)); + workspaceService.setup((w) => w.workspaceFile).returns(() => undefined); + const workspaceFolder: WorkspaceFolder = { name: 'one', uri, index: 0 }; + workspaceService.setup((w) => w.workspaceFolders).returns(() => [workspaceFolder]); + pythonPathUpdaterService + .setup((p) => + p.updatePythonPath( + TypeMoq.It.isValue(condaPrefix), + TypeMoq.It.isValue(ConfigurationTarget.WorkspaceFolder), + TypeMoq.It.isValue('load'), + TypeMoq.It.isValue(uri), + ), + ) + .returns(() => Promise.resolve()) + .verifiable(TypeMoq.Times.never()); + activatedEnvLaunch = new ActivatedEnvironmentLaunch( + workspaceService.object, + appShell.object, + pythonPathUpdaterService.object, + interpreterService.object, + processServiceFactory.object, + ); + const result = await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv(); + expect(result).to.be.equal(undefined, 'Incorrect value'); + pythonPathUpdaterService.verifyAll(); + }); + test('Updates interpreter path with the base conda prefix if activated and environment var is configured to not auto activate it', async () => { process.env.CONDA_PREFIX = condaPrefix; process.env.CONDA_SHLVL = '1';