From 74a5cad0f3390d83eeb8b75b42c6531acde23917 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Fri, 10 Jan 2025 14:53:30 -0800 Subject: [PATCH] Restrict conda binary to be from PATH or Settings (#24709) Closes https://github.com/microsoft/vscode-python/issues/24627 --------- Co-authored-by: Eleanor Boyd --- src/client/common/utils/platform.ts | 8 ++++ .../common/environmentManagers/conda.ts | 6 +++ src/client/pythonEnvironments/nativeAPI.ts | 37 +++++++++++++++++-- .../pythonEnvironments/nativeAPI.unit.test.ts | 11 +++++- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/client/common/utils/platform.ts b/src/client/common/utils/platform.ts index c86f5ff9364e..a1a49ba3c427 100644 --- a/src/client/common/utils/platform.ts +++ b/src/client/common/utils/platform.ts @@ -71,3 +71,11 @@ export function getUserHomeDir(): string | undefined { export function isWindows(): boolean { return getOSType() === OSType.Windows; } + +export function getPathEnvVariable(): string[] { + const value = getEnvironmentVariable('PATH') || getEnvironmentVariable('Path'); + if (value) { + return value.split(isWindows() ? ';' : ':'); + } + return []; +} diff --git a/src/client/pythonEnvironments/common/environmentManagers/conda.ts b/src/client/pythonEnvironments/common/environmentManagers/conda.ts index bc60745dfeff..5301f82eda18 100644 --- a/src/client/pythonEnvironments/common/environmentManagers/conda.ts +++ b/src/client/pythonEnvironments/common/environmentManagers/conda.ts @@ -24,6 +24,7 @@ import { OUTPUT_MARKER_SCRIPT } from '../../../common/process/internal/scripts'; import { splitLines } from '../../../common/stringUtils'; import { SpawnOptions } from '../../../common/process/types'; import { sleep } from '../../../common/utils/async'; +import { getConfiguration } from '../../../common/vscodeApis/workspaceApis'; export const AnacondaCompanyName = 'Anaconda, Inc.'; export const CONDAPATH_SETTING_KEY = 'condaPath'; @@ -633,3 +634,8 @@ export async function getCondaEnvDirs(): Promise { const conda = await Conda.getConda(); return conda?.getEnvDirs(); } + +export function getCondaPathSetting(): string | undefined { + const config = getConfiguration('python'); + return config.get(CONDAPATH_SETTING_KEY, ''); +} diff --git a/src/client/pythonEnvironments/nativeAPI.ts b/src/client/pythonEnvironments/nativeAPI.ts index e069a3746ab6..a4a706fcb42b 100644 --- a/src/client/pythonEnvironments/nativeAPI.ts +++ b/src/client/pythonEnvironments/nativeAPI.ts @@ -20,14 +20,14 @@ import { NativePythonFinder, } from './base/locators/common/nativePythonFinder'; import { createDeferred, Deferred } from '../common/utils/async'; -import { Architecture, getUserHomeDir } from '../common/utils/platform'; +import { Architecture, getPathEnvVariable, getUserHomeDir } from '../common/utils/platform'; import { parseVersion } from './base/info/pythonVersion'; import { cache } from '../common/utils/decorators'; import { traceError, traceInfo, traceLog, traceWarn } from '../logging'; import { StopWatch } from '../common/utils/stopWatch'; import { FileChangeType } from '../common/platform/fileSystemWatcher'; import { categoryToKind, NativePythonEnvironmentKind } from './base/locators/common/nativePythonUtils'; -import { getCondaEnvDirs, setCondaBinary } from './common/environmentManagers/conda'; +import { getCondaEnvDirs, getCondaPathSetting, setCondaBinary } from './common/environmentManagers/conda'; import { setPyEnvBinary } from './common/environmentManagers/pyenv'; import { createPythonWatcher, @@ -166,6 +166,12 @@ function isSubDir(pathToCheck: string | undefined, parents: string[]): boolean { }); } +function foundOnPath(fsPath: string): boolean { + const paths = getPathEnvVariable().map((p) => path.normalize(p).toLowerCase()); + const normalized = path.normalize(fsPath).toLowerCase(); + return paths.some((p) => normalized.includes(p)); +} + function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: string[]): string { if (nativeEnv.name) { return nativeEnv.name; @@ -387,13 +393,36 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable { return undefined; } + private condaPathAlreadySet: string | undefined; + // eslint-disable-next-line class-methods-use-this private processEnvManager(native: NativeEnvManagerInfo) { const tool = native.tool.toLowerCase(); switch (tool) { case 'conda': - traceLog(`Conda environment manager found at: ${native.executable}`); - setCondaBinary(native.executable); + { + traceLog(`Conda environment manager found at: ${native.executable}`); + const settingPath = getCondaPathSetting(); + if (!this.condaPathAlreadySet) { + if (settingPath === '' || settingPath === undefined) { + if (foundOnPath(native.executable)) { + setCondaBinary(native.executable); + this.condaPathAlreadySet = native.executable; + traceInfo(`Using conda: ${native.executable}`); + } else { + traceInfo(`Conda not found on PATH, skipping: ${native.executable}`); + traceInfo( + 'You can set the path to conda using the setting: `python.condaPath` if you want to use a different conda binary', + ); + } + } else { + traceInfo(`Using conda from setting: ${settingPath}`); + this.condaPathAlreadySet = settingPath; + } + } else { + traceInfo(`Conda set to: ${this.condaPathAlreadySet}`); + } + } break; case 'pyenv': traceLog(`Pyenv environment manager found at: ${native.executable}`); diff --git a/src/test/pythonEnvironments/nativeAPI.unit.test.ts b/src/test/pythonEnvironments/nativeAPI.unit.test.ts index 678a8fcfe2e3..74811fa63bb6 100644 --- a/src/test/pythonEnvironments/nativeAPI.unit.test.ts +++ b/src/test/pythonEnvironments/nativeAPI.unit.test.ts @@ -13,7 +13,7 @@ import { NativeEnvManagerInfo, NativePythonFinder, } from '../../client/pythonEnvironments/base/locators/common/nativePythonFinder'; -import { Architecture, isWindows } from '../../client/common/utils/platform'; +import { Architecture, getPathEnvVariable, isWindows } from '../../client/common/utils/platform'; import { PythonEnvInfo, PythonEnvKind, PythonEnvType } from '../../client/pythonEnvironments/base/info'; import { NativePythonEnvironmentKind } from '../../client/pythonEnvironments/base/locators/common/nativePythonUtils'; import * as condaApi from '../../client/pythonEnvironments/common/environmentManagers/conda'; @@ -25,6 +25,8 @@ suite('Native Python API', () => { let api: IDiscoveryAPI; let mockFinder: typemoq.IMock; let setCondaBinaryStub: sinon.SinonStub; + let getCondaPathSettingStub: sinon.SinonStub; + let getCondaEnvDirsStub: sinon.SinonStub; let setPyEnvBinaryStub: sinon.SinonStub; let createPythonWatcherStub: sinon.SinonStub; let mockWatcher: typemoq.IMock; @@ -136,6 +138,8 @@ suite('Native Python API', () => { setup(() => { setCondaBinaryStub = sinon.stub(condaApi, 'setCondaBinary'); + getCondaEnvDirsStub = sinon.stub(condaApi, 'getCondaEnvDirs'); + getCondaPathSettingStub = sinon.stub(condaApi, 'getCondaPathSetting'); setPyEnvBinaryStub = sinon.stub(pyenvApi, 'setPyEnvBinary'); getWorkspaceFoldersStub = sinon.stub(ws, 'getWorkspaceFolders'); getWorkspaceFoldersStub.returns([]); @@ -294,9 +298,12 @@ suite('Native Python API', () => { }); test('Setting conda binary', async () => { + getCondaPathSettingStub.returns(undefined); + getCondaEnvDirsStub.resolves(undefined); + const condaFakeDir = getPathEnvVariable()[0]; const condaMgr: NativeEnvManagerInfo = { tool: 'Conda', - executable: '/usr/bin/conda', + executable: path.join(condaFakeDir, 'conda'), }; mockFinder .setup((f) => f.refresh())