Skip to content

Only trigger auto environment discovery once in the first session for a particular scope #19145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1 Enhancements/19102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only trigger auto environment discovery once in the first session for a particular scope (a workspace folder or globally).
49 changes: 38 additions & 11 deletions src/client/pythonEnvironments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,20 +55,44 @@ export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
/**
* Make use of the component (e.g. register with VS Code).
*/
export async function activate(api: IDiscoveryAPI, _ext: ExtensionState): Promise<ActivationResult> {
export async function activate(api: IDiscoveryAPI, ext: ExtensionState): Promise<ActivationResult> {
/**
* 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<boolean>(ext.context, 'PYTHON_WAS_DISCOVERY_TRIGGERED', false);
if (!wasTriggered.get()) {
api.triggerRefresh().ignoreErrors();
wasTriggered.set(true).then(() => {
folders?.forEach(async (folder) => {
const wasTriggeredForFolder = getGlobalStorage<boolean>(
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<boolean>(
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(),
Expand Down