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

Only use activated environment from terminal if VSCode was launched via CLI #20667

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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