@@ -19,7 +19,7 @@ import {
1919 AutoSelectionRule ,
2020 IInterpreterAutoSelectionRule ,
2121 IInterpreterAutoSelectionService ,
22- IInterpreterAutoSeletionProxyService ,
22+ IInterpreterAutoSelectionProxyService ,
2323 IInterpreterSecurityService ,
2424} from './types' ;
2525
@@ -29,10 +29,15 @@ const workspacePathNameForGlobalWorkspaces = '';
2929@injectable ( )
3030export class InterpreterAutoSelectionService implements IInterpreterAutoSelectionService {
3131 protected readonly autoSelectedWorkspacePromises = new Map < string , Deferred < void > > ( ) ;
32+
3233 private readonly didAutoSelectedInterpreterEmitter = new EventEmitter < void > ( ) ;
34+
3335 private readonly autoSelectedInterpreterByWorkspace = new Map < string , PythonEnvironment | undefined > ( ) ;
36+
3437 private globallyPreferredInterpreter ! : IPersistentState < PythonEnvironment | undefined > ;
38+
3539 private readonly rules : IInterpreterAutoSelectionRule [ ] = [ ] ;
40+
3641 constructor (
3742 @inject ( IWorkspaceService ) private readonly workspaceService : IWorkspaceService ,
3843 @inject ( IPersistentStateFactory ) private readonly stateFactory : IPersistentStateFactory ,
@@ -55,7 +60,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
5560 @inject ( IInterpreterAutoSelectionRule )
5661 @named ( AutoSelectionRule . workspaceVirtualEnvs )
5762 workspaceInterpreter : IInterpreterAutoSelectionRule ,
58- @inject ( IInterpreterAutoSeletionProxyService ) proxy : IInterpreterAutoSeletionProxyService ,
63+ @inject ( IInterpreterAutoSelectionProxyService ) proxy : IInterpreterAutoSelectionProxyService ,
5964 @inject ( IInterpreterHelper ) private readonly interpreterHelper : IInterpreterHelper ,
6065 @inject ( IInterpreterSecurityService ) private readonly interpreterSecurityService : IInterpreterSecurityService ,
6166 ) {
@@ -95,6 +100,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
95100 currentPathInterpreter . setNextRule ( winRegInterpreter ) ;
96101 winRegInterpreter . setNextRule ( systemInterpreter ) ;
97102 }
103+
98104 @captureTelemetry ( EventName . PYTHON_INTERPRETER_AUTO_SELECTION , { rule : AutoSelectionRule . all } , true )
99105 public async autoSelectInterpreter ( resource : Resource ) : Promise < void > {
100106 const key = this . getWorkspacePathKey ( resource ) ;
@@ -110,9 +116,11 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
110116 }
111117 return this . autoSelectedWorkspacePromises . get ( key ) ! . promise ;
112118 }
119+
113120 public get onDidChangeAutoSelectedInterpreter ( ) : Event < void > {
114121 return this . didAutoSelectedInterpreterEmitter . event ;
115122 }
123+
116124 public getAutoSelectedInterpreter ( resource : Resource ) : PythonEnvironment | undefined {
117125 // Do not execute anycode other than fetching fromm a property.
118126 // This method gets invoked from settings class, and this class in turn uses classes that relies on settings.
@@ -135,20 +143,27 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
135143
136144 return this . globallyPreferredInterpreter . value ;
137145 }
138- public async setWorkspaceInterpreter ( resource : Uri , interpreter : PythonEnvironment | undefined ) {
146+
147+ public async setWorkspaceInterpreter ( resource : Uri , interpreter : PythonEnvironment | undefined ) : Promise < void > {
139148 await this . storeAutoSelectedInterpreter ( resource , interpreter ) ;
140149 }
141- public async setGlobalInterpreter ( interpreter : PythonEnvironment ) {
150+
151+ public async setGlobalInterpreter ( interpreter : PythonEnvironment ) : Promise < void > {
142152 await this . storeAutoSelectedInterpreter ( undefined , interpreter ) ;
143153 }
144- protected async clearWorkspaceStoreIfInvalid ( resource : Resource ) {
154+
155+ protected async clearWorkspaceStoreIfInvalid ( resource : Resource ) : Promise < void > {
145156 const stateStore = this . getWorkspaceState ( resource ) ;
146157 if ( stateStore && stateStore . value && ! ( await this . fs . fileExists ( stateStore . value . path ) ) ) {
147158 sendTelemetryEvent ( EventName . PYTHON_INTERPRETER_AUTO_SELECTION , { } , { interpreterMissing : true } ) ;
148159 await stateStore . updateValue ( undefined ) ;
149160 }
150161 }
151- protected async storeAutoSelectedInterpreter ( resource : Resource , interpreter : PythonEnvironment | undefined ) {
162+
163+ protected async storeAutoSelectedInterpreter (
164+ resource : Resource ,
165+ interpreter : PythonEnvironment | undefined ,
166+ ) : Promise < void > {
152167 const workspaceFolderPath = this . getWorkspacePathKey ( resource ) ;
153168 if ( workspaceFolderPath === workspacePathNameForGlobalWorkspaces ) {
154169 // Update store only if this version is better.
@@ -173,7 +188,8 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
173188 this . autoSelectedInterpreterByWorkspace . set ( workspaceFolderPath , interpreter ) ;
174189 }
175190 }
176- protected async initializeStore ( resource : Resource ) {
191+
192+ protected async initializeStore ( resource : Resource ) : Promise < void > {
177193 const workspaceFolderPath = this . getWorkspacePathKey ( resource ) ;
178194 // Since we're initializing for this resource,
179195 // Ensure any cached information for this workspace have been removed.
@@ -183,6 +199,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
183199 }
184200 await this . clearStoreIfFileIsInvalid ( ) ;
185201 }
202+
186203 private async clearStoreIfFileIsInvalid ( ) {
187204 this . globallyPreferredInterpreter = this . stateFactory . createGlobalPersistentState <
188205 PythonEnvironment | undefined
@@ -194,15 +211,17 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
194211 await this . globallyPreferredInterpreter . updateValue ( undefined ) ;
195212 }
196213 }
214+
197215 private getWorkspacePathKey ( resource : Resource ) : string {
198216 return this . workspaceService . getWorkspaceFolderIdentifier ( resource , workspacePathNameForGlobalWorkspaces ) ;
199217 }
218+
200219 private getWorkspaceState ( resource : Resource ) : undefined | IPersistentState < PythonEnvironment | undefined > {
201220 const workspaceUri = this . interpreterHelper . getActiveWorkspaceUri ( resource ) ;
202- if ( ! workspaceUri ) {
203- return ;
221+ if ( workspaceUri ) {
222+ const key = `autoSelectedWorkspacePythonInterpreter-${ workspaceUri . folderUri . fsPath } ` ;
223+ return this . stateFactory . createWorkspacePersistentState ( key , undefined ) ;
204224 }
205- const key = `autoSelectedWorkspacePythonInterpreter-${ workspaceUri . folderUri . fsPath } ` ;
206- return this . stateFactory . createWorkspacePersistentState ( key , undefined ) ;
225+ return undefined ;
207226 }
208227}
0 commit comments