@@ -12,7 +12,10 @@ import { PythonEnvsReducer } from './base/locators/composite/envsReducer';
1212import { PythonEnvsResolver } from './base/locators/composite/envsResolver' ;
1313import { WindowsPathEnvVarLocator } from './base/locators/lowLevel/windowsKnownPathsLocator' ;
1414import { 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' ;
1619import { ExtensionLocators , WatchRootsArgs , WorkspaceLocators } from './base/locators/wrappers' ;
1720import { CustomVirtualEnvironmentLocator } from './base/locators/lowLevel/customVirtualEnvLocator' ;
1821import { 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