forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.ts
224 lines (201 loc) · 10.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, named } from 'inversify';
import { Event, EventEmitter, Uri } from 'vscode';
import { IWorkspaceService } from '../../common/application/types';
import '../../common/extensions';
import { IFileSystem } from '../../common/platform/types';
import { IPersistentState, IPersistentStateFactory, Resource } from '../../common/types';
import { createDeferred, Deferred } from '../../common/utils/async';
import { compareSemVerLikeVersions } from '../../pythonEnvironments/base/info/pythonVersion';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { captureTelemetry, sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { IInterpreterHelper } from '../contracts';
import {
AutoSelectionRule,
IInterpreterAutoSelectionRule,
IInterpreterAutoSelectionService,
IInterpreterAutoSelectionProxyService,
} from './types';
const preferredGlobalInterpreter = 'preferredGlobalPyInterpreter';
const workspacePathNameForGlobalWorkspaces = '';
@injectable()
export class InterpreterAutoSelectionService implements IInterpreterAutoSelectionService {
protected readonly autoSelectedWorkspacePromises = new Map<string, Deferred<void>>();
private readonly didAutoSelectedInterpreterEmitter = new EventEmitter<void>();
private readonly autoSelectedInterpreterByWorkspace = new Map<string, PythonEnvironment | undefined>();
private globallyPreferredInterpreter: IPersistentState<
PythonEnvironment | undefined
> = this.stateFactory.createGlobalPersistentState<PythonEnvironment | undefined>(
preferredGlobalInterpreter,
undefined,
);
private readonly rules: IInterpreterAutoSelectionRule[] = [];
constructor(
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IPersistentStateFactory) private readonly stateFactory: IPersistentStateFactory,
@inject(IFileSystem) private readonly fs: IFileSystem,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.systemWide)
systemInterpreter: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.currentPath)
currentPathInterpreter: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.windowsRegistry)
winRegInterpreter: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.cachedInterpreters)
cachedPaths: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.settings)
private readonly userDefinedInterpreter: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionRule)
@named(AutoSelectionRule.workspaceVirtualEnvs)
workspaceInterpreter: IInterpreterAutoSelectionRule,
@inject(IInterpreterAutoSelectionProxyService) proxy: IInterpreterAutoSelectionProxyService,
@inject(IInterpreterHelper) private readonly interpreterHelper: IInterpreterHelper,
) {
// It is possible we area always opening the same workspace folder, but we still need to determine and cache
// the best available interpreters based on other rules (cache for furture use).
this.rules.push(
...[
winRegInterpreter,
currentPathInterpreter,
systemInterpreter,
cachedPaths,
userDefinedInterpreter,
workspaceInterpreter,
],
);
proxy.registerInstance!(this);
// Rules are as follows in order
// 1. First check user settings.json
// If we have user settings, then always use that, do not proceed.
// 2. Check workspace virtual environments (pipenv, etc).
// If we have some, then use those as preferred workspace environments.
// 3. Check list of cached interpreters (previously cachced from all the rules).
// If we find a good one, use that as preferred global env.
// Provided its better than what we have already cached as globally preffered interpreter (globallyPreferredInterpreter).
// 4. Check current path.
// If we find a good one, use that as preferred global env.
// Provided its better than what we have already cached as globally preffered interpreter (globallyPreferredInterpreter).
// 5. Check windows registry.
// If we find a good one, use that as preferred global env.
// Provided its better than what we have already cached as globally preffered interpreter (globallyPreferredInterpreter).
// 6. Check the entire system.
// If we find a good one, use that as preferred global env.
// Provided its better than what we have already cached as globally preffered interpreter (globallyPreferredInterpreter).
userDefinedInterpreter.setNextRule(workspaceInterpreter);
workspaceInterpreter.setNextRule(cachedPaths);
cachedPaths.setNextRule(currentPathInterpreter);
currentPathInterpreter.setNextRule(winRegInterpreter);
winRegInterpreter.setNextRule(systemInterpreter);
}
@captureTelemetry(EventName.PYTHON_INTERPRETER_AUTO_SELECTION, { rule: AutoSelectionRule.all }, true)
public async autoSelectInterpreter(resource: Resource): Promise<void> {
const key = this.getWorkspacePathKey(resource);
if (!this.autoSelectedWorkspacePromises.has(key)) {
const deferred = createDeferred<void>();
this.autoSelectedWorkspacePromises.set(key, deferred);
await this.initializeStore(resource);
await this.clearWorkspaceStoreIfInvalid(resource);
await this.userDefinedInterpreter.autoSelectInterpreter(resource, this);
this.didAutoSelectedInterpreterEmitter.fire();
Promise.all(this.rules.map((item) => item.autoSelectInterpreter(resource))).ignoreErrors();
deferred.resolve();
}
return this.autoSelectedWorkspacePromises.get(key)!.promise;
}
public get onDidChangeAutoSelectedInterpreter(): Event<void> {
return this.didAutoSelectedInterpreterEmitter.event;
}
public getAutoSelectedInterpreter(resource: Resource): PythonEnvironment | undefined {
// Do not execute anycode other than fetching fromm a property.
// This method gets invoked from settings class, and this class in turn uses classes that relies on settings.
// I.e. we can end up in a recursive loop.
const workspaceState = this.getWorkspaceState(resource);
if (workspaceState && workspaceState.value) {
return workspaceState.value;
}
const workspaceFolderPath = this.getWorkspacePathKey(resource);
if (this.autoSelectedInterpreterByWorkspace.has(workspaceFolderPath)) {
return this.autoSelectedInterpreterByWorkspace.get(workspaceFolderPath);
}
return this.globallyPreferredInterpreter.value;
}
public async setWorkspaceInterpreter(resource: Uri, interpreter: PythonEnvironment | undefined): Promise<void> {
await this.storeAutoSelectedInterpreter(resource, interpreter);
}
public async setGlobalInterpreter(interpreter: PythonEnvironment): Promise<void> {
await this.storeAutoSelectedInterpreter(undefined, interpreter);
}
protected async clearWorkspaceStoreIfInvalid(resource: Resource): Promise<void> {
const stateStore = this.getWorkspaceState(resource);
if (stateStore && stateStore.value && !(await this.fs.fileExists(stateStore.value.path))) {
sendTelemetryEvent(EventName.PYTHON_INTERPRETER_AUTO_SELECTION, {}, { interpreterMissing: true });
await stateStore.updateValue(undefined);
}
}
protected async storeAutoSelectedInterpreter(
resource: Resource,
interpreter: PythonEnvironment | undefined,
): Promise<void> {
const workspaceFolderPath = this.getWorkspacePathKey(resource);
if (workspaceFolderPath === workspacePathNameForGlobalWorkspaces) {
// Update store only if this version is better.
if (
this.globallyPreferredInterpreter.value &&
this.globallyPreferredInterpreter.value.version &&
interpreter &&
interpreter.version &&
compareSemVerLikeVersions(this.globallyPreferredInterpreter.value.version, interpreter.version) > 0
) {
return;
}
// Don't pass in manager instance, as we don't want any updates to take place.
await this.globallyPreferredInterpreter.updateValue(interpreter);
this.autoSelectedInterpreterByWorkspace.set(workspaceFolderPath, interpreter);
} else {
const workspaceState = this.getWorkspaceState(resource);
if (workspaceState && interpreter) {
await workspaceState.updateValue(interpreter);
}
this.autoSelectedInterpreterByWorkspace.set(workspaceFolderPath, interpreter);
}
}
protected async initializeStore(resource: Resource): Promise<void> {
const workspaceFolderPath = this.getWorkspacePathKey(resource);
// Since we're initializing for this resource,
// Ensure any cached information for this workspace have been removed.
this.autoSelectedInterpreterByWorkspace.delete(workspaceFolderPath);
if (this.globallyPreferredInterpreter) {
return;
}
await this.clearStoreIfFileIsInvalid();
}
private async clearStoreIfFileIsInvalid() {
this.globallyPreferredInterpreter = this.stateFactory.createGlobalPersistentState<
PythonEnvironment | undefined
>(preferredGlobalInterpreter, undefined);
if (
this.globallyPreferredInterpreter.value &&
!(await this.fs.fileExists(this.globallyPreferredInterpreter.value.path))
) {
await this.globallyPreferredInterpreter.updateValue(undefined);
}
}
private getWorkspacePathKey(resource: Resource): string {
return this.workspaceService.getWorkspaceFolderIdentifier(resource, workspacePathNameForGlobalWorkspaces);
}
private getWorkspaceState(resource: Resource): undefined | IPersistentState<PythonEnvironment | undefined> {
const workspaceUri = this.interpreterHelper.getActiveWorkspaceUri(resource);
if (workspaceUri) {
const key = `autoSelectedWorkspacePythonInterpreter-${workspaceUri.folderUri.fsPath}`;
return this.stateFactory.createWorkspacePersistentState(key, undefined);
}
return undefined;
}
}