From e78934859fba927231bfdb2673cece754169ecd8 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 10 Dec 2024 13:54:59 +0530 Subject: [PATCH] Use env extension when available (#24564) --- src/client/common/persistentState.ts | 4 + src/client/common/terminal/activator/index.ts | 3 +- src/client/common/terminal/service.ts | 38 +- src/client/envExt/api.internal.ts | 108 ++ src/client/envExt/api.legacy.ts | 167 +++ src/client/envExt/envExtApi.ts | 327 +++++ src/client/envExt/types.ts | 1233 +++++++++++++++++ .../commands/resetInterpreter.ts | 5 + .../commands/setInterpreter.ts | 5 + src/client/interpreter/display/index.ts | 10 + src/client/interpreter/interpreterService.ts | 16 + src/client/providers/terminalProvider.ts | 4 +- .../creation/createEnvApi.ts | 25 +- src/client/pythonEnvironments/index.ts | 13 + .../codeExecution/codeExecutionManager.ts | 53 +- .../indicatorPrompt.ts | 3 +- .../envCollectionActivation/service.ts | 12 + .../pytest/pytestDiscoveryAdapter.ts | 45 +- .../pytest/pytestExecutionAdapter.ts | 50 +- .../unittest/testDiscoveryAdapter.ts | 61 +- .../unittest/testExecutionAdapter.ts | 42 + .../terminals/activator/index.unit.test.ts | 10 + .../common/terminals/service.unit.test.ts | 5 + .../commands/resetInterpreter.unit.test.ts | 9 + .../commands/setInterpreter.unit.test.ts | 5 + .../activation/indicatorPrompt.unit.test.ts | 10 + ...rminalEnvVarCollectionService.unit.test.ts | 5 + src/test/interpreters/display.unit.test.ts | 5 + .../interpreterService.unit.test.ts | 5 + src/test/providers/terminal.unit.test.ts | 7 + .../codeExecutionManager.unit.test.ts | 5 + .../pytestDiscoveryAdapter.unit.test.ts | 5 + .../pytestExecutionAdapter.unit.test.ts | 5 + .../testCancellationRunAdapters.unit.test.ts | 5 + .../testDiscoveryAdapter.unit.test.ts | 5 + .../testExecutionAdapter.unit.test.ts | 4 + 36 files changed, 2272 insertions(+), 42 deletions(-) create mode 100644 src/client/envExt/api.internal.ts create mode 100644 src/client/envExt/api.legacy.ts create mode 100644 src/client/envExt/envExtApi.ts create mode 100644 src/client/envExt/types.ts diff --git a/src/client/common/persistentState.ts b/src/client/common/persistentState.ts index ab30ac5d611c..3f9c17657cf4 100644 --- a/src/client/common/persistentState.ts +++ b/src/client/common/persistentState.ts @@ -20,6 +20,7 @@ import { import { cache } from './utils/decorators'; import { noop } from './utils/misc'; import { clearCacheDirectory } from '../pythonEnvironments/base/locators/common/nativePythonFinder'; +import { clearCache, useEnvExtension } from '../envExt/api.internal'; let _workspaceState: Memento | undefined; const _workspaceKeys: string[] = []; @@ -134,6 +135,9 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi this.cmdManager?.registerCommand(Commands.ClearStorage, async () => { await clearWorkspaceState(); await this.cleanAllPersistentStates(); + if (useEnvExtension()) { + await clearCache(); + } }); const globalKeysStorageDeprecated = this.createGlobalPersistentState(GLOBAL_PERSISTENT_KEYS_DEPRECATED, []); const workspaceKeysStorageDeprecated = this.createWorkspacePersistentState( diff --git a/src/client/common/terminal/activator/index.ts b/src/client/common/terminal/activator/index.ts index 1c2cf4041585..6501688b548a 100644 --- a/src/client/common/terminal/activator/index.ts +++ b/src/client/common/terminal/activator/index.ts @@ -9,6 +9,7 @@ import { IConfigurationService, IExperimentService } from '../../types'; import { ITerminalActivationHandler, ITerminalActivator, ITerminalHelper, TerminalActivationOptions } from '../types'; import { BaseTerminalActivator } from './base'; import { inTerminalEnvVarExperiment } from '../../experiments/helpers'; +import { useEnvExtension } from '../../../envExt/api.internal'; @injectable() export class TerminalActivator implements ITerminalActivator { @@ -41,7 +42,7 @@ export class TerminalActivator implements ITerminalActivator { const settings = this.configurationService.getSettings(options?.resource); const activateEnvironment = settings.terminal.activateEnvironment && !inTerminalEnvVarExperiment(this.experimentService); - if (!activateEnvironment || options?.hideFromUser) { + if (!activateEnvironment || options?.hideFromUser || useEnvExtension()) { return false; } diff --git a/src/client/common/terminal/service.ts b/src/client/common/terminal/service.ts index e37539f1bc7c..b02670836015 100644 --- a/src/client/common/terminal/service.ts +++ b/src/client/common/terminal/service.ts @@ -21,6 +21,9 @@ import { } from './types'; import { traceVerbose } from '../../logging'; import { getConfiguration } from '../vscodeApis/workspaceApis'; +import { useEnvExtension } from '../../envExt/api.internal'; +import { ensureTerminalLegacy } from '../../envExt/api.legacy'; +import { sleep } from '../utils/async'; import { isWindows } from '../utils/platform'; @injectable() @@ -132,22 +135,29 @@ export class TerminalService implements ITerminalService, Disposable { if (this.terminal) { return; } - this.terminalShellType = this.terminalHelper.identifyTerminalShell(this.terminal); - this.terminal = this.terminalManager.createTerminal({ - name: this.options?.title || 'Python', - hideFromUser: this.options?.hideFromUser, - }); - this.terminalAutoActivator.disableAutoActivation(this.terminal); - // Sometimes the terminal takes some time to start up before it can start accepting input. - await new Promise((resolve) => setTimeout(resolve, 100)); + if (useEnvExtension()) { + this.terminal = await ensureTerminalLegacy(this.options?.resource, { + name: this.options?.title || 'Python', + hideFromUser: this.options?.hideFromUser, + }); + } else { + this.terminalShellType = this.terminalHelper.identifyTerminalShell(this.terminal); + this.terminal = this.terminalManager.createTerminal({ + name: this.options?.title || 'Python', + hideFromUser: this.options?.hideFromUser, + }); + this.terminalAutoActivator.disableAutoActivation(this.terminal); + + await sleep(100); - await this.terminalActivator.activateEnvironmentInTerminal(this.terminal, { - resource: this.options?.resource, - preserveFocus, - interpreter: this.options?.interpreter, - hideFromUser: this.options?.hideFromUser, - }); + await this.terminalActivator.activateEnvironmentInTerminal(this.terminal, { + resource: this.options?.resource, + preserveFocus, + interpreter: this.options?.interpreter, + hideFromUser: this.options?.hideFromUser, + }); + } if (!this.options?.hideFromUser) { this.terminal.show(preserveFocus); diff --git a/src/client/envExt/api.internal.ts b/src/client/envExt/api.internal.ts new file mode 100644 index 000000000000..a47193d3cd95 --- /dev/null +++ b/src/client/envExt/api.internal.ts @@ -0,0 +1,108 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { Terminal, Uri } from 'vscode'; +import { getExtension } from '../common/vscodeApis/extensionsApi'; +import { + GetEnvironmentScope, + PythonBackgroundRunOptions, + PythonEnvironment, + PythonEnvironmentApi, + PythonProcess, + RefreshEnvironmentsScope, +} from './types'; +import { executeCommand } from '../common/vscodeApis/commandApis'; + +export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs'; + +let _useExt: boolean | undefined; +export function useEnvExtension(): boolean { + if (_useExt !== undefined) { + return _useExt; + } + _useExt = !!getExtension(ENVS_EXTENSION_ID); + return _useExt; +} + +let _extApi: PythonEnvironmentApi | undefined; +export async function getEnvExtApi(): Promise { + if (_extApi) { + return _extApi; + } + const extension = getExtension(ENVS_EXTENSION_ID); + if (!extension) { + throw new Error('Python Environments extension not found.'); + } + if (extension?.isActive) { + _extApi = extension.exports as PythonEnvironmentApi; + return _extApi; + } + + await extension.activate(); + + _extApi = extension.exports as PythonEnvironmentApi; + return _extApi; +} + +export async function runInBackground( + environment: PythonEnvironment, + options: PythonBackgroundRunOptions, +): Promise { + const envExtApi = await getEnvExtApi(); + return envExtApi.runInBackground(environment, options); +} + +export async function getEnvironment(scope: GetEnvironmentScope): Promise { + const envExtApi = await getEnvExtApi(); + return envExtApi.getEnvironment(scope); +} + +export async function refreshEnvironments(scope: RefreshEnvironmentsScope): Promise { + const envExtApi = await getEnvExtApi(); + return envExtApi.refreshEnvironments(scope); +} + +export async function runInTerminal( + resource: Uri | undefined, + args?: string[], + cwd?: string | Uri, + show?: boolean, +): Promise { + const envExtApi = await getEnvExtApi(); + const env = await getEnvironment(resource); + const project = resource ? envExtApi.getPythonProject(resource) : undefined; + if (env && resource) { + return envExtApi.runInTerminal(env, { + cwd: cwd ?? project?.uri ?? process.cwd(), + args, + show, + }); + } + throw new Error('Invalid arguments to run in terminal'); +} + +export async function runInDedicatedTerminal( + resource: Uri | undefined, + args?: string[], + cwd?: string | Uri, + show?: boolean, +): Promise { + const envExtApi = await getEnvExtApi(); + const env = await getEnvironment(resource); + const project = resource ? envExtApi.getPythonProject(resource) : undefined; + if (env) { + return envExtApi.runInDedicatedTerminal(resource ?? 'global', env, { + cwd: cwd ?? project?.uri ?? process.cwd(), + args, + show, + }); + } + throw new Error('Invalid arguments to run in dedicated terminal'); +} + +export async function clearCache(): Promise { + const envExtApi = await getEnvExtApi(); + if (envExtApi) { + await executeCommand('python-envs.clearCache'); + } +} diff --git a/src/client/envExt/api.legacy.ts b/src/client/envExt/api.legacy.ts new file mode 100644 index 000000000000..1d9d94ccc98f --- /dev/null +++ b/src/client/envExt/api.legacy.ts @@ -0,0 +1,167 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { Terminal, Uri } from 'vscode'; +import { getEnvExtApi, getEnvironment } from './api.internal'; +import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info'; +import { PythonEnvironment, PythonTerminalOptions } from './types'; +import { Architecture } from '../common/utils/platform'; +import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion'; +import { PythonEnvType } from '../pythonEnvironments/base/info'; +import { traceError, traceInfo } from '../logging'; +import { reportActiveInterpreterChanged } from '../environmentApi'; +import { getWorkspaceFolder } from '../common/vscodeApis/workspaceApis'; + +function toEnvironmentType(pythonEnv: PythonEnvironment): EnvironmentType { + if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) { + return EnvironmentType.System; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('venv')) { + return EnvironmentType.Venv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenv')) { + return EnvironmentType.VirtualEnv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) { + return EnvironmentType.Conda; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pipenv')) { + return EnvironmentType.Pipenv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('poetry')) { + return EnvironmentType.Poetry; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pyenv')) { + return EnvironmentType.Pyenv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('hatch')) { + return EnvironmentType.Hatch; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pixi')) { + return EnvironmentType.Pixi; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenvwrapper')) { + return EnvironmentType.VirtualEnvWrapper; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('activestate')) { + return EnvironmentType.ActiveState; + } + return EnvironmentType.Unknown; +} + +function getEnvType(kind: EnvironmentType): PythonEnvType | undefined { + switch (kind) { + case EnvironmentType.Pipenv: + case EnvironmentType.VirtualEnv: + case EnvironmentType.Pyenv: + case EnvironmentType.Venv: + case EnvironmentType.Poetry: + case EnvironmentType.Hatch: + case EnvironmentType.Pixi: + case EnvironmentType.VirtualEnvWrapper: + case EnvironmentType.ActiveState: + return PythonEnvType.Virtual; + + case EnvironmentType.Conda: + return PythonEnvType.Conda; + + case EnvironmentType.MicrosoftStore: + case EnvironmentType.Global: + case EnvironmentType.System: + default: + return undefined; + } +} + +function toLegacyType(env: PythonEnvironment): PythonEnvironmentLegacy { + const ver = parseVersion(env.version); + const envType = toEnvironmentType(env); + return { + id: env.environmentPath.fsPath, + displayName: env.displayName, + detailedDisplayName: env.name, + envType, + envPath: env.sysPrefix, + type: getEnvType(envType), + path: env.environmentPath.fsPath, + version: { + raw: env.version, + major: ver.major, + minor: ver.minor, + patch: ver.micro, + build: [], + prerelease: [], + }, + sysVersion: env.version, + architecture: Architecture.x64, + sysPrefix: env.sysPrefix, + }; +} + +const previousEnvMap = new Map(); +export async function getActiveInterpreterLegacy(resource?: Uri): Promise { + const api = await getEnvExtApi(); + const uri = resource ? api.getPythonProject(resource)?.uri : undefined; + + const pythonEnv = await getEnvironment(resource); + const oldEnv = previousEnvMap.get(uri?.fsPath || ''); + const newEnv = pythonEnv ? toLegacyType(pythonEnv) : undefined; + if (newEnv && oldEnv?.envId.id !== pythonEnv?.envId.id) { + reportActiveInterpreterChanged({ + resource: getWorkspaceFolder(resource), + path: newEnv.path, + }); + } + return pythonEnv ? toLegacyType(pythonEnv) : undefined; +} + +export async function ensureEnvironmentContainsPythonLegacy(pythonPath: string): Promise { + const api = await getEnvExtApi(); + const pythonEnv = await api.resolveEnvironment(Uri.file(pythonPath)); + if (!pythonEnv) { + traceError(`EnvExt: Failed to resolve environment for ${pythonPath}`); + return; + } + + const envType = toEnvironmentType(pythonEnv); + if (envType === EnvironmentType.Conda) { + const packages = await api.getPackages(pythonEnv); + if (packages && packages.length > 0 && packages.some((pkg) => pkg.name.toLowerCase() === 'python')) { + return; + } + traceInfo(`EnvExt: Python not found in ${envType} environment ${pythonPath}`); + traceInfo(`EnvExt: Installing Python in ${envType} environment ${pythonPath}`); + await api.installPackages(pythonEnv, ['python']); + } +} + +export async function setInterpreterLegacy(pythonPath: string, uri: Uri | undefined): Promise { + const api = await getEnvExtApi(); + const pythonEnv = await api.resolveEnvironment(Uri.file(pythonPath)); + if (!pythonEnv) { + traceError(`EnvExt: Failed to resolve environment for ${pythonPath}`); + return; + } + await api.setEnvironment(uri, pythonEnv); +} + +export async function resetInterpreterLegacy(uri: Uri | undefined): Promise { + const api = await getEnvExtApi(); + await api.setEnvironment(uri, undefined); +} + +export async function ensureTerminalLegacy( + resource: Uri | undefined, + options?: PythonTerminalOptions, +): Promise { + const api = await getEnvExtApi(); + const pythonEnv = await api.getEnvironment(resource); + const project = resource ? api.getPythonProject(resource) : undefined; + + if (pythonEnv && project) { + const fixedOptions = options ? { ...options } : { cwd: project.uri }; + const terminal = await api.createTerminal(pythonEnv, fixedOptions); + return terminal; + } + throw new Error('Invalid arguments to create terminal'); +} diff --git a/src/client/envExt/envExtApi.ts b/src/client/envExt/envExtApi.ts new file mode 100644 index 000000000000..598899b7d248 --- /dev/null +++ b/src/client/envExt/envExtApi.ts @@ -0,0 +1,327 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/* eslint-disable class-methods-use-this */ + +import * as path from 'path'; +import { Event, EventEmitter, Disposable, Uri } from 'vscode'; +import { PythonEnvInfo, PythonEnvKind, PythonEnvType, PythonVersion } from '../pythonEnvironments/base/info'; +import { + GetRefreshEnvironmentsOptions, + IDiscoveryAPI, + ProgressNotificationEvent, + ProgressReportStage, + PythonLocatorQuery, + TriggerRefreshOptions, +} from '../pythonEnvironments/base/locator'; +import { PythonEnvCollectionChangedEvent } from '../pythonEnvironments/base/watcher'; +import { getEnvExtApi } from './api.internal'; +import { createDeferred, Deferred } from '../common/utils/async'; +import { StopWatch } from '../common/utils/stopWatch'; +import { traceLog } from '../logging'; +import { + DidChangeEnvironmentsEventArgs, + EnvironmentChangeKind, + PythonEnvironment, + PythonEnvironmentApi, +} from './types'; +import { FileChangeType } from '../common/platform/fileSystemWatcher'; +import { Architecture, isWindows } from '../common/utils/platform'; +import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion'; + +function getKind(pythonEnv: PythonEnvironment): PythonEnvKind { + if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) { + return PythonEnvKind.System; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) { + return PythonEnvKind.Conda; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('venv')) { + return PythonEnvKind.Venv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenv')) { + return PythonEnvKind.VirtualEnv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenvwrapper')) { + return PythonEnvKind.VirtualEnvWrapper; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pyenv')) { + return PythonEnvKind.Pyenv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pipenv')) { + return PythonEnvKind.Pipenv; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('poetry')) { + return PythonEnvKind.Poetry; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('pixi')) { + return PythonEnvKind.Pixi; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('hatch')) { + return PythonEnvKind.Hatch; + } + if (pythonEnv.envId.managerId.toLowerCase().endsWith('activestate')) { + return PythonEnvKind.ActiveState; + } + + return PythonEnvKind.Unknown; +} + +function makeExecutablePath(prefix?: string): string { + if (!prefix) { + return process.platform === 'win32' ? 'python.exe' : 'python'; + } + return process.platform === 'win32' ? path.join(prefix, 'python.exe') : path.join(prefix, 'python'); +} + +function getExecutable(pythonEnv: PythonEnvironment): string { + if (pythonEnv.execInfo?.run?.executable) { + return pythonEnv.execInfo?.run?.executable; + } + + const basename = path.basename(pythonEnv.environmentPath.fsPath).toLowerCase(); + if (isWindows() && basename.startsWith('python') && basename.endsWith('.exe')) { + return pythonEnv.environmentPath.fsPath; + } + + if (!isWindows() && basename.startsWith('python')) { + return pythonEnv.environmentPath.fsPath; + } + + return makeExecutablePath(pythonEnv.sysPrefix); +} + +function getLocation(pythonEnv: PythonEnvironment): string { + if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) { + return pythonEnv.sysPrefix; + } + + return pythonEnv.environmentPath.fsPath; +} + +function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined { + switch (kind) { + case PythonEnvKind.Poetry: + case PythonEnvKind.Pyenv: + case PythonEnvKind.VirtualEnv: + case PythonEnvKind.Venv: + case PythonEnvKind.VirtualEnvWrapper: + case PythonEnvKind.OtherVirtual: + case PythonEnvKind.Pipenv: + case PythonEnvKind.ActiveState: + case PythonEnvKind.Hatch: + case PythonEnvKind.Pixi: + return PythonEnvType.Virtual; + + case PythonEnvKind.Conda: + return PythonEnvType.Conda; + + case PythonEnvKind.System: + case PythonEnvKind.Unknown: + case PythonEnvKind.OtherGlobal: + case PythonEnvKind.Custom: + case PythonEnvKind.MicrosoftStore: + default: + return undefined; + } +} + +function toPythonEnvInfo(pythonEnv: PythonEnvironment): PythonEnvInfo | undefined { + const kind = getKind(pythonEnv); + const arch = Architecture.x64; + const version: PythonVersion = parseVersion(pythonEnv.version); + const { name, displayName, sysPrefix } = pythonEnv; + const executable = getExecutable(pythonEnv); + const location = getLocation(pythonEnv); + + return { + name, + location, + kind, + id: executable, + executable: { + filename: executable, + sysPrefix, + ctime: -1, + mtime: -1, + }, + version: { + sysVersion: pythonEnv.version, + major: version.major, + minor: version.minor, + micro: version.micro, + }, + arch, + distro: { + org: '', + }, + source: [], + detailedDisplayName: displayName, + display: displayName, + type: getEnvType(kind), + }; +} + +function hasChanged(old: PythonEnvInfo, newEnv: PythonEnvInfo): boolean { + if (old.executable.filename !== newEnv.executable.filename) { + return true; + } + if (old.version.major !== newEnv.version.major) { + return true; + } + if (old.version.minor !== newEnv.version.minor) { + return true; + } + if (old.version.micro !== newEnv.version.micro) { + return true; + } + if (old.location !== newEnv.location) { + return true; + } + if (old.kind !== newEnv.kind) { + return true; + } + if (old.arch !== newEnv.arch) { + return true; + } + + return false; +} + +class EnvExtApis implements IDiscoveryAPI, Disposable { + private _onProgress: EventEmitter; + + private _onChanged: EventEmitter; + + private _refreshPromise?: Deferred; + + private _envs: PythonEnvInfo[] = []; + + refreshState: ProgressReportStage; + + private _disposables: Disposable[] = []; + + constructor(private envExtApi: PythonEnvironmentApi) { + this._onProgress = new EventEmitter(); + this._onChanged = new EventEmitter(); + + this.onProgress = this._onProgress.event; + this.onChanged = this._onChanged.event; + + this.refreshState = ProgressReportStage.idle; + this._disposables.push( + this._onProgress, + this._onChanged, + this.envExtApi.onDidChangeEnvironments((e) => this.onDidChangeEnvironments(e)), + this.envExtApi.onDidChangeEnvironment((e) => { + this._onChanged.fire({ + type: FileChangeType.Changed, + searchLocation: e.uri, + old: e.old ? toPythonEnvInfo(e.old) : undefined, + new: e.new ? toPythonEnvInfo(e.new) : undefined, + }); + }), + ); + } + + onProgress: Event; + + onChanged: Event; + + getRefreshPromise(_options?: GetRefreshEnvironmentsOptions): Promise | undefined { + return this._refreshPromise?.promise; + } + + triggerRefresh(_query?: PythonLocatorQuery, _options?: TriggerRefreshOptions): Promise { + const stopwatch = new StopWatch(); + traceLog('Native locator: Refresh started'); + if (this.refreshState === ProgressReportStage.discoveryStarted && this._refreshPromise?.promise) { + return this._refreshPromise?.promise; + } + + this.refreshState = ProgressReportStage.discoveryStarted; + this._onProgress.fire({ stage: this.refreshState }); + this._refreshPromise = createDeferred(); + + setImmediate(async () => { + try { + await this.envExtApi.refreshEnvironments(undefined); + this._refreshPromise?.resolve(); + } catch (error) { + this._refreshPromise?.reject(error); + } finally { + traceLog(`Native locator: Refresh finished in ${stopwatch.elapsedTime} ms`); + this.refreshState = ProgressReportStage.discoveryFinished; + this._refreshPromise = undefined; + this._onProgress.fire({ stage: this.refreshState }); + } + }); + + return this._refreshPromise?.promise; + } + + getEnvs(_query?: PythonLocatorQuery): PythonEnvInfo[] { + return this._envs; + } + + private addEnv(pythonEnv: PythonEnvironment, searchLocation?: Uri): PythonEnvInfo | undefined { + const info = toPythonEnvInfo(pythonEnv); + if (info) { + const old = this._envs.find((item) => item.executable.filename === info.executable.filename); + if (old) { + this._envs = this._envs.filter((item) => item.executable.filename !== info.executable.filename); + this._envs.push(info); + if (hasChanged(old, info)) { + this._onChanged.fire({ type: FileChangeType.Changed, old, new: info, searchLocation }); + } + } else { + this._envs.push(info); + this._onChanged.fire({ type: FileChangeType.Created, new: info, searchLocation }); + } + } + + return info; + } + + private removeEnv(env: PythonEnvInfo | string): void { + if (typeof env === 'string') { + const old = this._envs.find((item) => item.executable.filename === env); + this._envs = this._envs.filter((item) => item.executable.filename !== env); + this._onChanged.fire({ type: FileChangeType.Deleted, old }); + return; + } + this._envs = this._envs.filter((item) => item.executable.filename !== env.executable.filename); + this._onChanged.fire({ type: FileChangeType.Deleted, old: env }); + } + + async resolveEnv(envPath?: string): Promise { + if (envPath === undefined) { + return undefined; + } + const pythonEnv = await this.envExtApi.resolveEnvironment(Uri.file(envPath)); + if (pythonEnv) { + return this.addEnv(pythonEnv); + } + return undefined; + } + + dispose(): void { + this._disposables.forEach((d) => d.dispose()); + } + + onDidChangeEnvironments(e: DidChangeEnvironmentsEventArgs): void { + e.forEach((item) => { + if (item.kind === EnvironmentChangeKind.remove) { + this.removeEnv(item.environment.environmentPath.fsPath); + } + if (item.kind === EnvironmentChangeKind.add) { + this.addEnv(item.environment); + } + }); + } +} + +export async function createEnvExtApi(disposables: Disposable[]): Promise { + const api = new EnvExtApis(await getEnvExtApi()); + disposables.push(api); + return api; +} diff --git a/src/client/envExt/types.ts b/src/client/envExt/types.ts new file mode 100644 index 000000000000..190c0ccea5b9 --- /dev/null +++ b/src/client/envExt/types.ts @@ -0,0 +1,1233 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { + Uri, + Disposable, + MarkdownString, + Event, + LogOutputChannel, + ThemeIcon, + Terminal, + TaskExecution, + TerminalOptions, + FileChangeType, +} from 'vscode'; + +/** + * The path to an icon, or a theme-specific configuration of icons. + */ +export type IconPath = + | Uri + | { + /** + * The icon path for the light theme. + */ + light: Uri; + /** + * The icon path for the dark theme. + */ + dark: Uri; + } + | ThemeIcon; + +/** + * Options for executing a Python executable. + */ +export interface PythonCommandRunConfiguration { + /** + * Path to the binary like `python.exe` or `python3` to execute. This should be an absolute path + * to an executable that can be spawned. + */ + executable: string; + + /** + * Arguments to pass to the python executable. These arguments will be passed on all execute calls. + * This is intended for cases where you might want to do interpreter specific flags. + */ + args?: string[]; +} + +export enum TerminalShellType { + powershell = 'powershell', + powershellCore = 'powershellCore', + commandPrompt = 'commandPrompt', + gitbash = 'gitbash', + bash = 'bash', + zsh = 'zsh', + ksh = 'ksh', + fish = 'fish', + cshell = 'cshell', + tcshell = 'tshell', + nushell = 'nushell', + wsl = 'wsl', + xonsh = 'xonsh', + unknown = 'unknown', +} + +/** + * Contains details on how to use a particular python environment + * + * Running In Terminal: + * 1. If {@link PythonEnvironmentExecutionInfo.activatedRun} is provided, then that will be used. + * 2. If {@link PythonEnvironmentExecutionInfo.activatedRun} is not provided, then: + * - If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is known, then that will be used. + * - If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is not known, then: + * - {@link TerminalShellType.unknown} will be used if provided. + * - {@link PythonEnvironmentExecutionInfo.activation} will be used otherwise. + * - If {@link PythonEnvironmentExecutionInfo.shellActivation} is not provided, then {@link PythonEnvironmentExecutionInfo.activation} will be used. + * - If {@link PythonEnvironmentExecutionInfo.activation} is not provided, then {@link PythonEnvironmentExecutionInfo.run} will be used. + * + * Creating a Terminal: + * 1. If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is known, then that will be used. + * 2. If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is not known, then {@link PythonEnvironmentExecutionInfo.activation} will be used. + * 3. If {@link PythonEnvironmentExecutionInfo.shellActivation} is not provided, then: + * - {@link TerminalShellType.unknown} will be used if provided. + * - {@link PythonEnvironmentExecutionInfo.activation} will be used otherwise. + * 4. If {@link PythonEnvironmentExecutionInfo.activation} is not provided, then {@link PythonEnvironmentExecutionInfo.run} will be used. + * + */ +export interface PythonEnvironmentExecutionInfo { + /** + * Details on how to run the python executable. + */ + run: PythonCommandRunConfiguration; + + /** + * Details on how to run the python executable after activating the environment. + * If set this will overrides the {@link PythonEnvironmentExecutionInfo.run} command. + */ + activatedRun?: PythonCommandRunConfiguration; + + /** + * Details on how to activate an environment. + */ + activation?: PythonCommandRunConfiguration[]; + + /** + * Details on how to activate an environment using a shell specific command. + * If set this will override the {@link PythonEnvironmentExecutionInfo.activation}. + * {@link TerminalShellType.unknown} is used if shell type is not known. + * If {@link TerminalShellType.unknown} is not provided and shell type is not known then + * {@link PythonEnvironmentExecutionInfo.activation} if set. + */ + shellActivation?: Map; + + /** + * Details on how to deactivate an environment. + */ + deactivation?: PythonCommandRunConfiguration[]; + + /** + * Details on how to deactivate an environment using a shell specific command. + * If set this will override the {@link PythonEnvironmentExecutionInfo.deactivation} property. + * {@link TerminalShellType.unknown} is used if shell type is not known. + * If {@link TerminalShellType.unknown} is not provided and shell type is not known then + * {@link PythonEnvironmentExecutionInfo.deactivation} if set. + */ + shellDeactivation?: Map; +} + +/** + * Interface representing the ID of a Python environment. + */ +export interface PythonEnvironmentId { + /** + * The unique identifier of the Python environment. + */ + id: string; + + /** + * The ID of the manager responsible for the Python environment. + */ + managerId: string; +} + +/** + * Interface representing information about a Python environment. + */ +export interface PythonEnvironmentInfo { + /** + * The name of the Python environment. + */ + readonly name: string; + + /** + * The display name of the Python environment. + */ + readonly displayName: string; + + /** + * The short display name of the Python environment. + */ + readonly shortDisplayName?: string; + + /** + * The display path of the Python environment. + */ + readonly displayPath: string; + + /** + * The version of the Python environment. + */ + readonly version: string; + + /** + * Path to the python binary or environment folder. + */ + readonly environmentPath: Uri; + + /** + * The description of the Python environment. + */ + readonly description?: string; + + /** + * The tooltip for the Python environment, which can be a string or a Markdown string. + */ + readonly tooltip?: string | MarkdownString; + + /** + * The icon path for the Python environment, which can be a string, Uri, or an object with light and dark theme paths. + */ + readonly iconPath?: IconPath; + + /** + * Information on how to execute the Python environment. If not provided, {@link PythonEnvironmentApi.resolveEnvironment} will be + * used to to get the details at later point if needed. The recommendation is to fill this in if known. + */ + readonly execInfo?: PythonEnvironmentExecutionInfo; + + /** + * `sys.prefix` is the path to the base directory of the Python installation. Typically obtained by executing `sys.prefix` in the Python interpreter. + * This is required by extension like Jupyter, Pylance, and other extensions to provide better experience with python. + */ + readonly sysPrefix: string; +} + +/** + * Interface representing a Python environment. + */ +export interface PythonEnvironment extends PythonEnvironmentInfo { + /** + * The ID of the Python environment. + */ + readonly envId: PythonEnvironmentId; +} + +/** + * Type representing the scope for setting a Python environment. + * Can be undefined or a URI. + */ +export type SetEnvironmentScope = undefined | Uri; + +/** + * Type representing the scope for getting a Python environment. + * Can be undefined or a URI. + */ +export type GetEnvironmentScope = undefined | Uri; + +/** + * Type representing the scope for creating a Python environment. + * Can be a Python project or 'global'. + */ +export type CreateEnvironmentScope = Uri | Uri[] | 'global'; +/** + * The scope for which environments are to be refreshed. + * - `undefined`: Search for environments globally and workspaces. + * - {@link Uri}: Environments in the workspace/folder or associated with the Uri. + */ +export type RefreshEnvironmentsScope = Uri | undefined; + +/** + * The scope for which environments are required. + * - `"all"`: All environments. + * - `"global"`: Python installations that are usually a base for creating virtual environments. + * - {@link Uri}: Environments for the workspace/folder/file pointed to by the Uri. + */ +export type GetEnvironmentsScope = Uri | 'all' | 'global'; + +/** + * Event arguments for when the current Python environment changes. + */ +export type DidChangeEnvironmentEventArgs = { + /** + * The URI of the environment that changed. + */ + readonly uri: Uri | undefined; + + /** + * The old Python environment before the change. + */ + readonly old: PythonEnvironment | undefined; + + /** + * The new Python environment after the change. + */ + readonly new: PythonEnvironment | undefined; +}; + +/** + * Enum representing the kinds of environment changes. + */ +export enum EnvironmentChangeKind { + /** + * Indicates that an environment was added. + */ + add = 'add', + + /** + * Indicates that an environment was removed. + */ + remove = 'remove', +} + +/** + * Event arguments for when the list of Python environments changes. + */ +export type DidChangeEnvironmentsEventArgs = { + /** + * The kind of change that occurred (add or remove). + */ + kind: EnvironmentChangeKind; + + /** + * The Python environment that was added or removed. + */ + environment: PythonEnvironment; +}[]; + +/** + * Type representing the context for resolving a Python environment. + */ +export type ResolveEnvironmentContext = PythonEnvironment | Uri; + +/** + * Interface representing an environment manager. + */ +export interface EnvironmentManager { + /** + * The name of the environment manager. + */ + readonly name: string; + + /** + * The display name of the environment manager. + */ + readonly displayName?: string; + + /** + * The preferred package manager ID for the environment manager. + * + * @example + * 'ms-python.python:pip' + */ + readonly preferredPackageManagerId: string; + + /** + * The description of the environment manager. + */ + readonly description?: string; + + /** + * The tooltip for the environment manager, which can be a string or a Markdown string. + */ + readonly tooltip?: string | MarkdownString | undefined; + + /** + * The icon path for the environment manager, which can be a string, Uri, or an object with light and dark theme paths. + */ + readonly iconPath?: IconPath; + + /** + * The log output channel for the environment manager. + */ + readonly log?: LogOutputChannel; + + /** + * Creates a new Python environment within the specified scope. + * @param scope - The scope within which to create the environment. + * @returns A promise that resolves to the created Python environment, or undefined if creation failed. + */ + create?(scope: CreateEnvironmentScope): Promise; + + /** + * Removes the specified Python environment. + * @param environment - The Python environment to remove. + * @returns A promise that resolves when the environment is removed. + */ + remove?(environment: PythonEnvironment): Promise; + + /** + * Refreshes the list of Python environments within the specified scope. + * @param scope - The scope within which to refresh environments. + * @returns A promise that resolves when the refresh is complete. + */ + refresh(scope: RefreshEnvironmentsScope): Promise; + + /** + * Retrieves a list of Python environments within the specified scope. + * @param scope - The scope within which to retrieve environments. + * @returns A promise that resolves to an array of Python environments. + */ + getEnvironments(scope: GetEnvironmentsScope): Promise; + + /** + * Event that is fired when the list of Python environments changes. + */ + onDidChangeEnvironments?: Event; + + /** + * Sets the current Python environment within the specified scope. + * @param scope - The scope within which to set the environment. + * @param environment - The Python environment to set. If undefined, the environment is unset. + * @returns A promise that resolves when the environment is set. + */ + set(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise; + + /** + * Retrieves the current Python environment within the specified scope. + * @param scope - The scope within which to retrieve the environment. + * @returns A promise that resolves to the current Python environment, or undefined if none is set. + */ + get(scope: GetEnvironmentScope): Promise; + + /** + * Event that is fired when the current Python environment changes. + */ + onDidChangeEnvironment?: Event; + + /** + * Resolves the specified Python environment. The environment can be either a {@link PythonEnvironment} or a {@link Uri} context. + * + * This method is used to obtain a fully detailed {@link PythonEnvironment} object. The input can be: + * - A {@link PythonEnvironment} object, which might be missing key details such as {@link PythonEnvironment.execInfo}. + * - A {@link Uri} object, which typically represents either: + * - A folder that contains the Python environment. + * - The path to a Python executable. + * + * @param context - The context for resolving the environment, which can be a {@link PythonEnvironment} or a {@link Uri}. + * @returns A promise that resolves to the fully detailed {@link PythonEnvironment}, or `undefined` if the environment cannot be resolved. + */ + resolve(context: ResolveEnvironmentContext): Promise; + + /** + * Clears the environment manager's cache. + * + * @returns A promise that resolves when the cache is cleared. + */ + clearCache?(): Promise; +} + +/** + * Interface representing a package ID. + */ +export interface PackageId { + /** + * The ID of the package. + */ + id: string; + + /** + * The ID of the package manager. + */ + managerId: string; + + /** + * The ID of the environment in which the package is installed. + */ + environmentId: string; +} + +/** + * Interface representing package information. + */ +export interface PackageInfo { + /** + * The name of the package. + */ + readonly name: string; + + /** + * The display name of the package. + */ + readonly displayName: string; + + /** + * The version of the package. + */ + readonly version?: string; + + /** + * The description of the package. + */ + readonly description?: string; + + /** + * The tooltip for the package, which can be a string or a Markdown string. + */ + readonly tooltip?: string | MarkdownString | undefined; + + /** + * The icon path for the package, which can be a string, Uri, or an object with light and dark theme paths. + */ + readonly iconPath?: IconPath; + + /** + * The URIs associated with the package. + */ + readonly uris?: readonly Uri[]; +} + +/** + * Interface representing a package. + */ +export interface Package extends PackageInfo { + /** + * The ID of the package. + */ + readonly pkgId: PackageId; +} + +/** + * Enum representing the kinds of package changes. + */ +export enum PackageChangeKind { + /** + * Indicates that a package was added. + */ + add = 'add', + + /** + * Indicates that a package was removed. + */ + remove = 'remove', +} + +/** + * Event arguments for when packages change. + */ +export interface DidChangePackagesEventArgs { + /** + * The Python environment in which the packages changed. + */ + environment: PythonEnvironment; + + /** + * The package manager responsible for the changes. + */ + manager: PackageManager; + + /** + * The list of changes, each containing the kind of change and the package affected. + */ + changes: { kind: PackageChangeKind; pkg: Package }[]; +} + +/** + * Interface representing a package manager. + */ +export interface PackageManager { + /** + * The name of the package manager. + */ + name: string; + + /** + * The display name of the package manager. + */ + displayName?: string; + + /** + * The description of the package manager. + */ + description?: string; + + /** + * The tooltip for the package manager, which can be a string or a Markdown string. + */ + tooltip?: string | MarkdownString | undefined; + + /** + * The icon path for the package manager, which can be a string, Uri, or an object with light and dark theme paths. + */ + iconPath?: IconPath; + + /** + * The log output channel for the package manager. + */ + log?: LogOutputChannel; + + /** + * Installs packages in the specified Python environment. + * @param environment - The Python environment in which to install packages. + * @param packages - The packages to install. + * @returns A promise that resolves when the installation is complete. + */ + install(environment: PythonEnvironment, packages: string[], options: PackageInstallOptions): Promise; + + /** + * Uninstalls packages from the specified Python environment. + * @param environment - The Python environment from which to uninstall packages. + * @param packages - The packages to uninstall, which can be an array of packages or strings. + * @returns A promise that resolves when the uninstall is complete. + */ + uninstall(environment: PythonEnvironment, packages: Package[] | string[]): Promise; + + /** + * Refreshes the package list for the specified Python environment. + * @param environment - The Python environment for which to refresh the package list. + * @returns A promise that resolves when the refresh is complete. + */ + refresh(environment: PythonEnvironment): Promise; + + /** + * Retrieves the list of packages for the specified Python environment. + * @param environment - The Python environment for which to retrieve packages. + * @returns An array of packages, or undefined if the packages could not be retrieved. + */ + getPackages(environment: PythonEnvironment): Promise; + + /** + * Get a list of installable items for a Python project. + * + * @param environment The Python environment for which to get installable items. + * + * Note: An environment can be used by multiple projects, so the installable items returned. + * should be for the environment. If you want to do it for a particular project, then you should + * ask user to select a project, and filter the installable items based on the project. + */ + getInstallable?(environment: PythonEnvironment): Promise; + + /** + * Event that is fired when packages change. + */ + onDidChangePackages?: Event; + + /** + * Clears the package manager's cache. + * @returns A promise that resolves when the cache is cleared. + */ + clearCache?(): Promise; +} + +/** + * Interface representing a Python project. + */ +export interface PythonProject { + /** + * The name of the Python project. + */ + readonly name: string; + + /** + * The URI of the Python project. + */ + readonly uri: Uri; + + /** + * The description of the Python project. + */ + readonly description?: string; + + /** + * The tooltip for the Python project, which can be a string or a Markdown string. + */ + readonly tooltip?: string | MarkdownString; + + /** + * The icon path for the Python project, which can be a string, Uri, or an object with light and dark theme paths. + */ + readonly iconPath?: IconPath; +} + +/** + * Options for creating a Python project. + */ +export interface PythonProjectCreatorOptions { + /** + * The name of the Python project. + */ + name: string; + + /** + * Optional path that may be provided as a root for the project. + */ + uri?: Uri; +} + +/** + * Interface representing a creator for Python projects. + */ +export interface PythonProjectCreator { + /** + * The name of the Python project creator. + */ + readonly name: string; + + /** + * The display name of the Python project creator. + */ + readonly displayName?: string; + + /** + * The description of the Python project creator. + */ + readonly description?: string; + + /** + * The tooltip for the Python project creator, which can be a string or a Markdown string. + */ + readonly tooltip?: string | MarkdownString; + + /** + * The icon path for the Python project creator, which can be a string, Uri, or an object with light and dark theme paths. + */ + readonly iconPath?: IconPath; + + /** + * Creates a new Python project or projects. + * @param options - Optional parameters for creating the Python project. + * @returns A promise that resolves to a Python project, an array of Python projects, or undefined. + */ + create(options?: PythonProjectCreatorOptions): Promise; +} + +/** + * Event arguments for when Python projects change. + */ +export interface DidChangePythonProjectsEventArgs { + /** + * The list of Python projects that were added. + */ + added: PythonProject[]; + + /** + * The list of Python projects that were removed. + */ + removed: PythonProject[]; +} + +/** + * Options for package installation. + */ +export interface PackageInstallOptions { + /** + * Upgrade the packages if it is already installed. + */ + upgrade?: boolean; +} + +export interface Installable { + /** + * The display name of the package, requirements, pyproject.toml or any other project file. + */ + readonly displayName: string; + + /** + * Arguments passed to the package manager to install the package. + * + * @example + * ['debugpy==1.8.7'] for `pip install debugpy==1.8.7`. + * ['--pre', 'debugpy'] for `pip install --pre debugpy`. + * ['-r', 'requirements.txt'] for `pip install -r requirements.txt`. + */ + readonly args: string[]; + + /** + * Installable group name, this will be used to group installable items in the UI. + * + * @example + * `Requirements` for any requirements file. + * `Packages` for any package. + */ + readonly group?: string; + + /** + * Description about the installable item. This can also be path to the requirements, + * version of the package, or any other project file path. + */ + readonly description?: string; + + /** + * External Uri to the package on pypi or docs. + * @example + * https://pypi.org/project/debugpy/ for `debugpy`. + */ + readonly uri?: Uri; +} + +export interface PythonProcess { + /** + * The process ID of the Python process. + */ + readonly pid?: number; + + /** + * The standard input of the Python process. + */ + readonly stdin: NodeJS.WritableStream; + + /** + * The standard output of the Python process. + */ + readonly stdout: NodeJS.ReadableStream; + + /** + * The standard error of the Python process. + */ + readonly stderr: NodeJS.ReadableStream; + + /** + * Kills the Python process. + */ + kill(): void; + + /** + * Event that is fired when the Python process exits. + */ + onExit(listener: (code: number | null, signal: NodeJS.Signals | null) => void): void; +} + +export interface PythonEnvironmentManagerRegistrationApi { + /** + * Register an environment manager implementation. + * + * @param manager Environment Manager implementation to register. + * @returns A disposable that can be used to unregister the environment manager. + * @see {@link EnvironmentManager} + */ + registerEnvironmentManager(manager: EnvironmentManager): Disposable; +} + +export interface PythonEnvironmentItemApi { + /** + * Create a Python environment item from the provided environment info. This item is used to interact + * with the environment. + * + * @param info Some details about the environment like name, version, etc. needed to interact with the environment. + * @param manager The environment manager to associate with the environment. + * @returns The Python environment. + */ + createPythonEnvironmentItem(info: PythonEnvironmentInfo, manager: EnvironmentManager): PythonEnvironment; +} + +export interface PythonEnvironmentManagementApi { + /** + * Create a Python environment using environment manager associated with the scope. + * + * @param scope Where the environment is to be created. + * @returns The Python environment created. `undefined` if not created. + */ + createEnvironment(scope: CreateEnvironmentScope): Promise; + + /** + * Remove a Python environment. + * + * @param environment The Python environment to remove. + * @returns A promise that resolves when the environment has been removed. + */ + removeEnvironment(environment: PythonEnvironment): Promise; +} + +export interface PythonEnvironmentsApi { + /** + * Initiates a refresh of Python environments within the specified scope. + * @param scope - The scope within which to search for environments. + * @returns A promise that resolves when the search is complete. + */ + refreshEnvironments(scope: RefreshEnvironmentsScope): Promise; + + /** + * Retrieves a list of Python environments within the specified scope. + * @param scope - The scope within which to retrieve environments. + * @returns A promise that resolves to an array of Python environments. + */ + getEnvironments(scope: GetEnvironmentsScope): Promise; + + /** + * Event that is fired when the list of Python environments changes. + * @see {@link DidChangeEnvironmentsEventArgs} + */ + onDidChangeEnvironments: Event; + + /** + * This method is used to get the details missing from a PythonEnvironment. Like + * {@link PythonEnvironment.execInfo} and other details. + * + * @param context : The PythonEnvironment or Uri for which details are required. + */ + resolveEnvironment(context: ResolveEnvironmentContext): Promise; +} + +export interface PythonProjectEnvironmentApi { + /** + * Sets the current Python environment within the specified scope. + * @param scope - The scope within which to set the environment. + * @param environment - The Python environment to set. If undefined, the environment is unset. + */ + setEnvironment(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise; + + /** + * Retrieves the current Python environment within the specified scope. + * @param scope - The scope within which to retrieve the environment. + * @returns A promise that resolves to the current Python environment, or undefined if none is set. + */ + getEnvironment(scope: GetEnvironmentScope): Promise; + + /** + * Event that is fired when the selected Python environment changes for Project, Folder or File. + * @see {@link DidChangeEnvironmentEventArgs} + */ + onDidChangeEnvironment: Event; +} + +export interface PythonEnvironmentManagerApi + extends PythonEnvironmentManagerRegistrationApi, + PythonEnvironmentItemApi, + PythonEnvironmentManagementApi, + PythonEnvironmentsApi, + PythonProjectEnvironmentApi {} + +export interface PythonPackageManagerRegistrationApi { + /** + * Register a package manager implementation. + * + * @param manager Package Manager implementation to register. + * @returns A disposable that can be used to unregister the package manager. + * @see {@link PackageManager} + */ + registerPackageManager(manager: PackageManager): Disposable; +} + +export interface PythonPackageGetterApi { + /** + * Refresh the list of packages in a Python Environment. + * + * @param environment The Python Environment for which the list of packages is to be refreshed. + * @returns A promise that resolves when the list of packages has been refreshed. + */ + refreshPackages(environment: PythonEnvironment): Promise; + + /** + * Get the list of packages in a Python Environment. + * + * @param environment The Python Environment for which the list of packages is required. + * @returns The list of packages in the Python Environment. + */ + getPackages(environment: PythonEnvironment): Promise; + + /** + * Event raised when the list of packages in a Python Environment changes. + * @see {@link DidChangePackagesEventArgs} + */ + onDidChangePackages: Event; +} + +export interface PythonPackageItemApi { + /** + * Create a package item from the provided package info. + * + * @param info The package info. + * @param environment The Python Environment in which the package is installed. + * @param manager The package manager that installed the package. + * @returns The package item. + */ + createPackageItem(info: PackageInfo, environment: PythonEnvironment, manager: PackageManager): Package; +} + +export interface PythonPackageManagementApi { + /** + * Install packages into a Python Environment. + * + * @param environment The Python Environment into which packages are to be installed. + * @param packages The packages to install. + * @param options Options for installing packages. + */ + installPackages(environment: PythonEnvironment, packages: string[], options?: PackageInstallOptions): Promise; + + /** + * Uninstall packages from a Python Environment. + * + * @param environment The Python Environment from which packages are to be uninstalled. + * @param packages The packages to uninstall. + */ + uninstallPackages(environment: PythonEnvironment, packages: PackageInfo[] | string[]): Promise; +} + +export interface PythonPackageManagerApi + extends PythonPackageManagerRegistrationApi, + PythonPackageGetterApi, + PythonPackageManagementApi, + PythonPackageItemApi {} + +export interface PythonProjectCreationApi { + /** + * Register a Python project creator. + * + * @param creator The project creator to register. + * @returns A disposable that can be used to unregister the project creator. + * @see {@link PythonProjectCreator} + */ + registerPythonProjectCreator(creator: PythonProjectCreator): Disposable; +} +export interface PythonProjectGetterApi { + /** + * Get all python projects. + */ + getPythonProjects(): readonly PythonProject[]; + + /** + * Get the python project for a given URI. + * + * @param uri The URI of the project + * @returns The project or `undefined` if not found. + */ + getPythonProject(uri: Uri): PythonProject | undefined; +} + +export interface PythonProjectModifyApi { + /** + * Add a python project or projects to the list of projects. + * + * @param projects The project or projects to add. + */ + addPythonProject(projects: PythonProject | PythonProject[]): void; + + /** + * Remove a python project from the list of projects. + * + * @param project The project to remove. + */ + removePythonProject(project: PythonProject): void; + + /** + * Event raised when python projects are added or removed. + * @see {@link DidChangePythonProjectsEventArgs} + */ + onDidChangePythonProjects: Event; +} + +/** + * The API for interacting with Python projects. A project in python is any folder or file that is a contained + * in some manner. For example, a PEP-723 compliant file can be treated as a project. A folder with a `pyproject.toml`, + * or just python files can be treated as a project. All this allows you to do is set a python environment for that project. + * + * By default all `vscode.workspace.workspaceFolders` are treated as projects. + */ +export interface PythonProjectApi extends PythonProjectCreationApi, PythonProjectGetterApi, PythonProjectModifyApi {} + +export interface PythonTerminalOptions extends TerminalOptions { + /** + * Whether to show the terminal. + */ + disableActivation?: boolean; +} + +export interface PythonTerminalCreateApi { + /** + * Creates a terminal and activates any (activatable) environment for the terminal. + * + * @param environment The Python environment to activate. + * @param options Options for creating the terminal. + * + * Note: Non-activatable environments have no effect on the terminal. + */ + createTerminal(environment: PythonEnvironment, options: PythonTerminalOptions): Promise; +} + +/** + * Options for running a Python script or module in a terminal. + * + * Example: + * * Running Script: `python myscript.py --arg1` + * ```typescript + * { + * args: ["myscript.py", "--arg1"] + * } + * ``` + * * Running a module: `python -m my_module --arg1` + * ```typescript + * { + * args: ["-m", "my_module", "--arg1"] + * } + * ``` + */ +export interface PythonTerminalExecutionOptions { + /** + * Current working directory for the terminal. This in only used to create the terminal. + */ + cwd: string | Uri; + + /** + * Arguments to pass to the python executable. + */ + args?: string[]; + + /** + * Set `true` to show the terminal. + */ + show?: boolean; +} + +export interface PythonTerminalRunApi { + /** + * Runs a Python script or module in a terminal. This API will create a terminal if one is not available to use. + * If a terminal is available, it will be used to run the script or module. + * + * Note: + * - If you restart VS Code, this will create a new terminal, this is a limitation of VS Code. + * - If you close the terminal, this will create a new terminal. + * - In cases of multi-root/project scenario, it will create a separate terminal for each project. + */ + runInTerminal(environment: PythonEnvironment, options: PythonTerminalExecutionOptions): Promise; + + /** + * Runs a Python script or module in a dedicated terminal. This API will create a terminal if one is not available to use. + * If a terminal is available, it will be used to run the script or module. This terminal will be dedicated to the script, + * and selected based on the `terminalKey`. + * + * @param terminalKey A unique key to identify the terminal. For scripts you can use the Uri of the script file. + */ + runInDedicatedTerminal( + terminalKey: Uri | string, + environment: PythonEnvironment, + options: PythonTerminalExecutionOptions, + ): Promise; +} + +/** + * Options for running a Python task. + * + * Example: + * * Running Script: `python myscript.py --arg1` + * ```typescript + * { + * args: ["myscript.py", "--arg1"] + * } + * ``` + * * Running a module: `python -m my_module --arg1` + * ```typescript + * { + * args: ["-m", "my_module", "--arg1"] + * } + * ``` + */ +export interface PythonTaskExecutionOptions { + /** + * Name of the task to run. + */ + name: string; + + /** + * Arguments to pass to the python executable. + */ + args: string[]; + + /** + * The Python project to use for the task. + */ + project?: PythonProject; + + /** + * Current working directory for the task. Default is the project directory for the script being run. + */ + cwd?: string; + + /** + * Environment variables to set for the task. + */ + env?: { [key: string]: string }; +} + +export interface PythonTaskRunApi { + /** + * Run a Python script or module as a task. + * + */ + runAsTask(environment: PythonEnvironment, options: PythonTaskExecutionOptions): Promise; +} + +/** + * Options for running a Python script or module in the background. + */ +export interface PythonBackgroundRunOptions { + /** + * The Python environment to use for running the script or module. + */ + args: string[]; + + /** + * Current working directory for the script or module. Default is the project directory for the script being run. + */ + cwd?: string; + + /** + * Environment variables to set for the script or module. + */ + env?: { [key: string]: string | undefined }; +} +export interface PythonBackgroundRunApi { + /** + * Run a Python script or module in the background. This API will create a new process to run the script or module. + */ + runInBackground(environment: PythonEnvironment, options: PythonBackgroundRunOptions): Promise; +} + +export interface PythonExecutionApi + extends PythonTerminalCreateApi, + PythonTerminalRunApi, + PythonTaskRunApi, + PythonBackgroundRunApi {} + +/** + * Event arguments for when the monitored `.env` files or any other sources change. + */ +export interface DidChangeEnvironmentVariablesEventArgs { + /** + * The URI of the file that changed. No `Uri` means a non-file source of environment variables changed. + */ + uri?: Uri; + + /** + * The type of change that occurred. + */ + changeTye: FileChangeType; +} + +export interface PythonEnvironmentVariablesApi { + /** + * Get environment variables for a workspace. This picks up `.env` file from the root of the + * workspace. + * + * Order of overrides: + * 1. `baseEnvVar` if given or `process.env` + * 2. `.env` file from the "python.envFile" setting in the workspace. + * 3. `.env` file at the root of the python project. + * 4. `overrides` in the order provided. + * + * @param uri The URI of the project, workspace or a file in a for which environment variables are required. + * @param overrides Additional environment variables to override the defaults. + * @param baseEnvVar The base environment variables that should be used as a starting point. + */ + getEnvironmentVariables( + uri: Uri, + overrides?: ({ [key: string]: string | undefined } | Uri)[], + baseEnvVar?: { [key: string]: string | undefined }, + ): Promise<{ [key: string]: string | undefined }>; + + /** + * Event raised when `.env` file changes or any other monitored source of env variable changes. + */ + onDidChangeEnvironmentVariables: Event; +} + +/** + * The API for interacting with Python environments, package managers, and projects. + */ +export interface PythonEnvironmentApi + extends PythonEnvironmentManagerApi, + PythonPackageManagerApi, + PythonProjectApi, + PythonExecutionApi, + PythonEnvironmentVariablesApi {} diff --git a/src/client/interpreter/configuration/interpreterSelector/commands/resetInterpreter.ts b/src/client/interpreter/configuration/interpreterSelector/commands/resetInterpreter.ts index 82b40a3ff5e8..c10f90781adb 100644 --- a/src/client/interpreter/configuration/interpreterSelector/commands/resetInterpreter.ts +++ b/src/client/interpreter/configuration/interpreterSelector/commands/resetInterpreter.ts @@ -9,6 +9,8 @@ import { Commands } from '../../../../common/constants'; import { IConfigurationService, IPathUtils } from '../../../../common/types'; import { IPythonPathUpdaterServiceManager } from '../../types'; import { BaseInterpreterSelectorCommand } from './base'; +import { useEnvExtension } from '../../../../envExt/api.internal'; +import { resetInterpreterLegacy } from '../../../../envExt/api.legacy'; @injectable() export class ResetInterpreterCommand extends BaseInterpreterSelectorCommand { @@ -46,6 +48,9 @@ export class ResetInterpreterCommand extends BaseInterpreterSelectorCommand { const configTarget = targetConfig.configTarget; const wkspace = targetConfig.folderUri; await this.pythonPathUpdaterService.updatePythonPath(undefined, configTarget, 'ui', wkspace); + if (useEnvExtension()) { + await resetInterpreterLegacy(wkspace); + } }), ); } diff --git a/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts b/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts index e65cd1567ac4..27816ee83296 100644 --- a/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts +++ b/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts @@ -47,6 +47,8 @@ import { } from '../../types'; import { BaseInterpreterSelectorCommand } from './base'; import { untildify } from '../../../../common/helpers'; +import { useEnvExtension } from '../../../../envExt/api.internal'; +import { setInterpreterLegacy } from '../../../../envExt/api.legacy'; export type InterpreterStateArgs = { path?: string; workspace: Resource }; export type QuickPickType = IInterpreterQuickPickItem | ISpecialQuickPickItem | QuickPickItem; @@ -581,6 +583,9 @@ export class SetInterpreterCommand extends BaseInterpreterSelectorCommand implem // an empty string, in which case we should update. // Having the value `undefined` means user cancelled the quickpick, so we update nothing in that case. await this.pythonPathUpdaterService.updatePythonPath(interpreterState.path, configTarget, 'ui', wkspace); + if (useEnvExtension()) { + await setInterpreterLegacy(interpreterState.path, wkspace); + } } } diff --git a/src/client/interpreter/display/index.ts b/src/client/interpreter/display/index.ts index aabb9f86f6d1..ebe7fc359cac 100644 --- a/src/client/interpreter/display/index.ts +++ b/src/client/interpreter/display/index.ts @@ -24,6 +24,7 @@ import { IInterpreterService, IInterpreterStatusbarVisibilityFilter, } from '../contracts'; +import { useEnvExtension } from '../../envExt/api.internal'; /** * Based on https://github.com/microsoft/vscode-python/issues/18040#issuecomment-992567670. @@ -67,6 +68,9 @@ export class InterpreterDisplay implements IInterpreterDisplay, IExtensionSingle } public async activate(): Promise { + if (useEnvExtension()) { + return; + } const application = this.serviceContainer.get(IApplicationShell); if (this.useLanguageStatus) { this.languageStatus = application.createLanguageStatusItem('python.selectedInterpreter', { @@ -111,6 +115,12 @@ export class InterpreterDisplay implements IInterpreterDisplay, IExtensionSingle } } private async updateDisplay(workspaceFolder?: Uri) { + if (useEnvExtension()) { + this.statusBar?.hide(); + this.languageStatus?.dispose(); + this.languageStatus = undefined; + return; + } const interpreter = await this.interpreterService.getActiveInterpreter(workspaceFolder); if ( this.currentlySelectedInterpreterDisplay && diff --git a/src/client/interpreter/interpreterService.ts b/src/client/interpreter/interpreterService.ts index e9829d978fb6..628a25d6b3b1 100644 --- a/src/client/interpreter/interpreterService.ts +++ b/src/client/interpreter/interpreterService.ts @@ -44,6 +44,8 @@ import { TriggerRefreshOptions, } from '../pythonEnvironments/base/locator'; import { sleep } from '../common/utils/async'; +import { useEnvExtension } from '../envExt/api.internal'; +import { ensureEnvironmentContainsPythonLegacy, getActiveInterpreterLegacy } from '../envExt/api.legacy'; type StoredPythonEnvironment = PythonEnvironment & { store?: boolean }; @@ -217,6 +219,10 @@ export class InterpreterService implements Disposable, IInterpreterService { } public async getActiveInterpreter(resource?: Uri): Promise { + if (useEnvExtension()) { + return getActiveInterpreterLegacy(resource); + } + const activatedEnvLaunch = this.serviceContainer.get(IActivatedEnvironmentLaunch); let path = await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv(true); // This is being set as interpreter in background, after which it'll show up in `.pythonPath` config. @@ -283,6 +289,16 @@ export class InterpreterService implements Disposable, IInterpreterService { @cache(-1, true) private async ensureEnvironmentContainsPython(pythonPath: string, workspaceFolder: WorkspaceFolder | undefined) { + if (useEnvExtension()) { + await ensureEnvironmentContainsPythonLegacy(pythonPath); + this.didChangeInterpreterEmitter.fire(workspaceFolder?.uri); + reportActiveInterpreterChanged({ + path: pythonPath, + resource: workspaceFolder, + }); + return; + } + const installer = this.serviceContainer.get(IInstaller); if (!(await installer.isInstalled(Product.python))) { // If Python is not installed into the environment, install it. diff --git a/src/client/providers/terminalProvider.ts b/src/client/providers/terminalProvider.ts index d047ea4b6d82..407a5520b29a 100644 --- a/src/client/providers/terminalProvider.ts +++ b/src/client/providers/terminalProvider.ts @@ -11,6 +11,7 @@ import { swallowExceptions } from '../common/utils/decorators'; import { IServiceContainer } from '../ioc/types'; import { captureTelemetry, sendTelemetryEvent } from '../telemetry'; import { EventName } from '../telemetry/constants'; +import { useEnvExtension } from '../envExt/api.internal'; export class TerminalProvider implements Disposable { private disposables: Disposable[] = []; @@ -31,7 +32,8 @@ export class TerminalProvider implements Disposable { if ( currentTerminal && pythonSettings.terminal.activateEnvInCurrentTerminal && - !inTerminalEnvVarExperiment(experimentService) + !inTerminalEnvVarExperiment(experimentService) && + !useEnvExtension() ) { const hideFromUser = 'hideFromUser' in currentTerminal.creationOptions && currentTerminal.creationOptions.hideFromUser; diff --git a/src/client/pythonEnvironments/creation/createEnvApi.ts b/src/client/pythonEnvironments/creation/createEnvApi.ts index 6bc5de8ce4df..ab0f0db317c3 100644 --- a/src/client/pythonEnvironments/creation/createEnvApi.ts +++ b/src/client/pythonEnvironments/creation/createEnvApi.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { ConfigurationTarget, Disposable } from 'vscode'; +import { ConfigurationTarget, Disposable, QuickInputButtons } from 'vscode'; import { Commands } from '../../common/constants'; import { IDisposableRegistry, IInterpreterPathService, IPathUtils } from '../../common/types'; import { executeCommand, registerCommand } from '../../common/vscodeApis/commandApis'; @@ -21,6 +21,8 @@ import { import { sendTelemetryEvent } from '../../telemetry'; import { EventName } from '../../telemetry/constants'; import { CreateEnvironmentOptionsInternal } from './types'; +import { useEnvExtension } from '../../envExt/api.internal'; +import { PythonEnvironment } from '../../envExt/types'; class CreateEnvironmentProviders { private _createEnvProviders: CreateEnvironmentProvider[] = []; @@ -65,11 +67,26 @@ export function registerCreateEnvironmentFeatures( disposables.push( registerCommand( Commands.Create_Environment, - ( + async ( options?: CreateEnvironmentOptions & CreateEnvironmentOptionsInternal, ): Promise => { - const providers = _createEnvironmentProviders.getAll(); - return handleCreateEnvironmentCommand(providers, options); + if (useEnvExtension()) { + try { + const result = await executeCommand('python-envs.createAny'); + if (result) { + return { path: result.environmentPath.path }; + } + } catch (err) { + if (err === QuickInputButtons.Back) { + return { workspaceFolder: undefined, action: 'Back' }; + } + throw err; + } + } else { + const providers = _createEnvironmentProviders.getAll(); + return handleCreateEnvironmentCommand(providers, options); + } + return undefined; }, ), registerCommand( diff --git a/src/client/pythonEnvironments/index.ts b/src/client/pythonEnvironments/index.ts index e7e7f390fa58..299dfab59132 100644 --- a/src/client/pythonEnvironments/index.ts +++ b/src/client/pythonEnvironments/index.ts @@ -43,6 +43,8 @@ import { PixiLocator } from './base/locators/lowLevel/pixiLocator'; import { getConfiguration } from '../common/vscodeApis/workspaceApis'; import { getNativePythonFinder } from './base/locators/common/nativePythonFinder'; import { createNativeEnvironmentsApi } from './nativeAPI'; +import { useEnvExtension } from '../envExt/api.internal'; +import { createEnvExtApi } from '../envExt/envExtApi'; const PYTHON_ENV_INFO_CACHE_KEY = 'PYTHON_ENV_INFO_CACHEv2'; @@ -58,6 +60,16 @@ export async function initialize(ext: ExtensionState): Promise { // Set up the legacy IOC container before api is created. initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer); + if (useEnvExtension()) { + const api = await createEnvExtApi(ext.disposables); + registerNewDiscoveryForIOC( + // These are what get wrapped in the legacy adapter. + ext.legacyIOC.serviceManager, + api, + ); + return api; + } + if (shouldUseNativeLocator()) { const finder = getNativePythonFinder(ext.context); const api = createNativeEnvironmentsApi(finder); @@ -69,6 +81,7 @@ export async function initialize(ext: ExtensionState): Promise { ); return api; } + const api = await createPythonEnvironments(() => createLocator(ext)); registerNewDiscoveryForIOC( // These are what get wrapped in the legacy adapter. diff --git a/src/client/terminals/codeExecution/codeExecutionManager.ts b/src/client/terminals/codeExecution/codeExecutionManager.ts index 120725f11983..b1dc328d137e 100644 --- a/src/client/terminals/codeExecution/codeExecutionManager.ts +++ b/src/client/terminals/codeExecution/codeExecutionManager.ts @@ -4,7 +4,7 @@ 'use strict'; import { inject, injectable } from 'inversify'; -import { Disposable, EventEmitter, Uri } from 'vscode'; +import { Disposable, EventEmitter, Terminal, Uri } from 'vscode'; import { ICommandManager, IDocumentManager } from '../../common/application/types'; import { Commands } from '../../common/constants'; @@ -22,6 +22,7 @@ import { triggerCreateEnvironmentCheckNonBlocking, } from '../../pythonEnvironments/creation/createEnvironmentTrigger'; import { ReplType } from '../../repl/types'; +import { runInDedicatedTerminal, runInTerminal, useEnvExtension } from '../../envExt/api.internal'; @injectable() export class CodeExecutionManager implements ICodeExecutionManager { @@ -40,6 +41,16 @@ export class CodeExecutionManager implements ICodeExecutionManager { this.disposableRegistry.push( this.commandManager.registerCommand(cmd as any, async (file: Resource) => { traceVerbose(`Attempting to run Python file`, file?.fsPath); + + if (useEnvExtension()) { + try { + await this.executeUsingExtension(file, cmd === Commands.Exec_In_Separate_Terminal); + } catch (ex) { + traceError('Failed to execute file in terminal', ex); + } + return; + } + const interpreterService = this.serviceContainer.get(IInterpreterService); const interpreter = await interpreterService.getActiveInterpreter(file); if (!interpreter) { @@ -101,6 +112,42 @@ export class CodeExecutionManager implements ICodeExecutionManager { ), ); } + + private async executeUsingExtension(file: Resource, dedicated: boolean): Promise { + const codeExecutionHelper = this.serviceContainer.get(ICodeExecutionHelper); + file = file instanceof Uri ? file : undefined; + let fileToExecute = file ? file : await codeExecutionHelper.getFileToExecute(); + if (!fileToExecute) { + return; + } + const fileAfterSave = await codeExecutionHelper.saveFileIfDirty(fileToExecute); + if (fileAfterSave) { + fileToExecute = fileAfterSave; + } + + const show = this.shouldTerminalFocusOnStart(fileToExecute); + let terminal: Terminal | undefined; + if (dedicated) { + terminal = await runInDedicatedTerminal( + fileToExecute, + [fileToExecute.fsPath.fileToCommandArgumentForPythonExt()], + undefined, + show, + ); + } else { + terminal = await runInTerminal( + fileToExecute, + [fileToExecute.fsPath.fileToCommandArgumentForPythonExt()], + undefined, + show, + ); + } + + if (terminal) { + terminal.show(); + } + } + private async executeFileInTerminal( file: Resource, trigger: 'command' | 'icon', @@ -145,7 +192,7 @@ export class CodeExecutionManager implements ICodeExecutionManager { return; } const codeExecutionHelper = this.serviceContainer.get(ICodeExecutionHelper); - const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor!); + const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor); let wholeFileContent = ''; if (activeEditor && activeEditor.document) { wholeFileContent = activeEditor.document.getText(); @@ -167,7 +214,7 @@ export class CodeExecutionManager implements ICodeExecutionManager { noop(); } - await executionService.execute(normalizedCode, activeEditor!.document.uri); + await executionService.execute(normalizedCode, activeEditor.document.uri); } private shouldTerminalFocusOnStart(uri: Uri | undefined): boolean { diff --git a/src/client/terminals/envCollectionActivation/indicatorPrompt.ts b/src/client/terminals/envCollectionActivation/indicatorPrompt.ts index 3e463e386545..5701bf78603e 100644 --- a/src/client/terminals/envCollectionActivation/indicatorPrompt.ts +++ b/src/client/terminals/envCollectionActivation/indicatorPrompt.ts @@ -21,6 +21,7 @@ import { ITerminalEnvVarCollectionService } from '../types'; import { sleep } from '../../common/utils/async'; import { isTestExecution } from '../../common/constants'; import { PythonEnvType } from '../../pythonEnvironments/base/info'; +import { useEnvExtension } from '../../envExt/api.internal'; export const terminalEnvCollectionPromptKey = 'TERMINAL_ENV_COLLECTION_PROMPT_KEY'; @@ -42,7 +43,7 @@ export class TerminalIndicatorPrompt implements IExtensionSingleActivationServic ) {} public async activate(): Promise { - if (!inTerminalEnvVarExperiment(this.experimentService)) { + if (!inTerminalEnvVarExperiment(this.experimentService) || useEnvExtension()) { return; } if (!isTestExecution()) { diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 62971aa1fa98..a527abe90454 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -43,6 +43,7 @@ import { ITerminalEnvVarCollectionService, } from '../types'; import { ProgressService } from '../../common/application/progressService'; +import { useEnvExtension } from '../../envExt/api.internal'; @injectable() export class TerminalEnvVarCollectionService implements IExtensionActivationService, ITerminalEnvVarCollectionService { @@ -175,6 +176,12 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ const workspaceFolder = this.getWorkspaceFolder(resource); const settings = this.configurationService.getSettings(resource); const envVarCollection = this.getEnvironmentVariableCollection({ workspaceFolder }); + if (useEnvExtension()) { + envVarCollection.clear(); + traceVerbose('Do not activate terminal env vars as env extension is being used'); + return; + } + if (!settings.terminal.activateEnvironment) { envVarCollection.clear(); traceVerbose('Activating environments in terminal is disabled for', resource?.fsPath); @@ -371,6 +378,11 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ try { const settings = this.configurationService.getSettings(resource); const workspaceFolder = this.getWorkspaceFolder(resource); + if (useEnvExtension()) { + this.getEnvironmentVariableCollection({ workspaceFolder }).clear(); + traceVerbose('Do not activate microvenv as env extension is being used'); + return; + } if (!settings.terminal.activateEnvironment) { this.getEnvironmentVariableCollection({ workspaceFolder }).clear(); traceVerbose( diff --git a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts index 837d2bd8f6c0..ba4216cf0f7c 100644 --- a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts +++ b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts @@ -24,6 +24,7 @@ import { } from '../common/utils'; import { IEnvironmentVariablesProvider } from '../../../common/variables/types'; import { PythonEnvironment } from '../../../pythonEnvironments/info'; +import { useEnvExtension, getEnvironment, runInBackground } from '../../../envExt/api.internal'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. #this seems incorrectly copied @@ -95,6 +96,47 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { mutableEnv.PYTHONPATH = pythonPathCommand; mutableEnv.TEST_RUN_PIPE = discoveryPipeName; traceInfo(`All environment variables set for pytest discovery: ${JSON.stringify(mutableEnv)}`); + + // delete UUID following entire discovery finishing. + const execArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs); + traceVerbose(`Running pytest discovery with command: ${execArgs.join(' ')} for workspace ${uri.fsPath}.`); + + if (useEnvExtension()) { + const pythonEnv = await getEnvironment(uri); + if (pythonEnv) { + const deferredTillExecClose: Deferred = createTestingDeferred(); + + const proc = await runInBackground(pythonEnv, { + cwd, + args: execArgs, + env: (mutableEnv as unknown) as { [key: string]: string }, + }); + proc.stdout.on('data', (data) => { + const out = fixLogLinesNoTrailing(data.toString()); + traceInfo(out); + this.outputChannel?.append(out); + }); + proc.stderr.on('data', (data) => { + const out = fixLogLinesNoTrailing(data.toString()); + traceError(out); + this.outputChannel?.append(out); + }); + proc.onExit((code, signal) => { + this.outputChannel?.append(MESSAGE_ON_TESTING_OUTPUT_MOVE); + if (code !== 0) { + traceError( + `Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`, + ); + } + deferredTillExecClose.resolve(); + }); + await deferredTillExecClose.promise; + } else { + traceError(`Python Environment not found for: ${uri.fsPath}`); + } + return; + } + const spawnOptions: SpawnOptions = { cwd, throwOnStdErr: true, @@ -109,9 +151,6 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { interpreter, }; const execService = await executionFactory?.createActivatedEnvironment(creationOptions); - // delete UUID following entire discovery finishing. - const execArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs); - traceVerbose(`Running pytest discovery with command: ${execArgs.join(' ')} for workspace ${uri.fsPath}.`); const deferredTillExecClose: Deferred = createTestingDeferred(); const result = execService?.execObservable(execArgs, spawnOptions); diff --git a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index bc5ac7dfae9f..6413baf8baee 100644 --- a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -20,6 +20,7 @@ import { EXTENSION_ROOT_DIR } from '../../../common/constants'; import * as utils from '../common/utils'; import { IEnvironmentVariablesProvider } from '../../../common/variables/types'; import { PythonEnvironment } from '../../../pythonEnvironments/info'; +import { getEnvironment, runInBackground, useEnvExtension } from '../../../envExt/api.internal'; export class PytestTestExecutionAdapter implements ITestExecutionAdapter { constructor( @@ -153,7 +154,6 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { cwd, throwOnStdErr: true, outputChannel: this.outputChannel, - stdinStr: testIds.toString(), env: mutableEnv, token: runInstance?.token, }; @@ -178,6 +178,50 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { }, sessionOptions, ); + } else if (useEnvExtension()) { + const pythonEnv = await getEnvironment(uri); + if (pythonEnv) { + const deferredTillExecClose: Deferred = utils.createTestingDeferred(); + + const scriptPath = path.join(fullPluginPath, 'vscode_pytest', 'run_pytest_script.py'); + const runArgs = [scriptPath, ...testArgs]; + traceInfo(`Running pytest with arguments: ${runArgs.join(' ')} for workspace ${uri.fsPath} \r\n`); + + const proc = await runInBackground(pythonEnv, { + cwd, + args: runArgs, + env: (mutableEnv as unknown) as { [key: string]: string }, + }); + runInstance?.token.onCancellationRequested(() => { + traceInfo(`Test run cancelled, killing pytest subprocess for workspace ${uri.fsPath}`); + proc.kill(); + deferredTillExecClose.resolve(); + serverCancel.cancel(); + }); + proc.stdout.on('data', (data) => { + const out = utils.fixLogLinesNoTrailing(data.toString()); + runInstance?.appendOutput(out); + this.outputChannel?.append(out); + }); + proc.stderr.on('data', (data) => { + const out = utils.fixLogLinesNoTrailing(data.toString()); + runInstance?.appendOutput(out); + this.outputChannel?.append(out); + }); + proc.onExit((code, signal) => { + this.outputChannel?.append(utils.MESSAGE_ON_TESTING_OUTPUT_MOVE); + if (code !== 0) { + traceError( + `Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`, + ); + } + deferredTillExecClose.resolve(); + serverCancel.cancel(); + }); + await deferredTillExecClose.promise; + } else { + traceError(`Python Environment not found for: ${uri.fsPath}`); + } } else { // deferredTillExecClose is resolved when all stdout and stderr is read const deferredTillExecClose: Deferred = utils.createTestingDeferred(); @@ -217,7 +261,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { }); result?.proc?.on('exit', (code, signal) => { this.outputChannel?.append(utils.MESSAGE_ON_TESTING_OUTPUT_MOVE); - if (code !== 0 && testIds) { + if (code !== 0) { traceError( `Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`, ); @@ -228,7 +272,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { traceVerbose('Test run finished, subprocess closed.'); // if the child has testIds then this is a run request // if the child process exited with a non-zero exit code, then we need to send the error payload. - if (code !== 0 && testIds) { + if (code !== 0) { traceError( `Subprocess closed unsuccessfully with exit code ${code} and signal ${signal} for workspace ${uri.fsPath}. Creating and sending error execution payload \n`, ); diff --git a/src/client/testing/testController/unittest/testDiscoveryAdapter.ts b/src/client/testing/testController/unittest/testDiscoveryAdapter.ts index ba52d1ffd57b..04518e121651 100644 --- a/src/client/testing/testController/unittest/testDiscoveryAdapter.ts +++ b/src/client/testing/testController/unittest/testDiscoveryAdapter.ts @@ -27,6 +27,7 @@ import { startDiscoveryNamedPipe, } from '../common/utils'; import { traceError, traceInfo, traceLog } from '../../../logging'; +import { getEnvironment, runInBackground, useEnvExtension } from '../../../envExt/api.internal'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. @@ -86,6 +87,47 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter { ...(await this.envVarsService?.getEnvironmentVariables(uri)), }; mutableEnv.TEST_RUN_PIPE = testRunPipeName; + const args = [options.command.script].concat(options.command.args); + + if (options.outChannel) { + options.outChannel.appendLine(`python ${args.join(' ')}`); + } + + if (useEnvExtension()) { + const pythonEnv = await getEnvironment(uri); + if (pythonEnv) { + const deferredTillExecClose = createDeferred(); + + const proc = await runInBackground(pythonEnv, { + cwd, + args, + env: (mutableEnv as unknown) as { [key: string]: string }, + }); + proc.stdout.on('data', (data) => { + const out = fixLogLinesNoTrailing(data.toString()); + traceInfo(out); + this.outputChannel?.append(out); + }); + proc.stderr.on('data', (data) => { + const out = fixLogLinesNoTrailing(data.toString()); + traceError(out); + this.outputChannel?.append(out); + }); + proc.onExit((code, signal) => { + this.outputChannel?.append(MESSAGE_ON_TESTING_OUTPUT_MOVE); + if (code !== 0) { + traceError( + `Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`, + ); + } + deferredTillExecClose.resolve(); + }); + await deferredTillExecClose.promise; + } else { + traceError(`Python Environment not found for: ${uri.fsPath}`); + } + return; + } const spawnOptions: SpawnOptions = { token: options.token, @@ -94,23 +136,18 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter { outputChannel: options.outChannel, env: mutableEnv, }; - // Create the Python environment in which to execute the command. - const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { - allowEnvironmentFetchExceptions: false, - resource: options.workspaceFolder, - }; - const execService = await executionFactory?.createActivatedEnvironment(creationOptions); - - const args = [options.command.script].concat(options.command.args); - - if (options.outChannel) { - options.outChannel.appendLine(`python ${args.join(' ')}`); - } try { traceLog(`Discovering unittest tests for workspace ${options.cwd} with arguments: ${args}\r\n`); const deferredTillExecClose = createDeferred>(); + // Create the Python environment in which to execute the command. + const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { + allowEnvironmentFetchExceptions: false, + resource: options.workspaceFolder, + }; + const execService = await executionFactory?.createActivatedEnvironment(creationOptions); + const result = execService?.execObservable(args, spawnOptions); // Displays output to user and ensure the subprocess doesn't run into buffer overflow. diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index 3254e9570fd9..6db36d96149f 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -26,6 +26,7 @@ import { import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; import { UNITTEST_PROVIDER } from '../../common/constants'; import * as utils from '../common/utils'; +import { getEnvironment, runInBackground, useEnvExtension } from '../../../envExt/api.internal'; /** * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? @@ -182,6 +183,47 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { }, sessionOptions, ); + } else if (useEnvExtension()) { + const pythonEnv = await getEnvironment(uri); + if (pythonEnv) { + traceInfo(`Running unittest with arguments: ${args.join(' ')} for workspace ${uri.fsPath} \r\n`); + const deferredTillExecClose = createDeferred(); + + const proc = await runInBackground(pythonEnv, { + cwd, + args, + env: (mutableEnv as unknown) as { [key: string]: string }, + }); + runInstance?.token.onCancellationRequested(() => { + traceInfo(`Test run cancelled, killing unittest subprocess for workspace ${uri.fsPath}`); + proc.kill(); + deferredTillExecClose.resolve(); + serverCancel.cancel(); + }); + proc.stdout.on('data', (data) => { + const out = utils.fixLogLinesNoTrailing(data.toString()); + runInstance?.appendOutput(out); + this.outputChannel?.append(out); + }); + proc.stderr.on('data', (data) => { + const out = utils.fixLogLinesNoTrailing(data.toString()); + runInstance?.appendOutput(out); + this.outputChannel?.append(out); + }); + proc.onExit((code, signal) => { + this.outputChannel?.append(utils.MESSAGE_ON_TESTING_OUTPUT_MOVE); + if (code !== 0) { + traceError( + `Subprocess exited unsuccessfully with exit code ${code} and signal ${signal} on workspace ${uri.fsPath}`, + ); + } + deferredTillExecClose.resolve(); + serverCancel.cancel(); + }); + await deferredTillExecClose.promise; + } else { + traceError(`Python Environment not found for: ${uri.fsPath}`); + } } else { // This means it is running the test traceInfo(`Running unittests for workspace ${cwd} with arguments: ${args}\r\n`); diff --git a/src/test/common/terminals/activator/index.unit.test.ts b/src/test/common/terminals/activator/index.unit.test.ts index a50b946c391f..6a50901bc99d 100644 --- a/src/test/common/terminals/activator/index.unit.test.ts +++ b/src/test/common/terminals/activator/index.unit.test.ts @@ -4,6 +4,7 @@ 'use strict'; import { assert } from 'chai'; +import * as sinon from 'sinon'; import * as TypeMoq from 'typemoq'; import { Terminal } from 'vscode'; import { TerminalActivator } from '../../../../client/common/terminal/activator'; @@ -18,6 +19,7 @@ import { IPythonSettings, ITerminalSettings, } from '../../../../client/common/types'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('Terminal Activator', () => { let activator: TerminalActivator; @@ -26,7 +28,11 @@ suite('Terminal Activator', () => { let handler2: TypeMoq.IMock; let terminalSettings: TypeMoq.IMock; let experimentService: TypeMoq.IMock; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + baseActivator = TypeMoq.Mock.ofType(); terminalSettings = TypeMoq.Mock.ofType(); experimentService = TypeMoq.Mock.ofType(); @@ -52,6 +58,10 @@ suite('Terminal Activator', () => { experimentService.object, ); }); + teardown(() => { + sinon.restore(); + }); + async function testActivationAndHandlers( activationSuccessful: boolean, activateEnvironmentSetting: boolean, diff --git a/src/test/common/terminals/service.unit.test.ts b/src/test/common/terminals/service.unit.test.ts index 147803a72598..9903f6781f28 100644 --- a/src/test/common/terminals/service.unit.test.ts +++ b/src/test/common/terminals/service.unit.test.ts @@ -25,6 +25,7 @@ import { ITerminalAutoActivation } from '../../../client/terminals/types'; import { createPythonInterpreter } from '../../utils/interpreters'; import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; import * as platform from '../../../client/common/utils/platform'; +import * as extapi from '../../../client/envExt/api.internal'; suite('Terminal Service', () => { let service: TerminalService; @@ -44,8 +45,12 @@ suite('Terminal Service', () => { let pythonConfig: TypeMoq.IMock; let editorConfig: TypeMoq.IMock; let isWindowsStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + terminal = TypeMoq.Mock.ofType(); terminalShellIntegration = TypeMoq.Mock.ofType(); terminal.setup((t) => t.shellIntegration).returns(() => terminalShellIntegration.object); diff --git a/src/test/configuration/interpreterSelector/commands/resetInterpreter.unit.test.ts b/src/test/configuration/interpreterSelector/commands/resetInterpreter.unit.test.ts index e1c3a960b99f..a32c794b7dc7 100644 --- a/src/test/configuration/interpreterSelector/commands/resetInterpreter.unit.test.ts +++ b/src/test/configuration/interpreterSelector/commands/resetInterpreter.unit.test.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import * as path from 'path'; +import * as sinon from 'sinon'; import * as TypeMoq from 'typemoq'; import { ConfigurationTarget, Uri } from 'vscode'; import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../../../client/common/application/types'; @@ -10,6 +11,7 @@ import { IConfigurationService } from '../../../../client/common/types'; import { Common, Interpreters } from '../../../../client/common/utils/localize'; import { ResetInterpreterCommand } from '../../../../client/interpreter/configuration/interpreterSelector/commands/resetInterpreter'; import { IPythonPathUpdaterServiceManager } from '../../../../client/interpreter/configuration/types'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('Reset Interpreter Command', () => { let workspace: TypeMoq.IMock; @@ -21,8 +23,12 @@ suite('Reset Interpreter Command', () => { const folder2 = { name: 'two', uri: Uri.parse('two'), index: 2 }; let resetInterpreterCommand: ResetInterpreterCommand; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + configurationService = TypeMoq.Mock.ofType(); configurationService .setup((c) => c.getSettings(TypeMoq.It.isAny())) @@ -42,6 +48,9 @@ suite('Reset Interpreter Command', () => { configurationService.object, ); }); + teardown(() => { + sinon.restore(); + }); suite('Test method resetInterpreter()', async () => { test('Update Global settings when there are no workspaces', async () => { diff --git a/src/test/configuration/interpreterSelector/commands/setInterpreter.unit.test.ts b/src/test/configuration/interpreterSelector/commands/setInterpreter.unit.test.ts index 5737a2e416c5..0016ca339bfe 100644 --- a/src/test/configuration/interpreterSelector/commands/setInterpreter.unit.test.ts +++ b/src/test/configuration/interpreterSelector/commands/setInterpreter.unit.test.ts @@ -48,6 +48,7 @@ import { IInterpreterService, PythonEnvironmentsChangedEvent } from '../../../.. import { createDeferred, sleep } from '../../../../client/common/utils/async'; import { SystemVariables } from '../../../../client/common/variables/systemVariables'; import { untildify } from '../../../../client/common/helpers'; +import * as extapi from '../../../../client/envExt/api.internal'; type TelemetryEventType = { eventName: EventName; properties: unknown }; @@ -62,12 +63,16 @@ suite('Set Interpreter Command', () => { let platformService: TypeMoq.IMock; let multiStepInputFactory: TypeMoq.IMock; let interpreterService: IInterpreterService; + let useEnvExtensionStub: sinon.SinonStub; const folder1 = { name: 'one', uri: Uri.parse('one'), index: 1 }; const folder2 = { name: 'two', uri: Uri.parse('two'), index: 2 }; let setInterpreterCommand: SetInterpreterCommand; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + interpreterSelector = TypeMoq.Mock.ofType(); multiStepInputFactory = TypeMoq.Mock.ofType(); platformService = TypeMoq.Mock.ofType(); diff --git a/src/test/interpreters/activation/indicatorPrompt.unit.test.ts b/src/test/interpreters/activation/indicatorPrompt.unit.test.ts index 2214057fc952..b15cd84dc01a 100644 --- a/src/test/interpreters/activation/indicatorPrompt.unit.test.ts +++ b/src/test/interpreters/activation/indicatorPrompt.unit.test.ts @@ -3,6 +3,7 @@ 'use strict'; +import * as sinon from 'sinon'; import { mock, when, anything, instance, verify, reset } from 'ts-mockito'; import { EventEmitter, Terminal, Uri } from 'vscode'; import { IActiveResourceService, IApplicationShell, ITerminalManager } from '../../../client/common/application/types'; @@ -21,6 +22,7 @@ import { IInterpreterService } from '../../../client/interpreter/contracts'; import { PythonEnvironment } from '../../../client/pythonEnvironments/info'; import { ITerminalEnvVarCollectionService } from '../../../client/terminals/types'; import { PythonEnvType } from '../../../client/pythonEnvironments/base/info'; +import * as extapi from '../../../client/envExt/api.internal'; suite('Terminal Activation Indicator Prompt', () => { let shell: IApplicationShell; @@ -34,12 +36,16 @@ suite('Terminal Activation Indicator Prompt', () => { let notificationEnabled: IPersistentState; let configurationService: IConfigurationService; let interpreterService: IInterpreterService; + let useEnvExtensionStub: sinon.SinonStub; const prompts = [Common.doNotShowAgain]; const envName = 'env'; const type = PythonEnvType.Virtual; const expectedMessage = Interpreters.terminalEnvVarCollectionPrompt.format('Python virtual', `"(${envName})"`); setup(async () => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + shell = mock(); terminalManager = mock(); interpreterService = mock(); @@ -77,6 +83,10 @@ suite('Terminal Activation Indicator Prompt', () => { ); }); + teardown(() => { + sinon.restore(); + }); + test('Show notification when a new terminal is opened for which there is no prompt set', async () => { const resource = Uri.file('a'); const terminal = ({ diff --git a/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts b/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts index 3550a92ba1ec..7016a25c7a4e 100644 --- a/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts +++ b/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts @@ -39,6 +39,7 @@ import { PythonEnvType } from '../../../client/pythonEnvironments/base/info'; import { PythonEnvironment } from '../../../client/pythonEnvironments/info'; import { IShellIntegrationDetectionService, ITerminalDeactivateService } from '../../../client/terminals/types'; import { IEnvironmentVariablesProvider } from '../../../client/common/variables/types'; +import * as extapi from '../../../client/envExt/api.internal'; suite('Terminal Environment Variable Collection Service', () => { let platform: IPlatformService; @@ -53,6 +54,7 @@ suite('Terminal Environment Variable Collection Service', () => { let workspaceService: IWorkspaceService; let terminalEnvVarCollectionService: TerminalEnvVarCollectionService; let terminalDeactivateService: ITerminalDeactivateService; + let useEnvExtensionStub: sinon.SinonStub; const progressOptions = { location: ProgressLocation.Window, title: Interpreters.activatingTerminals, @@ -64,6 +66,9 @@ suite('Terminal Environment Variable Collection Service', () => { const defaultShell = defaultShells[getOSType()]; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + workspaceService = mock(); terminalDeactivateService = mock(); when(terminalDeactivateService.getScriptLocation(anything(), anything())).thenResolve(undefined); diff --git a/src/test/interpreters/display.unit.test.ts b/src/test/interpreters/display.unit.test.ts index 3537425f2efc..7d53fbfc0561 100644 --- a/src/test/interpreters/display.unit.test.ts +++ b/src/test/interpreters/display.unit.test.ts @@ -32,6 +32,7 @@ import { IServiceContainer } from '../../client/ioc/types'; import * as logging from '../../client/logging'; import { EnvironmentType, PythonEnvironment } from '../../client/pythonEnvironments/info'; import { ThemeColor } from '../mocks/vsc'; +import * as extapi from '../../client/envExt/api.internal'; const info: PythonEnvironment = { architecture: Architecture.Unknown, @@ -58,6 +59,7 @@ suite('Interpreters Display', () => { let pathUtils: TypeMoq.IMock; let languageStatusItem: TypeMoq.IMock; let traceLogStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; async function createInterpreterDisplay(filters: IInterpreterStatusbarVisibilityFilter[] = []) { interpreterDisplay = new InterpreterDisplay(serviceContainer.object); try { @@ -67,6 +69,9 @@ suite('Interpreters Display', () => { } async function setupMocks(useLanguageStatus: boolean) { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + serviceContainer = TypeMoq.Mock.ofType(); workspaceService = TypeMoq.Mock.ofType(); applicationShell = TypeMoq.Mock.ofType(); diff --git a/src/test/interpreters/interpreterService.unit.test.ts b/src/test/interpreters/interpreterService.unit.test.ts index 81a6a014a7e0..1d521dad8ec8 100644 --- a/src/test/interpreters/interpreterService.unit.test.ts +++ b/src/test/interpreters/interpreterService.unit.test.ts @@ -37,6 +37,7 @@ import { PYTHON_PATH } from '../common'; import { MockAutoSelectionService } from '../mocks/autoSelector'; import * as proposedApi from '../../client/environmentApi'; import { createTypeMoq } from '../mocks/helper'; +import * as extapi from '../../client/envExt/api.internal'; /* eslint-disable @typescript-eslint/no-explicit-any */ @@ -62,8 +63,12 @@ suite('Interpreters service', () => { let installer: TypeMoq.IMock; let appShell: TypeMoq.IMock; let reportActiveInterpreterChangedStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + const cont = new Container(); serviceManager = new ServiceManager(cont); serviceContainer = new ServiceContainer(cont); diff --git a/src/test/providers/terminal.unit.test.ts b/src/test/providers/terminal.unit.test.ts index 1924f42d6927..ac39ded922c8 100644 --- a/src/test/providers/terminal.unit.test.ts +++ b/src/test/providers/terminal.unit.test.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import * as assert from 'assert'; +import * as sinon from 'sinon'; import { expect } from 'chai'; import * as TypeMoq from 'typemoq'; import { Disposable, Terminal, Uri } from 'vscode'; @@ -18,6 +19,7 @@ import { } from '../../client/common/types'; import { IServiceContainer } from '../../client/ioc/types'; import { TerminalProvider } from '../../client/providers/terminalProvider'; +import * as extapi from '../../client/envExt/api.internal'; suite('Terminal Provider', () => { let serviceContainer: TypeMoq.IMock; @@ -26,8 +28,12 @@ suite('Terminal Provider', () => { let activeResourceService: TypeMoq.IMock; let experimentService: TypeMoq.IMock; let terminalProvider: TerminalProvider; + let useEnvExtensionStub: sinon.SinonStub; const resource = Uri.parse('a'); setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + serviceContainer = TypeMoq.Mock.ofType(); commandManager = TypeMoq.Mock.ofType(); experimentService = TypeMoq.Mock.ofType(); @@ -40,6 +46,7 @@ suite('Terminal Provider', () => { serviceContainer.setup((c) => c.get(IActiveResourceService)).returns(() => activeResourceService.object); }); teardown(() => { + sinon.restore(); try { terminalProvider.dispose(); } catch { diff --git a/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts b/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts index be58ecbc8e6b..726b118ce180 100644 --- a/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts +++ b/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts @@ -14,6 +14,7 @@ import { IConfigurationService } from '../../../client/common/types'; import { IInterpreterService } from '../../../client/interpreter/contracts'; import { PythonEnvironment } from '../../../client/pythonEnvironments/info'; import * as triggerApis from '../../../client/pythonEnvironments/creation/createEnvironmentTrigger'; +import * as extapi from '../../../client/envExt/api.internal'; suite('Terminal - Code Execution Manager', () => { let executionManager: ICodeExecutionManager; @@ -25,7 +26,11 @@ suite('Terminal - Code Execution Manager', () => { let configService: TypeMoq.IMock; let interpreterService: TypeMoq.IMock; let triggerCreateEnvironmentCheckNonBlockingStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + workspace = TypeMoq.Mock.ofType(); workspace .setup((c) => c.onDidChangeWorkspaceFolders(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())) diff --git a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts index 9670f52108a5..538b77161483 100644 --- a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts @@ -20,6 +20,7 @@ import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; import { MockChildProcess } from '../../../mocks/mockChildProcess'; import { Deferred, createDeferred } from '../../../../client/common/utils/async'; import * as util from '../../../../client/testing/testController/common/utils'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('pytest test discovery adapter', () => { let configService: IConfigurationService; @@ -34,8 +35,12 @@ suite('pytest test discovery adapter', () => { let mockProc: MockChildProcess; let deferred2: Deferred; let utilsStartDiscoveryNamedPipeStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + const mockExtensionRootDir = typeMoq.Mock.ofType(); mockExtensionRootDir.setup((m) => m.toString()).returns(() => '/mocked/extension/root/dir'); diff --git a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts index 2eb615dbd1a2..18cabcc96772 100644 --- a/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestExecutionAdapter.unit.test.ts @@ -21,8 +21,10 @@ import * as util from '../../../../client/testing/testController/common/utils'; import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; import { MockChildProcess } from '../../../mocks/mockChildProcess'; import { traceInfo } from '../../../../client/logging'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('pytest test execution adapter', () => { + let useEnvExtensionStub: sinon.SinonStub; let configService: IConfigurationService; let execFactory = typeMoq.Mock.ofType(); let adapter: PytestTestExecutionAdapter; @@ -35,7 +37,10 @@ suite('pytest test execution adapter', () => { let mockProc: MockChildProcess; let utilsWriteTestIdsFileStub: sinon.SinonStub; let utilsStartRunResultNamedPipeStub: sinon.SinonStub; + setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); configService = ({ getSettings: () => ({ testing: { pytestArgs: ['.'] }, diff --git a/src/test/testing/testController/testCancellationRunAdapters.unit.test.ts b/src/test/testing/testController/testCancellationRunAdapters.unit.test.ts index af4903a1515b..81480d08b2b8 100644 --- a/src/test/testing/testController/testCancellationRunAdapters.unit.test.ts +++ b/src/test/testing/testController/testCancellationRunAdapters.unit.test.ts @@ -15,6 +15,7 @@ import { PytestTestExecutionAdapter } from '../../../client/testing/testControll import { UnittestTestExecutionAdapter } from '../../../client/testing/testController/unittest/testExecutionAdapter'; import { MockChildProcess } from '../../mocks/mockChildProcess'; import * as util from '../../../client/testing/testController/common/utils'; +import * as extapi from '../../../client/envExt/api.internal'; const adapters: Array = ['pytest', 'unittest']; @@ -32,7 +33,11 @@ suite('Execution Flow Run Adapters', () => { let utilsStartRunResultNamedPipe: sinon.SinonStub; let serverDisposeStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; + setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); // general vars myTestPath = path.join('/', 'my', 'test', 'path', '/'); configService = ({ diff --git a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts index e6d1cbc29293..a0ee65d57922 100644 --- a/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testDiscoveryAdapter.unit.test.ts @@ -20,6 +20,7 @@ import { Output, SpawnOptions, } from '../../../../client/common/process/types'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('Unittest test discovery adapter', () => { let stubConfigSettings: IConfigurationService; @@ -32,8 +33,12 @@ suite('Unittest test discovery adapter', () => { let expectedPath: string; let uri: Uri; let utilsStartDiscoveryNamedPipeStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); + expectedPath = path.join('/', 'new', 'cwd'); stubConfigSettings = ({ getSettings: () => ({ diff --git a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index 8f2afcc51cd1..78dcb0229e45 100644 --- a/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -21,6 +21,7 @@ import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; import { MockChildProcess } from '../../../mocks/mockChildProcess'; import { traceInfo } from '../../../../client/logging'; import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter'; +import * as extapi from '../../../../client/envExt/api.internal'; suite('Unittest test execution adapter', () => { let configService: IConfigurationService; @@ -35,7 +36,10 @@ suite('Unittest test execution adapter', () => { let mockProc: MockChildProcess; let utilsWriteTestIdsFileStub: sinon.SinonStub; let utilsStartRunResultNamedPipeStub: sinon.SinonStub; + let useEnvExtensionStub: sinon.SinonStub; setup(() => { + useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension'); + useEnvExtensionStub.returns(false); configService = ({ getSettings: () => ({ testing: { unittestArgs: ['.'] },