|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { inject, injectable, optional } from 'inversify'; |
| 5 | +import { ConfigurationTarget } from 'vscode'; |
| 6 | +import * as path from 'path'; |
| 7 | +import { IApplicationShell, IWorkspaceService } from '../../common/application/types'; |
| 8 | +import { IProcessServiceFactory } from '../../common/process/types'; |
| 9 | +import { sleep } from '../../common/utils/async'; |
| 10 | +import { cache } from '../../common/utils/decorators'; |
| 11 | +import { Common, Interpreters } from '../../common/utils/localize'; |
| 12 | +import { traceError, traceLog, traceWarn } from '../../logging'; |
| 13 | +import { Conda } from '../../pythonEnvironments/common/environmentManagers/conda'; |
| 14 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 15 | +import { EventName } from '../../telemetry/constants'; |
| 16 | +import { IPythonPathUpdaterServiceManager } from '../configuration/types'; |
| 17 | +import { IActivatedEnvironmentLaunch, IInterpreterService } from '../contracts'; |
| 18 | + |
| 19 | +@injectable() |
| 20 | +export class ActivatedEnvironmentLaunch implements IActivatedEnvironmentLaunch { |
| 21 | + public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: true }; |
| 22 | + |
| 23 | + private inMemorySelection: string | undefined; |
| 24 | + |
| 25 | + constructor( |
| 26 | + @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, |
| 27 | + @inject(IApplicationShell) private readonly appShell: IApplicationShell, |
| 28 | + @inject(IPythonPathUpdaterServiceManager) |
| 29 | + private readonly pythonPathUpdaterService: IPythonPathUpdaterServiceManager, |
| 30 | + @inject(IInterpreterService) private readonly interpreterService: IInterpreterService, |
| 31 | + @inject(IProcessServiceFactory) private readonly processServiceFactory: IProcessServiceFactory, |
| 32 | + @optional() public wasSelected: boolean = false, |
| 33 | + ) {} |
| 34 | + |
| 35 | + @cache(-1, true) |
| 36 | + public async _promptIfApplicable(): Promise<void> { |
| 37 | + const baseCondaPrefix = getPrefixOfActivatedCondaEnv(); |
| 38 | + if (!baseCondaPrefix) { |
| 39 | + return; |
| 40 | + } |
| 41 | + const info = await this.interpreterService.getInterpreterDetails(baseCondaPrefix); |
| 42 | + if (info?.envName !== 'base') { |
| 43 | + // Only show prompt for base conda environments, as we need to check config for such envs which can be slow. |
| 44 | + return; |
| 45 | + } |
| 46 | + const conda = await Conda.getConda(); |
| 47 | + if (!conda) { |
| 48 | + traceWarn('Conda not found even though activated environment vars are set'); |
| 49 | + return; |
| 50 | + } |
| 51 | + const service = await this.processServiceFactory.create(); |
| 52 | + const autoActivateBaseConfig = await service |
| 53 | + .shellExec(`${conda.shellCommand} config --get auto_activate_base`) |
| 54 | + .catch((ex) => { |
| 55 | + traceError(ex); |
| 56 | + return { stdout: '' }; |
| 57 | + }); |
| 58 | + if (autoActivateBaseConfig.stdout.trim().toLowerCase().endsWith('false')) { |
| 59 | + await this.promptAndUpdate(baseCondaPrefix); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private async promptAndUpdate(prefix: string) { |
| 64 | + this.wasSelected = true; |
| 65 | + const prompts = [Common.bannerLabelYes, Common.bannerLabelNo]; |
| 66 | + const telemetrySelections: ['Yes', 'No'] = ['Yes', 'No']; |
| 67 | + const selection = await this.appShell.showInformationMessage(Interpreters.activatedCondaEnvLaunch, ...prompts); |
| 68 | + sendTelemetryEvent(EventName.ACTIVATED_CONDA_ENV_LAUNCH, undefined, { |
| 69 | + selection: selection ? telemetrySelections[prompts.indexOf(selection)] : undefined, |
| 70 | + }); |
| 71 | + if (!selection) { |
| 72 | + return; |
| 73 | + } |
| 74 | + if (selection === prompts[0]) { |
| 75 | + await this.setInterpeterInStorage(prefix); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public async selectIfLaunchedViaActivatedEnv(doNotBlockOnSelection = false): Promise<string | undefined> { |
| 80 | + if (this.wasSelected) { |
| 81 | + return this.inMemorySelection; |
| 82 | + } |
| 83 | + return this._selectIfLaunchedViaActivatedEnv(doNotBlockOnSelection); |
| 84 | + } |
| 85 | + |
| 86 | + @cache(-1, true) |
| 87 | + private async _selectIfLaunchedViaActivatedEnv(doNotBlockOnSelection = false): Promise<string | undefined> { |
| 88 | + if (this.workspaceService.workspaceFile) { |
| 89 | + // Assuming multiroot workspaces cannot be directly launched via `code .` command. |
| 90 | + return undefined; |
| 91 | + } |
| 92 | + const prefix = await this.getPrefixOfSelectedActivatedEnv(); |
| 93 | + if (!prefix) { |
| 94 | + this._promptIfApplicable().ignoreErrors(); |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + this.wasSelected = true; |
| 98 | + this.inMemorySelection = prefix; |
| 99 | + traceLog( |
| 100 | + `VS Code was launched from an activated environment: '${path.basename( |
| 101 | + prefix, |
| 102 | + )}', selecting it as the interpreter for workspace.`, |
| 103 | + ); |
| 104 | + if (doNotBlockOnSelection) { |
| 105 | + this.setInterpeterInStorage(prefix).ignoreErrors(); |
| 106 | + } else { |
| 107 | + await this.setInterpeterInStorage(prefix); |
| 108 | + await sleep(1); // Yield control so config service can update itself. |
| 109 | + } |
| 110 | + this.inMemorySelection = undefined; // Once we have set the prefix in storage, clear the in memory selection. |
| 111 | + return prefix; |
| 112 | + } |
| 113 | + |
| 114 | + private async setInterpeterInStorage(prefix: string) { |
| 115 | + const { workspaceFolders } = this.workspaceService; |
| 116 | + if (!workspaceFolders || workspaceFolders.length === 0) { |
| 117 | + await this.pythonPathUpdaterService.updatePythonPath(prefix, ConfigurationTarget.Global, 'load'); |
| 118 | + } else { |
| 119 | + await this.pythonPathUpdaterService.updatePythonPath( |
| 120 | + prefix, |
| 121 | + ConfigurationTarget.WorkspaceFolder, |
| 122 | + 'load', |
| 123 | + workspaceFolders[0].uri, |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private async getPrefixOfSelectedActivatedEnv(): Promise<string | undefined> { |
| 129 | + const virtualEnvVar = process.env.VIRTUAL_ENV; |
| 130 | + if (virtualEnvVar !== undefined && virtualEnvVar.length > 0) { |
| 131 | + return virtualEnvVar; |
| 132 | + } |
| 133 | + const condaPrefixVar = getPrefixOfActivatedCondaEnv(); |
| 134 | + if (!condaPrefixVar) { |
| 135 | + return undefined; |
| 136 | + } |
| 137 | + const info = await this.interpreterService.getInterpreterDetails(condaPrefixVar); |
| 138 | + if (info?.envName !== 'base') { |
| 139 | + return condaPrefixVar; |
| 140 | + } |
| 141 | + // Ignoring base conda environments, as they could be automatically set by conda. |
| 142 | + if (process.env.CONDA_AUTO_ACTIVATE_BASE !== undefined) { |
| 143 | + if (process.env.CONDA_AUTO_ACTIVATE_BASE.toLowerCase() === 'false') { |
| 144 | + return condaPrefixVar; |
| 145 | + } |
| 146 | + } |
| 147 | + return undefined; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +function getPrefixOfActivatedCondaEnv() { |
| 152 | + const condaPrefixVar = process.env.CONDA_PREFIX; |
| 153 | + if (condaPrefixVar && condaPrefixVar.length > 0) { |
| 154 | + const condaShlvl = process.env.CONDA_SHLVL; |
| 155 | + if (condaShlvl !== undefined && condaShlvl.length > 0 && condaShlvl > '0') { |
| 156 | + return condaPrefixVar; |
| 157 | + } |
| 158 | + } |
| 159 | + return undefined; |
| 160 | +} |
0 commit comments