forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwatcher.ts
More file actions
404 lines (350 loc) · 17.5 KB
/
watcher.ts
File metadata and controls
404 lines (350 loc) · 17.5 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { inject, injectable } from 'inversify';
import { ConfigurationChangeEvent, l10n, Uri, WorkspaceFoldersChangeEvent } from 'vscode';
import { LanguageServerChangeHandler } from '../activation/common/languageServerChangeHandler';
import { IExtensionActivationService, ILanguageServerOutputChannel, LanguageServerType } from '../activation/types';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../common/application/types';
import { IFileSystem } from '../common/platform/types';
import {
IConfigurationService,
IDisposableRegistry,
IExperimentService,
IExtensions,
IInterpreterPathService,
InterpreterConfigurationScope,
Resource,
} from '../common/types';
import { LanguageService } from '../common/utils/localize';
import { IEnvironmentVariablesProvider } from '../common/variables/types';
import { IInterpreterHelper, IInterpreterService } from '../interpreter/contracts';
import { IServiceContainer } from '../ioc/types';
import { traceLog } from '../logging';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { JediLSExtensionManager } from './jediLSExtensionManager';
import { NoneLSExtensionManager } from './noneLSExtensionManager';
import { PylanceLSExtensionManager } from './pylanceLSExtensionManager';
import { ILanguageServerExtensionManager, ILanguageServerWatcher } from './types';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { StopWatch } from '../common/utils/stopWatch';
@injectable()
/**
* The Language Server Watcher class implements the ILanguageServerWatcher interface, which is the one-stop shop for language server activation.
*/
export class LanguageServerWatcher implements IExtensionActivationService, ILanguageServerWatcher {
public readonly supportedWorkspaceTypes = { untrustedWorkspace: true, virtualWorkspace: true };
languageServerExtensionManager: ILanguageServerExtensionManager | undefined;
languageServerType: LanguageServerType;
private workspaceInterpreters: Map<string, PythonEnvironment | undefined>;
// In a multiroot workspace scenario we may have multiple language servers running:
// When using Jedi, there will be one language server per workspace folder.
// When using Pylance, there will only be one language server for the project.
private workspaceLanguageServers: Map<string, ILanguageServerExtensionManager | undefined>;
private registered = false;
constructor(
@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer,
@inject(ILanguageServerOutputChannel) private readonly lsOutputChannel: ILanguageServerOutputChannel,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(IExperimentService) private readonly experimentService: IExperimentService,
@inject(IInterpreterHelper) private readonly interpreterHelper: IInterpreterHelper,
@inject(IInterpreterPathService) private readonly interpreterPathService: IInterpreterPathService,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IEnvironmentVariablesProvider) private readonly environmentService: IEnvironmentVariablesProvider,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IFileSystem) private readonly fileSystem: IFileSystem,
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IApplicationShell) readonly applicationShell: IApplicationShell,
@inject(IDisposableRegistry) readonly disposables: IDisposableRegistry,
) {
this.workspaceInterpreters = new Map();
this.workspaceLanguageServers = new Map();
this.languageServerType = this.configurationService.getSettings().languageServer;
}
// IExtensionActivationService
public async activate(resource?: Resource, startupStopWatch?: StopWatch): Promise<void> {
this.register();
await this.startLanguageServer(this.languageServerType, resource, startupStopWatch);
}
// ILanguageServerWatcher
public async startLanguageServer(
languageServerType: LanguageServerType,
resource?: Resource,
startupStopWatch?: StopWatch,
): Promise<void> {
await this.startAndGetLanguageServer(languageServerType, resource, startupStopWatch);
}
public register(): void {
if (!this.registered) {
this.registered = true;
this.disposables.push(
this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)),
);
this.disposables.push(
this.workspaceService.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders.bind(this)),
);
this.disposables.push(
this.interpreterService.onDidChangeInterpreterInformation(this.onDidChangeInterpreterInformation, this),
);
if (this.workspaceService.isTrusted) {
this.disposables.push(this.interpreterPathService.onDidChange(this.onDidChangeInterpreter.bind(this)));
}
this.disposables.push(
this.extensions.onDidChange(async () => {
await this.extensionsChangeHandler();
}),
);
this.disposables.push(
new LanguageServerChangeHandler(
this.languageServerType,
this.extensions,
this.applicationShell,
this.commandManager,
this.workspaceService,
this.configurationService,
),
);
}
}
private async startAndGetLanguageServer(
languageServerType: LanguageServerType,
resource?: Resource,
startupStopWatch?: StopWatch,
): Promise<ILanguageServerExtensionManager> {
const lsResource = this.getWorkspaceUri(resource);
const currentInterpreter = this.workspaceInterpreters.get(lsResource.fsPath);
const interpreter = await this.interpreterService?.getActiveInterpreter(resource);
// Destroy the old language server if it's different.
if (currentInterpreter && interpreter !== currentInterpreter) {
await this.stopLanguageServer(lsResource);
}
// If the interpreter is Python 2 and the LS setting is explicitly set to Jedi, turn it off.
// If set to Default, use Pylance.
let serverType = languageServerType;
if (interpreter && (interpreter.version?.major ?? 0) < 3) {
if (serverType === LanguageServerType.Jedi) {
serverType = LanguageServerType.None;
} else if (this.getCurrentLanguageServerTypeIsDefault()) {
serverType = LanguageServerType.Node;
}
}
if (
!this.workspaceService.isTrusted &&
serverType !== LanguageServerType.Node &&
serverType !== LanguageServerType.None
) {
traceLog(LanguageService.untrustedWorkspaceMessage);
serverType = LanguageServerType.None;
}
// If the language server type is Pylance or None,
// We only need to instantiate the language server once, even in multiroot workspace scenarios,
// so we only need one language server extension manager.
const key = this.getWorkspaceKey(resource, serverType);
let languageServer = this.workspaceLanguageServers.get(key);
if (!languageServer || serverType === LanguageServerType.Jedi || serverType === LanguageServerType.JediLSP) {
languageServer = this.createLanguageServer(serverType);
this.workspaceLanguageServers.set(key, languageServer);
}
if (languageServer.canStartLanguageServer(interpreter)) {
// Start the language server.
if (startupStopWatch) {
// It means that startup is triggering this code, track time it takes since startup to activate this code.
sendTelemetryEvent(EventName.LANGUAGE_SERVER_TRIGGER_TIME, startupStopWatch.elapsedTime, {
triggerTime: startupStopWatch.elapsedTime,
});
}
await languageServer.startLanguageServer(lsResource, interpreter);
logStartup(languageServerType, lsResource);
this.languageServerType = languageServerType;
this.workspaceInterpreters.set(lsResource.fsPath, interpreter);
} else {
await languageServer.languageServerNotAvailable();
}
return languageServer;
}
public async restartLanguageServers(): Promise<void> {
this.workspaceLanguageServers.forEach(async (_, resourceString) => {
sendTelemetryEvent(EventName.LANGUAGE_SERVER_RESTART, undefined, { reason: 'notebooksExperiment' });
const resource = Uri.parse(resourceString);
await this.stopLanguageServer(resource);
await this.startLanguageServer(this.languageServerType, resource);
});
}
public async get(resource?: Resource): Promise<ILanguageServerExtensionManager> {
const key = this.getWorkspaceKey(resource, this.languageServerType);
let languageServerExtensionManager = this.workspaceLanguageServers.get(key);
if (!languageServerExtensionManager) {
languageServerExtensionManager = await this.startAndGetLanguageServer(this.languageServerType, resource);
}
return Promise.resolve(languageServerExtensionManager);
}
// Private methods
private async stopLanguageServer(resource?: Resource): Promise<void> {
const key = this.getWorkspaceKey(resource, this.languageServerType);
const languageServerExtensionManager = this.workspaceLanguageServers.get(key);
if (languageServerExtensionManager) {
await languageServerExtensionManager.stopLanguageServer();
languageServerExtensionManager.dispose();
this.workspaceLanguageServers.delete(key);
}
}
private createLanguageServer(languageServerType: LanguageServerType): ILanguageServerExtensionManager {
let lsManager: ILanguageServerExtensionManager;
switch (languageServerType) {
case LanguageServerType.Jedi:
lsManager = new JediLSExtensionManager(
this.serviceContainer,
this.lsOutputChannel,
this.experimentService,
this.workspaceService,
this.configurationService,
this.interpreterPathService,
this.interpreterService,
this.environmentService,
this.commandManager,
);
break;
case LanguageServerType.Node:
lsManager = new PylanceLSExtensionManager(
this.serviceContainer,
this.lsOutputChannel,
this.experimentService,
this.workspaceService,
this.configurationService,
this.interpreterPathService,
this.interpreterService,
this.environmentService,
this.commandManager,
this.fileSystem,
this.extensions,
this.applicationShell,
);
break;
case LanguageServerType.None:
default:
lsManager = new NoneLSExtensionManager();
break;
}
this.disposables.push({
dispose: async () => {
await lsManager.stopLanguageServer();
lsManager.dispose();
},
});
return lsManager;
}
private async refreshLanguageServer(resource?: Resource, forced?: boolean): Promise<void> {
const lsResource = this.getWorkspaceUri(resource);
const languageServerType = this.configurationService.getSettings(lsResource).languageServer;
if (languageServerType !== this.languageServerType || forced) {
await this.stopLanguageServer(resource);
await this.startLanguageServer(languageServerType, lsResource);
}
}
private getCurrentLanguageServerTypeIsDefault(): boolean {
return this.configurationService.getSettings().languageServerIsDefault;
}
// Watch for settings changes.
private async onDidChangeConfiguration(event: ConfigurationChangeEvent): Promise<void> {
const workspacesUris = this.workspaceService.workspaceFolders?.map((workspace) => workspace.uri) ?? [];
workspacesUris.forEach(async (resource) => {
if (event.affectsConfiguration(`python.languageServer`, resource)) {
await this.refreshLanguageServer(resource);
} else if (event.affectsConfiguration(`python.analysis.pylanceLspClientEnabled`, resource)) {
await this.refreshLanguageServer(resource, /* forced */ true);
} else if (event.affectsConfiguration(`python.pyrefly.disableLanguageServices`, resource)) {
await this.refreshLanguageServer(resource);
}
});
}
// Watch for interpreter changes.
private async onDidChangeInterpreter(event: InterpreterConfigurationScope): Promise<void> {
if (this.languageServerType === LanguageServerType.Node) {
// Pylance client already handles interpreter changes, so restarting LS can be skipped.
return Promise.resolve();
}
// Reactivate the language server (if in a multiroot workspace scenario, pick the correct one).
return this.activate(event.uri);
}
// Watch for interpreter information changes.
private async onDidChangeInterpreterInformation(info: PythonEnvironment): Promise<void> {
if (!info.envPath || info.envPath === '') {
return;
}
// Find the interpreter and workspace that got updated (if any).
const iterator = this.workspaceInterpreters.entries();
let result = iterator.next();
let done = result.done || false;
while (!done) {
const [resourcePath, interpreter] = result.value as [string, PythonEnvironment | undefined];
const resource = Uri.parse(resourcePath);
// Restart the language server if the interpreter path changed (#18995).
if (info.envPath === interpreter?.envPath && info.path !== interpreter?.path) {
await this.activate(resource);
done = true;
} else {
result = iterator.next();
done = result.done || false;
}
}
}
// Watch for extension changes.
private async extensionsChangeHandler(): Promise<void> {
const languageServerType = this.configurationService.getSettings().languageServer;
if (languageServerType !== this.languageServerType) {
await this.refreshLanguageServer();
}
}
// Watch for workspace folder changes.
private async onDidChangeWorkspaceFolders(event: WorkspaceFoldersChangeEvent): Promise<void> {
// Since Jedi is the only language server type where we instantiate multiple language servers,
// Make sure to dispose of them only in that scenario.
if (event.removed.length && this.languageServerType === LanguageServerType.Jedi) {
for (const workspace of event.removed) {
await this.stopLanguageServer(workspace.uri);
}
}
}
// Get the workspace Uri for the given resource, in order to query this.workspaceInterpreters and this.workspaceLanguageServers.
private getWorkspaceUri(resource?: Resource): Uri {
let uri;
if (resource) {
uri = this.workspaceService.getWorkspaceFolder(resource)?.uri;
} else {
uri = this.interpreterHelper.getActiveWorkspaceUri(resource)?.folderUri;
}
return uri ?? Uri.parse('default');
}
// Get the key used to identify which language server extension manager is associated to which workspace.
// When using Pylance or having no LS enabled, we return a static key since there should only be one LS extension manager for these LS types.
private getWorkspaceKey(resource: Resource | undefined, languageServerType: LanguageServerType): string {
switch (languageServerType) {
case LanguageServerType.Node:
return 'Pylance';
case LanguageServerType.None:
return 'None';
default:
return this.getWorkspaceUri(resource).fsPath;
}
}
}
function logStartup(languageServerType: LanguageServerType, resource: Uri): void {
let outputLine;
const basename = path.basename(resource.fsPath);
switch (languageServerType) {
case LanguageServerType.Jedi:
outputLine = l10n.t('Starting Jedi language server for {0}.', basename);
break;
case LanguageServerType.Node:
outputLine = LanguageService.startingPylance;
break;
case LanguageServerType.None:
outputLine = LanguageService.startingNone;
break;
default:
throw new Error(`Unknown language server type: ${languageServerType}`);
}
traceLog(outputLine);
}