Skip to content

Commit

Permalink
Only use activated environment from terminal if VSCode was launched v…
Browse files Browse the repository at this point in the history
…ia CLI (#20667)

Closes #20644
  • Loading branch information
Kartik Raj authored Feb 9, 2023
1 parent 02a92fc commit b0ab10d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,7 @@ suite('Activated Env Launch', async () => {
processServiceFactory = TypeMoq.Mock.ofType<IProcessServiceFactory>();
_promptIfApplicable = sinon.stub(ActivatedEnvironmentLaunch.prototype, '_promptIfApplicable');
_promptIfApplicable.returns(Promise.resolve());
process.env.VSCODE_CLI = '1';
});

teardown(() => {
Expand All @@ -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();
});

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit b0ab10d

Please sign in to comment.