-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtypescriptServiceClient.ts
1307 lines (1120 loc) · 45 KB
/
typescriptServiceClient.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { homedir } from 'os';
import * as path from 'path';
import * as vscode from 'vscode';
import { ServiceConfigurationProvider, SyntaxServerConfiguration, TsServerLogLevel, TypeScriptServiceConfiguration, areServiceConfigurationsEqual } from './configuration/configuration';
import * as fileSchemes from './configuration/fileSchemes';
import { Schemes } from './configuration/schemes';
import { IExperimentationTelemetryReporter } from './experimentTelemetryReporter';
import { DiagnosticKind, DiagnosticsManager } from './languageFeatures/diagnostics';
import { Logger } from './logging/logger';
import { TelemetryReporter, VSCodeTelemetryReporter } from './logging/telemetry';
import Tracer from './logging/tracer';
import { ProjectType, inferredProjectCompilerOptions } from './tsconfig';
import { API } from './tsServer/api';
import BufferSyncSupport from './tsServer/bufferSyncSupport';
import { OngoingRequestCancellerFactory } from './tsServer/cancellation';
import { ILogDirectoryProvider } from './tsServer/logDirectoryProvider';
import { NodeVersionManager } from './tsServer/nodeManager';
import { TypeScriptPluginPathsProvider } from './tsServer/pluginPathsProvider';
import { PluginManager } from './tsServer/plugins';
import * as Proto from './tsServer/protocol/protocol';
import { EventName } from './tsServer/protocol/protocol.const';
import { ITypeScriptServer, TsServerLog, TsServerProcessFactory, TypeScriptServerExitEvent } from './tsServer/server';
import { TypeScriptServerError } from './tsServer/serverError';
import { TypeScriptServerSpawner } from './tsServer/spawner';
import { TypeScriptVersionManager } from './tsServer/versionManager';
import { ITypeScriptVersionProvider, TypeScriptVersion } from './tsServer/versionProvider';
import { ClientCapabilities, ClientCapability, ExecConfig, ITypeScriptServiceClient, ServerResponse, TypeScriptRequests } from './typescriptService';
import { Disposable, DisposableStore, disposeAll } from './utils/dispose';
import { hash } from './utils/hash';
import { isWeb, isWebAndHasSharedArrayBuffers } from './utils/platform';
export interface TsDiagnostics {
readonly kind: DiagnosticKind;
readonly resource: vscode.Uri;
readonly diagnostics: Proto.Diagnostic[];
readonly spans?: Proto.TextSpan[];
}
interface ToCancelOnResourceChanged {
readonly resource: vscode.Uri;
cancel(): void;
}
namespace ServerState {
export const enum Type {
None,
Running,
Errored
}
export const None = { type: Type.None } as const;
export class Running {
readonly type = Type.Running;
constructor(
public readonly server: ITypeScriptServer,
/**
* API version obtained from the version picker after checking the corresponding path exists.
*/
public readonly apiVersion: API,
/**
* Version reported by currently-running tsserver.
*/
public tsserverVersion: string | undefined,
public languageServiceEnabled: boolean,
) { }
public readonly toCancelOnResourceChange = new Set<ToCancelOnResourceChanged>();
updateTsserverVersion(tsserverVersion: string) {
this.tsserverVersion = tsserverVersion;
}
updateLanguageServiceEnabled(enabled: boolean) {
this.languageServiceEnabled = enabled;
}
}
export class Errored {
readonly type = Type.Errored;
constructor(
public readonly error: Error,
public readonly tsServerLog: TsServerLog | undefined,
) { }
}
export type State = typeof None | Running | Errored;
}
export const emptyAuthority = 'ts-nul-authority';
export const inMemoryResourcePrefix = '^';
interface WatchEvent {
updated?: Set<string>;
created?: Set<string>;
deleted?: Set<string>;
}
export default class TypeScriptServiceClient extends Disposable implements ITypeScriptServiceClient {
private readonly _onReady?: { promise: Promise<void>; resolve: () => void; reject: () => void };
private _configuration: TypeScriptServiceConfiguration;
private readonly pluginPathsProvider: TypeScriptPluginPathsProvider;
private readonly _versionManager: TypeScriptVersionManager;
private readonly _nodeVersionManager: NodeVersionManager;
private readonly logger: Logger;
private readonly tracer: Tracer;
private readonly typescriptServerSpawner: TypeScriptServerSpawner;
private serverState: ServerState.State = ServerState.None;
private lastStart: number;
private numberRestarts: number;
private _isPromptingAfterCrash = false;
private isRestarting: boolean = false;
private hasServerFatallyCrashedTooManyTimes = false;
private readonly loadingIndicator: ServerInitializingIndicator;
public readonly telemetryReporter: TelemetryReporter;
public readonly bufferSyncSupport: BufferSyncSupport;
public readonly diagnosticsManager: DiagnosticsManager;
public readonly pluginManager: PluginManager;
private readonly logDirectoryProvider: ILogDirectoryProvider;
private readonly cancellerFactory: OngoingRequestCancellerFactory;
private readonly versionProvider: ITypeScriptVersionProvider;
private readonly processFactory: TsServerProcessFactory;
private readonly watches = new Map<number, Disposable>();
private readonly watchEvents = new Map<number, WatchEvent>();
private watchChangeTimeout: NodeJS.Timeout | undefined;
constructor(
private readonly context: vscode.ExtensionContext,
onCaseInsensitiveFileSystem: boolean,
services: {
pluginManager: PluginManager;
logDirectoryProvider: ILogDirectoryProvider;
cancellerFactory: OngoingRequestCancellerFactory;
versionProvider: ITypeScriptVersionProvider;
processFactory: TsServerProcessFactory;
serviceConfigurationProvider: ServiceConfigurationProvider;
experimentTelemetryReporter: IExperimentationTelemetryReporter | undefined;
logger: Logger;
},
allModeIds: readonly string[]
) {
super();
this.loadingIndicator = this._register(new ServerInitializingIndicator(this));
this.logger = services.logger;
this.tracer = new Tracer(this.logger);
this.pluginManager = services.pluginManager;
this.logDirectoryProvider = services.logDirectoryProvider;
this.cancellerFactory = services.cancellerFactory;
this.versionProvider = services.versionProvider;
this.processFactory = services.processFactory;
this.lastStart = Date.now();
let resolve: () => void;
let reject: () => void;
const p = new Promise<void>((res, rej) => {
resolve = res;
reject = rej;
});
this._onReady = { promise: p, resolve: resolve!, reject: reject! };
this.numberRestarts = 0;
this._configuration = services.serviceConfigurationProvider.loadFromWorkspace();
this.versionProvider.updateConfiguration(this._configuration);
this.pluginPathsProvider = new TypeScriptPluginPathsProvider(this._configuration);
this._versionManager = this._register(new TypeScriptVersionManager(this._configuration, this.versionProvider, context.workspaceState));
this._register(this._versionManager.onDidPickNewVersion(() => {
this.restartTsServer();
}));
this._nodeVersionManager = this._register(new NodeVersionManager(this._configuration, context.workspaceState));
this._register(this._nodeVersionManager.onDidPickNewVersion(() => {
this.restartTsServer();
}));
this.bufferSyncSupport = new BufferSyncSupport(this, allModeIds, onCaseInsensitiveFileSystem);
this.onReady(() => { this.bufferSyncSupport.listen(); });
this.bufferSyncSupport.onDelete(resource => {
this.cancelInflightRequestsForResource(resource);
this.diagnosticsManager.deleteAllDiagnosticsInFile(resource);
}, null, this._disposables);
this.bufferSyncSupport.onWillChange(resource => {
this.cancelInflightRequestsForResource(resource);
});
vscode.workspace.onDidChangeConfiguration(() => {
const oldConfiguration = this._configuration;
this._configuration = services.serviceConfigurationProvider.loadFromWorkspace();
this.versionProvider.updateConfiguration(this._configuration);
this._versionManager.updateConfiguration(this._configuration);
this.pluginPathsProvider.updateConfiguration(this._configuration);
this._nodeVersionManager.updateConfiguration(this._configuration);
if (this.serverState.type === ServerState.Type.Running) {
if (!this._configuration.implicitProjectConfiguration.isEqualTo(oldConfiguration.implicitProjectConfiguration)) {
this.setCompilerOptionsForInferredProjects(this._configuration);
}
if (!areServiceConfigurationsEqual(this._configuration, oldConfiguration)) {
this.restartTsServer();
}
}
}, this, this._disposables);
this.telemetryReporter = new VSCodeTelemetryReporter(services.experimentTelemetryReporter, () => {
if (this.serverState.type === ServerState.Type.Running) {
if (this.serverState.tsserverVersion) {
return this.serverState.tsserverVersion;
}
}
return this.apiVersion.fullVersionString;
});
this.diagnosticsManager = new DiagnosticsManager('typescript', this._configuration, this.telemetryReporter, onCaseInsensitiveFileSystem);
this.typescriptServerSpawner = new TypeScriptServerSpawner(this.versionProvider, this._versionManager, this._nodeVersionManager, this.logDirectoryProvider, this.pluginPathsProvider, this.logger, this.telemetryReporter, this.tracer, this.processFactory);
this._register(this.pluginManager.onDidUpdateConfig(update => {
this.configurePlugin(update.pluginId, update.config);
}));
this._register(this.pluginManager.onDidChangePlugins(() => {
this.restartTsServer();
}));
}
public get capabilities() {
if (this._configuration.useSyntaxServer === SyntaxServerConfiguration.Always) {
return new ClientCapabilities(
ClientCapability.Syntax,
ClientCapability.EnhancedSyntax);
}
if (isWeb()) {
if (this.isProjectWideIntellisenseOnWebEnabled()) {
return new ClientCapabilities(
ClientCapability.Syntax,
ClientCapability.EnhancedSyntax,
ClientCapability.Semantic);
} else {
return new ClientCapabilities(
ClientCapability.Syntax,
ClientCapability.EnhancedSyntax);
}
}
if (this.apiVersion.gte(API.v400)) {
return new ClientCapabilities(
ClientCapability.Syntax,
ClientCapability.EnhancedSyntax,
ClientCapability.Semantic);
}
return new ClientCapabilities(
ClientCapability.Syntax,
ClientCapability.Semantic);
}
private readonly _onDidChangeCapabilities = this._register(new vscode.EventEmitter<void>());
readonly onDidChangeCapabilities = this._onDidChangeCapabilities.event;
private isProjectWideIntellisenseOnWebEnabled(): boolean {
return isWebAndHasSharedArrayBuffers() && this._configuration.webProjectWideIntellisenseEnabled;
}
private cancelInflightRequestsForResource(resource: vscode.Uri): void {
if (this.serverState.type !== ServerState.Type.Running) {
return;
}
for (const request of this.serverState.toCancelOnResourceChange) {
if (request.resource.toString() === resource.toString()) {
request.cancel();
}
}
}
public get configuration() {
return this._configuration;
}
public override dispose() {
super.dispose();
this.bufferSyncSupport.dispose();
if (this.serverState.type === ServerState.Type.Running) {
this.serverState.server.kill();
}
this.loadingIndicator.reset();
this.resetWatchers();
}
public restartTsServer(fromUserAction = false): void {
if (this.serverState.type === ServerState.Type.Running) {
this.logger.info('Killing TS Server');
this.isRestarting = true;
this.serverState.server.kill();
}
if (fromUserAction) {
// Reset crash trackers
this.hasServerFatallyCrashedTooManyTimes = false;
this.numberRestarts = 0;
this.lastStart = Date.now();
}
this.serverState = this.startService(true);
}
private readonly _onTsServerStarted = this._register(new vscode.EventEmitter<{ version: TypeScriptVersion; usedApiVersion: API }>());
public readonly onTsServerStarted = this._onTsServerStarted.event;
private readonly _onDiagnosticsReceived = this._register(new vscode.EventEmitter<TsDiagnostics>());
public readonly onDiagnosticsReceived = this._onDiagnosticsReceived.event;
private readonly _onConfigDiagnosticsReceived = this._register(new vscode.EventEmitter<Proto.ConfigFileDiagnosticEvent>());
public readonly onConfigDiagnosticsReceived = this._onConfigDiagnosticsReceived.event;
private readonly _onResendModelsRequested = this._register(new vscode.EventEmitter<void>());
public readonly onResendModelsRequested = this._onResendModelsRequested.event;
private readonly _onProjectLanguageServiceStateChanged = this._register(new vscode.EventEmitter<Proto.ProjectLanguageServiceStateEventBody>());
public readonly onProjectLanguageServiceStateChanged = this._onProjectLanguageServiceStateChanged.event;
private readonly _onDidBeginInstallTypings = this._register(new vscode.EventEmitter<Proto.BeginInstallTypesEventBody>());
public readonly onDidBeginInstallTypings = this._onDidBeginInstallTypings.event;
private readonly _onDidEndInstallTypings = this._register(new vscode.EventEmitter<Proto.EndInstallTypesEventBody>());
public readonly onDidEndInstallTypings = this._onDidEndInstallTypings.event;
private readonly _onTypesInstallerInitializationFailed = this._register(new vscode.EventEmitter<Proto.TypesInstallerInitializationFailedEventBody>());
public readonly onTypesInstallerInitializationFailed = this._onTypesInstallerInitializationFailed.event;
private readonly _onSurveyReady = this._register(new vscode.EventEmitter<Proto.SurveyReadyEventBody>());
public readonly onSurveyReady = this._onSurveyReady.event;
public get apiVersion(): API {
if (this.serverState.type === ServerState.Type.Running) {
return this.serverState.apiVersion;
}
return API.defaultVersion;
}
public onReady(f: () => void): Promise<void> {
return this._onReady!.promise.then(f);
}
public ensureServiceStarted() {
if (this.serverState.type !== ServerState.Type.Running) {
this.startService();
}
}
private token: number = 0;
private startService(resendModels: boolean = false): ServerState.State {
this.logger.info(`Starting TS Server`);
if (this.isDisposed) {
this.logger.info(`Not starting server: disposed`);
return ServerState.None;
}
if (this.hasServerFatallyCrashedTooManyTimes) {
this.logger.info(`Not starting server: too many crashes`);
return ServerState.None;
}
let version = this._versionManager.currentVersion;
if (!version.isValid) {
vscode.window.showWarningMessage(vscode.l10n.t("The path {0} doesn't point to a valid tsserver install. Falling back to bundled TypeScript version.", version.path));
this._versionManager.reset();
version = this._versionManager.currentVersion;
}
this.logger.info(`Using tsserver from: ${version.path}`);
const nodePath = this._nodeVersionManager.currentVersion;
if (nodePath) {
this.logger.info(`Using Node installation from ${nodePath} to run TS Server`);
}
this.resetWatchers();
const apiVersion = version.apiVersion || API.defaultVersion;
const mytoken = ++this.token;
const handle = this.typescriptServerSpawner.spawn(version, this.capabilities, this.configuration, this.pluginManager, this.cancellerFactory, {
onFatalError: (command, err) => this.fatalError(command, err),
});
this.serverState = new ServerState.Running(handle, apiVersion, undefined, true);
this.lastStart = Date.now();
/* __GDPR__FRAGMENT__
"TypeScriptServerEnvCommonProperties" : {
"hasGlobalPlugins": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"globalPluginNameHashes": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
const typeScriptServerEnvCommonProperties = {
hasGlobalPlugins: this.pluginManager.plugins.length > 0,
globalPluginNameHashes: JSON.stringify(this.pluginManager.plugins.map(plugin => hash(plugin.name))),
};
/* __GDPR__
"tsserver.spawned" : {
"owner": "mjbvz",
"${include}": [
"${TypeScriptCommonProperties}",
"${TypeScriptServerEnvCommonProperties}"
],
"localTypeScriptVersion": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"typeScriptVersionSource": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
this.telemetryReporter.logTelemetry('tsserver.spawned', {
...typeScriptServerEnvCommonProperties,
localTypeScriptVersion: this.versionProvider.localVersion ? this.versionProvider.localVersion.displayName : '',
typeScriptVersionSource: version.source,
});
handle.onError((err: Error) => {
if (this.token !== mytoken) {
// this is coming from an old process
return;
}
if (err) {
vscode.window.showErrorMessage(vscode.l10n.t("TypeScript language server exited with error. Error message is: {0}", err.message || err.name));
}
this.serverState = new ServerState.Errored(err, handle.tsServerLog);
this.logger.error('TSServer errored with error.', err);
if (handle.tsServerLog?.type === 'file') {
this.logger.error(`TSServer log file: ${handle.tsServerLog.uri.fsPath}`);
}
/* __GDPR__
"tsserver.error" : {
"owner": "mjbvz",
"${include}": [
"${TypeScriptCommonProperties}",
"${TypeScriptServerEnvCommonProperties}"
]
}
*/
this.telemetryReporter.logTelemetry('tsserver.error', {
...typeScriptServerEnvCommonProperties
});
this.serviceExited(false, apiVersion);
});
handle.onExit((data: TypeScriptServerExitEvent) => {
const { code, signal } = data;
this.logger.error(`TSServer exited. Code: ${code}. Signal: ${signal}`);
// In practice, the exit code is an integer with no ties to any identity,
// so it can be classified as SystemMetaData, rather than CallstackOrException.
/* __GDPR__
"tsserver.exitWithCode" : {
"owner": "mjbvz",
"${include}": [
"${TypeScriptCommonProperties}",
"${TypeScriptServerEnvCommonProperties}"
],
"code" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" },
"signal" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" }
}
*/
this.telemetryReporter.logTelemetry('tsserver.exitWithCode', {
...typeScriptServerEnvCommonProperties,
code: code ?? undefined,
signal: signal ?? undefined,
});
if (this.token !== mytoken) {
// this is coming from an old process
return;
}
if (handle.tsServerLog?.type === 'file') {
this.logger.info(`TSServer log file: ${handle.tsServerLog.uri.fsPath}`);
}
this.serviceExited(!this.isRestarting, apiVersion);
this.isRestarting = false;
});
handle.onEvent(event => this.dispatchEvent(event));
this.serviceStarted(resendModels);
this._onReady!.resolve();
this._onTsServerStarted.fire({ version: version, usedApiVersion: apiVersion });
this._onDidChangeCapabilities.fire();
return this.serverState;
}
private resetWatchers() {
clearTimeout(this.watchChangeTimeout);
disposeAll(Array.from(this.watches.values()));
}
public async showVersionPicker(): Promise<void> {
this._versionManager.promptUserForVersion();
}
public async openTsServerLogFile(): Promise<boolean> {
if (this._configuration.tsServerLogLevel === TsServerLogLevel.Off) {
vscode.window.showErrorMessage<vscode.MessageItem>(
vscode.l10n.t("TS Server logging is off. Please set 'typescript.tsserver.log' and restart the TS server to enable logging"),
{
title: vscode.l10n.t("Enable logging and restart TS server"),
})
.then(selection => {
if (selection) {
return vscode.workspace.getConfiguration().update('typescript.tsserver.log', 'verbose', true).then(() => {
this.restartTsServer();
});
}
return undefined;
});
return false;
}
if (this.serverState.type !== ServerState.Type.Running || !this.serverState.server.tsServerLog) {
vscode.window.showWarningMessage(vscode.l10n.t("TS Server has not started logging."));
return false;
}
switch (this.serverState.server.tsServerLog.type) {
case 'output': {
this.serverState.server.tsServerLog.output.show();
return true;
}
case 'file': {
try {
const doc = await vscode.workspace.openTextDocument(this.serverState.server.tsServerLog.uri);
await vscode.window.showTextDocument(doc);
return true;
} catch {
// noop
}
try {
await vscode.commands.executeCommand('revealFileInOS', this.serverState.server.tsServerLog.uri);
return true;
} catch {
vscode.window.showWarningMessage(vscode.l10n.t("Could not open TS Server log file"));
return false;
}
}
}
}
private serviceStarted(resendModels: boolean): void {
this.bufferSyncSupport.reset();
const watchOptions = this.apiVersion.gte(API.v380)
? this.configuration.watchOptions
: undefined;
const configureOptions: Proto.ConfigureRequestArguments = {
hostInfo: 'vscode',
preferences: {
providePrefixAndSuffixTextForRename: true,
allowRenameOfImportPath: true,
includePackageJsonAutoImports: this._configuration.includePackageJsonAutoImports,
excludeLibrarySymbolsInNavTo: this._configuration.workspaceSymbolsExcludeLibrarySymbols,
},
watchOptions
};
this.executeWithoutWaitingForResponse('configure', configureOptions);
this.setCompilerOptionsForInferredProjects(this._configuration);
if (resendModels) {
this._onResendModelsRequested.fire();
this.bufferSyncSupport.reinitialize();
this.bufferSyncSupport.requestAllDiagnostics();
}
// Reconfigure any plugins
for (const [pluginName, config] of this.pluginManager.configurations()) {
this.configurePlugin(pluginName, config);
}
}
private setCompilerOptionsForInferredProjects(configuration: TypeScriptServiceConfiguration): void {
const args: Proto.SetCompilerOptionsForInferredProjectsArgs = {
options: this.getCompilerOptionsForInferredProjects(configuration)
};
this.executeWithoutWaitingForResponse('compilerOptionsForInferredProjects', args);
}
private getCompilerOptionsForInferredProjects(configuration: TypeScriptServiceConfiguration): Proto.ExternalProjectCompilerOptions {
return {
...inferredProjectCompilerOptions(this.apiVersion, ProjectType.TypeScript, configuration),
allowJs: true,
allowSyntheticDefaultImports: true,
allowNonTsExtensions: true,
resolveJsonModule: true,
};
}
private serviceExited(restart: boolean, tsVersion: API): void {
this.resetWatchers();
this.loadingIndicator.reset();
this.serverState = ServerState.None;
if (restart) {
const diff = Date.now() - this.lastStart;
this.numberRestarts++;
let startService = true;
const pluginExtensionList = this.pluginManager.plugins.map(plugin => plugin.extension.id).join(', ');
const reportIssueItem: vscode.MessageItem = {
title: vscode.l10n.t("Report Issue"),
};
let prompt: Thenable<undefined | vscode.MessageItem> | undefined = undefined;
if (this.numberRestarts > 5) {
this.numberRestarts = 0;
if (diff < 10 * 1000 /* 10 seconds */) {
this.lastStart = Date.now();
startService = false;
this.hasServerFatallyCrashedTooManyTimes = true;
if (this.pluginManager.plugins.length) {
prompt = vscode.window.showErrorMessage<vscode.MessageItem>(
vscode.l10n.t("The JS/TS language service immediately crashed 5 times. The service will not be restarted.\nThis may be caused by a plugin contributed by one of these extensions: {0}.\nPlease try disabling these extensions before filing an issue against VS Code.", pluginExtensionList));
} else {
prompt = vscode.window.showErrorMessage(
vscode.l10n.t("The JS/TS language service immediately crashed 5 times. The service will not be restarted."),
reportIssueItem);
}
/* __GDPR__
"serviceExited" : {
"owner": "mjbvz",
"${include}": [
"${TypeScriptCommonProperties}"
]
}
*/
this.telemetryReporter.logTelemetry('serviceExited');
} else if (diff < 60 * 1000 * 5 /* 5 Minutes */) {
this.lastStart = Date.now();
if (!this._isPromptingAfterCrash) {
if (this.pluginManager.plugins.length) {
prompt = vscode.window.showWarningMessage<vscode.MessageItem>(
vscode.l10n.t("The JS/TS language service crashed 5 times in the last 5 Minutes.\nThis may be caused by a plugin contributed by one of these extensions: {0}\nPlease try disabling these extensions before filing an issue against VS Code.", pluginExtensionList));
} else {
prompt = vscode.window.showWarningMessage(
vscode.l10n.t("The JS/TS language service crashed 5 times in the last 5 Minutes."),
reportIssueItem);
}
}
}
} else if (['vscode-insiders', 'code-oss'].includes(vscode.env.uriScheme)) {
// Prompt after a single restart
this.numberRestarts = 0;
if (!this._isPromptingAfterCrash) {
if (this.pluginManager.plugins.length) {
prompt = vscode.window.showWarningMessage<vscode.MessageItem>(
vscode.l10n.t("The JS/TS language service crashed.\nThis may be caused by a plugin contributed by one of these extensions: {0}.\nPlease try disabling these extensions before filing an issue against VS Code.", pluginExtensionList));
} else {
prompt = vscode.window.showWarningMessage(
vscode.l10n.t("The JS/TS language service crashed."),
reportIssueItem);
}
}
}
if (prompt) {
this._isPromptingAfterCrash = true;
}
prompt?.then(async item => {
this._isPromptingAfterCrash = false;
if (item === reportIssueItem) {
const minModernTsVersion = this.versionProvider.bundledVersion.apiVersion;
// Don't allow reporting issues using the PnP patched version of TS Server
if (tsVersion.isYarnPnp()) {
const reportIssue: vscode.MessageItem = {
title: vscode.l10n.t("Report issue against Yarn PnP"),
};
const response = await vscode.window.showWarningMessage(
vscode.l10n.t("Please report an issue against Yarn PnP"),
{
modal: true,
detail: vscode.l10n.t("The workspace is using a version of the TypeScript Server that has been patched by Yarn PnP. This patching is a common source of bugs."),
},
reportIssue);
if (response === reportIssue) {
vscode.env.openExternal(vscode.Uri.parse('https://github.com/yarnpkg/berry/issues'));
}
}
// Don't allow reporting issues with old TS versions
else if (
minModernTsVersion &&
tsVersion.lt(minModernTsVersion)
) {
vscode.window.showWarningMessage(
vscode.l10n.t("Please update your TypeScript version"),
{
modal: true,
detail: vscode.l10n.t(
"The workspace is using an old version of TypeScript ({0}).\n\nBefore reporting an issue, please update the workspace to use TypeScript {1} or newer to make sure the bug has not already been fixed.",
tsVersion.displayName,
minModernTsVersion.displayName),
});
} else {
vscode.env.openExternal(vscode.Uri.parse('https://github.com/microsoft/vscode/wiki/TypeScript-Issues'));
}
}
});
if (startService) {
this.startService(true);
}
}
}
public toTsFilePath(resource: vscode.Uri): string | undefined {
if (fileSchemes.disabledSchemes.has(resource.scheme)) {
return undefined;
}
if (resource.scheme === fileSchemes.file && !isWeb()) {
return resource.fsPath;
}
return (this.isProjectWideIntellisenseOnWebEnabled() ? '' : inMemoryResourcePrefix)
+ '/' + resource.scheme
+ '/' + (resource.authority || emptyAuthority)
+ (resource.path.startsWith('/') ? resource.path : '/' + resource.path)
+ (resource.fragment ? '#' + resource.fragment : '');
}
public toOpenTsFilePath(document: vscode.TextDocument | vscode.Uri, options: { suppressAlertOnFailure?: boolean } = {}): string | undefined {
const uri = document instanceof vscode.Uri ? document : document.uri;
if (!this.bufferSyncSupport.ensureHasBuffer(uri)) {
if (!options.suppressAlertOnFailure && !fileSchemes.disabledSchemes.has(uri.scheme)) {
console.error(`Unexpected resource ${uri}`);
}
return undefined;
}
return this.toTsFilePath(uri);
}
public hasCapabilityForResource(resource: vscode.Uri, capability: ClientCapability): boolean {
if (!this.capabilities.has(capability)) {
return false;
}
switch (capability) {
case ClientCapability.Semantic: {
return fileSchemes.getSemanticSupportedSchemes().includes(resource.scheme);
}
case ClientCapability.Syntax:
case ClientCapability.EnhancedSyntax: {
return true;
}
}
}
public toResource(filepath: string): vscode.Uri {
if (isWeb()) {
// On web, the stdlib paths that TS return look like: '/lib.es2015.collection.d.ts'
// TODO: Find out what extensionUri is when testing (should be http://localhost:8080/static/sources/extensions/typescript-language-features/)
// TODO: make sure that this code path is getting hit
if (filepath.startsWith('/lib.') && filepath.endsWith('.d.ts')) {
return vscode.Uri.joinPath(this.context.extensionUri, 'dist', 'browser', 'typescript', filepath.slice(1));
}
const parts = filepath.match(/^\/([^\/]+)\/([^\/]*)\/(.+)$/);
if (parts) {
const resource = vscode.Uri.parse(parts[1] + '://' + (parts[2] === emptyAuthority ? '' : parts[2]) + '/' + parts[3]);
return this.bufferSyncSupport.toVsCodeResource(resource);
}
}
if (filepath.startsWith(inMemoryResourcePrefix)) {
const parts = filepath.match(/^\^\/([^\/]+)\/([^\/]*)\/(.+)$/);
if (parts) {
const resource = vscode.Uri.parse(parts[1] + '://' + (parts[2] === emptyAuthority ? '' : parts[2]) + '/' + parts[3]);
return this.bufferSyncSupport.toVsCodeResource(resource);
}
}
return this.bufferSyncSupport.toResource(filepath);
}
public getWorkspaceRootForResource(resource: vscode.Uri): vscode.Uri | undefined {
const roots = vscode.workspace.workspaceFolders ? Array.from(vscode.workspace.workspaceFolders) : undefined;
if (!roots?.length) {
return undefined;
}
// For notebook cells, we need to use the notebook document to look up the workspace
if (resource.scheme === Schemes.notebookCell) {
for (const notebook of vscode.workspace.notebookDocuments) {
for (const cell of notebook.getCells()) {
if (cell.document.uri.toString() === resource.toString()) {
resource = notebook.uri;
break;
}
}
}
}
// Find the highest level workspace folder that contains the file
for (const root of roots.sort((a, b) => a.uri.path.length - b.uri.path.length)) {
if (root.uri.scheme === resource.scheme && root.uri.authority === resource.authority) {
if (resource.path.startsWith(root.uri.path + '/')) {
return root.uri;
}
}
}
return vscode.workspace.getWorkspaceFolder(resource)?.uri;
}
public execute(command: keyof TypeScriptRequests, args: any, token: vscode.CancellationToken, config?: ExecConfig): Promise<ServerResponse.Response<Proto.Response>> {
let executions: Array<Promise<ServerResponse.Response<Proto.Response>> | undefined> | undefined;
if (config?.cancelOnResourceChange) {
const runningServerState = this.serverState;
if (runningServerState.type === ServerState.Type.Running) {
const source = new vscode.CancellationTokenSource();
token.onCancellationRequested(() => source.cancel());
const inFlight: ToCancelOnResourceChanged = {
resource: config.cancelOnResourceChange,
cancel: () => source.cancel(),
};
runningServerState.toCancelOnResourceChange.add(inFlight);
executions = this.executeImpl(command, args, {
isAsync: false,
token: source.token,
expectsResult: true,
...config,
});
executions[0]!.finally(() => {
runningServerState.toCancelOnResourceChange.delete(inFlight);
source.dispose();
});
}
}
if (!executions) {
executions = this.executeImpl(command, args, {
isAsync: false,
token,
expectsResult: true,
...config,
});
}
if (config?.nonRecoverable) {
executions[0]!.catch(err => this.fatalError(command, err));
}
if (command === 'updateOpen') {
// If update open has completed, consider that the project has loaded
const updateOpenTask = Promise.all(executions).then(() => {
this.loadingIndicator.reset();
});
const updateOpenArgs = (args as Proto.UpdateOpenRequestArgs);
if (updateOpenArgs.openFiles?.length === 1) {
this.loadingIndicator.startedLoadingFile(updateOpenArgs.openFiles[0].file, updateOpenTask);
}
}
return executions[0]!;
}
public executeWithoutWaitingForResponse(command: keyof TypeScriptRequests, args: any): void {
this.executeImpl(command, args, {
isAsync: false,
token: undefined,
expectsResult: false
});
}
public executeAsync(command: keyof TypeScriptRequests, args: Proto.GeterrRequestArgs, token: vscode.CancellationToken): Promise<ServerResponse.Response<Proto.Response>> {
return this.executeImpl(command, args, {
isAsync: true,
token,
expectsResult: true
})[0]!;
}
private executeImpl(command: keyof TypeScriptRequests, args: any, executeInfo: { isAsync: boolean; token?: vscode.CancellationToken; expectsResult: boolean; lowPriority?: boolean; requireSemantic?: boolean }): Array<Promise<ServerResponse.Response<Proto.Response>> | undefined> {
const serverState = this.serverState;
if (serverState.type === ServerState.Type.Running) {
this.bufferSyncSupport.beforeCommand(command);
return serverState.server.executeImpl(command, args, executeInfo);
} else {
return [Promise.resolve(ServerResponse.NoServer)];
}
}
public interruptGetErr<R>(f: () => R): R {
return this.bufferSyncSupport.interruptGetErr(f);
}
private fatalError(command: string, error: unknown): void {
/* __GDPR__
"fatalError" : {
"owner": "mjbvz",
"${include}": [
"${TypeScriptCommonProperties}",
"${TypeScriptRequestErrorProperties}"
],
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
this.telemetryReporter.logTelemetry('fatalError', { ...(error instanceof TypeScriptServerError ? error.telemetry : { command }) });
console.error(`A non-recoverable error occurred while executing tsserver command: ${command}`);
if (error instanceof TypeScriptServerError && error.serverErrorText) {
console.error(error.serverErrorText);
}
if (this.serverState.type === ServerState.Type.Running) {
this.logger.info('Killing TS Server');
const logfile = this.serverState.server.tsServerLog;
this.serverState.server.kill();
if (error instanceof TypeScriptServerError) {
this.serverState = new ServerState.Errored(error, logfile);
}
}
}
private dispatchEvent(event: Proto.Event) {
switch (event.event) {
case EventName.syntaxDiag:
case EventName.semanticDiag:
case EventName.suggestionDiag:
case EventName.regionSemanticDiag: {
// This event also roughly signals that projects have been loaded successfully (since the TS server is synchronous)
this.loadingIndicator.reset();
const diagnosticEvent = event as Proto.DiagnosticEvent;
if (diagnosticEvent.body?.diagnostics) {
this._onDiagnosticsReceived.fire({
kind: getDiagnosticsKind(event),
resource: this.toResource(diagnosticEvent.body.file),
diagnostics: diagnosticEvent.body.diagnostics,
spans: diagnosticEvent.body.spans,
});
}
return;
}
case EventName.configFileDiag:
this._onConfigDiagnosticsReceived.fire(event as Proto.ConfigFileDiagnosticEvent);
return;
case EventName.telemetry: {
const body = (event as Proto.TelemetryEvent).body;
this.dispatchTelemetryEvent(body);
return;
}
case EventName.projectLanguageServiceState: {
const body = (event as Proto.ProjectLanguageServiceStateEvent).body!;
if (this.serverState.type === ServerState.Type.Running) {
this.serverState.updateLanguageServiceEnabled(body.languageServiceEnabled);
}
this._onProjectLanguageServiceStateChanged.fire(body);
return;
}