Skip to content

Commit 2de8125

Browse files
author
Kartik Raj
authored
Only trigger auto environment discovery once in the first session for a particular scope (#19145)
* Only trigger auto environment discovery once in the first session for a particular scope * Oops
1 parent 758af61 commit 2de8125

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

news/1 Enhancements/19102.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Only trigger auto environment discovery once in the first session for a particular scope (a workspace folder or globally).

src/client/pythonEnvironments/index.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { PythonEnvsReducer } from './base/locators/composite/envsReducer';
1212
import { PythonEnvsResolver } from './base/locators/composite/envsResolver';
1313
import { WindowsPathEnvVarLocator } from './base/locators/lowLevel/windowsKnownPathsLocator';
1414
import { WorkspaceVirtualEnvironmentLocator } from './base/locators/lowLevel/workspaceVirtualEnvLocator';
15-
import { initializeExternalDependencies as initializeLegacyExternalDependencies } from './common/externalDependencies';
15+
import {
16+
initializeExternalDependencies as initializeLegacyExternalDependencies,
17+
normCasePath,
18+
} from './common/externalDependencies';
1619
import { ExtensionLocators, WatchRootsArgs, WorkspaceLocators } from './base/locators/wrappers';
1720
import { CustomVirtualEnvironmentLocator } from './base/locators/lowLevel/customVirtualEnvLocator';
1821
import { CondaEnvironmentLocator } from './base/locators/lowLevel/condaLocator';
@@ -52,20 +55,44 @@ export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
5255
/**
5356
* Make use of the component (e.g. register with VS Code).
5457
*/
55-
export async function activate(api: IDiscoveryAPI, _ext: ExtensionState): Promise<ActivationResult> {
58+
export async function activate(api: IDiscoveryAPI, ext: ExtensionState): Promise<ActivationResult> {
5659
/**
5760
* Force an initial background refresh of the environments.
5861
*
59-
* Note API is ready to be queried only after a refresh has been triggered, and extension activation is blocked on API. So,
60-
* * If discovery was never triggered, we need to block extension activation on the refresh trigger.
61-
* * If discovery was already triggered, it maybe the case that this is a new workspace for which it hasn't been triggered yet.
62-
* So always trigger discovery as part of extension activation for now.
63-
*
64-
* TODO: https://github.com/microsoft/vscode-python/issues/17498
65-
* Once `onInterpretersChanged` event is exposed via API, we can probably expect extensions to rely on that and
66-
* discovery can be triggered after activation, especially in the second case.
62+
* Note API is ready to be queried only after a refresh has been triggered, and extension activation is
63+
* blocked on API being ready. So if discovery was never triggered for a scope, we need to block
64+
* extension activation on the "refresh trigger".
6765
*/
68-
api.triggerRefresh().ignoreErrors();
66+
const folders = vscode.workspace.workspaceFolders;
67+
const wasTriggered = getGlobalStorage<boolean>(ext.context, 'PYTHON_WAS_DISCOVERY_TRIGGERED', false);
68+
if (!wasTriggered.get()) {
69+
api.triggerRefresh().ignoreErrors();
70+
wasTriggered.set(true).then(() => {
71+
folders?.forEach(async (folder) => {
72+
const wasTriggeredForFolder = getGlobalStorage<boolean>(
73+
ext.context,
74+
`PYTHON_WAS_DISCOVERY_TRIGGERED_${normCasePath(folder.uri.fsPath)}`,
75+
false,
76+
);
77+
await wasTriggeredForFolder.set(true);
78+
});
79+
});
80+
} else {
81+
// Figure out which workspace folders need to be activated.
82+
folders?.forEach(async (folder) => {
83+
const wasTriggeredForFolder = getGlobalStorage<boolean>(
84+
ext.context,
85+
`PYTHON_WAS_DISCOVERY_TRIGGERED_${normCasePath(folder.uri.fsPath)}`,
86+
false,
87+
);
88+
if (!wasTriggeredForFolder.get()) {
89+
api.triggerRefresh({
90+
searchLocations: { roots: [folder.uri], doNotIncludeNonRooted: true },
91+
}).ignoreErrors();
92+
await wasTriggeredForFolder.set(true);
93+
}
94+
});
95+
}
6996

7097
return {
7198
fullyReady: Promise.resolve(),

0 commit comments

Comments
 (0)