From a268958f6412afa33152fb87f798ce1bca390bf4 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Fri, 13 May 2022 15:13:28 -0700 Subject: [PATCH 1/2] Only trigger auto environment discovery once in the first session for a particular scope --- news/1 Enhancements/19102.md | 1 + src/client/pythonEnvironments/index.ts | 52 ++++++++++++++++++++------ 2 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 news/1 Enhancements/19102.md diff --git a/news/1 Enhancements/19102.md b/news/1 Enhancements/19102.md new file mode 100644 index 000000000000..0632716cd6de --- /dev/null +++ b/news/1 Enhancements/19102.md @@ -0,0 +1 @@ +Only trigger auto environment discovery once in the first session for a particular scope (a workspace folder or globally). diff --git a/src/client/pythonEnvironments/index.ts b/src/client/pythonEnvironments/index.ts index 691710504f9d..e7726b1549ed 100644 --- a/src/client/pythonEnvironments/index.ts +++ b/src/client/pythonEnvironments/index.ts @@ -12,7 +12,10 @@ import { PythonEnvsReducer } from './base/locators/composite/envsReducer'; import { PythonEnvsResolver } from './base/locators/composite/envsResolver'; import { WindowsPathEnvVarLocator } from './base/locators/lowLevel/windowsKnownPathsLocator'; import { WorkspaceVirtualEnvironmentLocator } from './base/locators/lowLevel/workspaceVirtualEnvLocator'; -import { initializeExternalDependencies as initializeLegacyExternalDependencies } from './common/externalDependencies'; +import { + initializeExternalDependencies as initializeLegacyExternalDependencies, + normCasePath, +} from './common/externalDependencies'; import { ExtensionLocators, WatchRootsArgs, WorkspaceLocators } from './base/locators/wrappers'; import { CustomVirtualEnvironmentLocator } from './base/locators/lowLevel/customVirtualEnvLocator'; import { CondaEnvironmentLocator } from './base/locators/lowLevel/condaLocator'; @@ -52,20 +55,47 @@ export async function initialize(ext: ExtensionState): Promise { /** * Make use of the component (e.g. register with VS Code). */ -export async function activate(api: IDiscoveryAPI, _ext: ExtensionState): Promise { +export async function activate(api: IDiscoveryAPI, ext: ExtensionState): Promise { + return { + fullyReady: Promise.resolve(), + }; /** * Force an initial background refresh of the environments. * - * Note API is ready to be queried only after a refresh has been triggered, and extension activation is blocked on API. So, - * * If discovery was never triggered, we need to block extension activation on the refresh trigger. - * * If discovery was already triggered, it maybe the case that this is a new workspace for which it hasn't been triggered yet. - * So always trigger discovery as part of extension activation for now. - * - * TODO: https://github.com/microsoft/vscode-python/issues/17498 - * Once `onInterpretersChanged` event is exposed via API, we can probably expect extensions to rely on that and - * discovery can be triggered after activation, especially in the second case. + * Note API is ready to be queried only after a refresh has been triggered, and extension activation is + * blocked on API being ready. So if discovery was never triggered for a scope, we need to block + * extension activation on the "refresh trigger". */ - api.triggerRefresh().ignoreErrors(); + const folders = vscode.workspace.workspaceFolders; + const wasTriggered = getGlobalStorage(ext.context, 'PYTHON_WAS_DISCOVERY_TRIGGERED', false); + if (!wasTriggered.get()) { + api.triggerRefresh().ignoreErrors(); + wasTriggered.set(true).then(() => { + folders?.forEach(async (folder) => { + const wasTriggeredForFolder = getGlobalStorage( + ext.context, + `PYTHON_WAS_DISCOVERY_TRIGGERED_${normCasePath(folder.uri.fsPath)}`, + false, + ); + await wasTriggeredForFolder.set(true); + }); + }); + } else { + // Figure out which workspace folders need to be activated. + folders?.forEach(async (folder) => { + const wasTriggeredForFolder = getGlobalStorage( + ext.context, + `PYTHON_WAS_DISCOVERY_TRIGGERED_${normCasePath(folder.uri.fsPath)}`, + false, + ); + if (!wasTriggeredForFolder.get()) { + api.triggerRefresh({ + searchLocations: { roots: [folder.uri], doNotIncludeNonRooted: true }, + }).ignoreErrors(); + await wasTriggeredForFolder.set(true); + } + }); + } return { fullyReady: Promise.resolve(), From 98fed214dbc7f08f0dfb91eb491b6036358de802 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Fri, 13 May 2022 15:19:26 -0700 Subject: [PATCH 2/2] Oops --- src/client/pythonEnvironments/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/client/pythonEnvironments/index.ts b/src/client/pythonEnvironments/index.ts index e7726b1549ed..3f2b1748d095 100644 --- a/src/client/pythonEnvironments/index.ts +++ b/src/client/pythonEnvironments/index.ts @@ -56,9 +56,6 @@ export async function initialize(ext: ExtensionState): Promise { * Make use of the component (e.g. register with VS Code). */ export async function activate(api: IDiscoveryAPI, ext: ExtensionState): Promise { - return { - fullyReady: Promise.resolve(), - }; /** * Force an initial background refresh of the environments. *