Add tracing support to tsserver (#41374)

* Add tracing support to tsserver

Read the `TSS_TRACE` environment variable to determine which directory
trace files should be written to.

Notable changes from tsc tracing:
1) Drop all tracepoints that depend on type IDs
2) Write output to trace.PID.json
3) New, server-specific events (request/response, cancellation, etc)

* Drop try-finally blocks that aren't strictly necessary

* Fix lint error

* Trace background work (for diagnostics)

* Move try-finally blocks into session so tsc doesn't use them

* Add missing try-finally

* Use consistent capitalization

* Inline canPop call where underlying variable is available

* Clarify comments

* Include PID in build-mode file names

* Introduce more efficient popAll function

* Trace throwIfCancellationRequested rather than isCancellationRequested

* Remove unnecessary try-finally blocks

* Add a command-line argument for consistency with logging

* Fix rebase issues

* Address PR feedback

* Rename completionEvents to eventStack

* Drop assertStackEmpty as hard-to-maintain and marginally valuable

* Rename stepCancellation to stepCanceledEarly

* Rename stepEarlyCancellation to stepCanceled and use flag instead

* Check correct variable on exit
This commit is contained in:
Andrew Casey
2020-11-16 09:26:28 -08:00
committed by GitHub
parent 4885dec80d
commit 79ffd03f8b
7 changed files with 99 additions and 28 deletions

View File

@@ -1126,16 +1126,22 @@ namespace ts {
return createLanguageServiceSourceFile(sourceFile.fileName, scriptSnapshot, sourceFile.languageVersion, version, /*setNodeParents*/ true, sourceFile.scriptKind);
}
const NoopCancellationToken: CancellationToken = {
isCancellationRequested: returnFalse,
throwIfCancellationRequested: noop,
};
class CancellationTokenObject implements CancellationToken {
constructor(private cancellationToken: HostCancellationToken | undefined) {
constructor(private cancellationToken: HostCancellationToken) {
}
public isCancellationRequested(): boolean {
return !!this.cancellationToken && this.cancellationToken.isCancellationRequested();
return this.cancellationToken.isCancellationRequested();
}
public throwIfCancellationRequested(): void {
if (this.isCancellationRequested()) {
tracing.instant(tracing.Phase.Session, "cancellationThrown", { kind: "CancellationTokenObject" });
throw new OperationCanceledException();
}
}
@@ -1166,6 +1172,7 @@ namespace ts {
public throwIfCancellationRequested(): void {
if (this.isCancellationRequested()) {
tracing.instant(tracing.Phase.Session, "cancellationThrown", { kind: "ThrottledCancellationToken" });
throw new OperationCanceledException();
}
}
@@ -1233,7 +1240,9 @@ namespace ts {
let lastProjectVersion: string;
let lastTypesRootVersion = 0;
const cancellationToken = new CancellationTokenObject(host.getCancellationToken && host.getCancellationToken());
const cancellationToken = host.getCancellationToken
? new CancellationTokenObject(host.getCancellationToken())
: NoopCancellationToken;
const currentDirectory = host.getCurrentDirectory();
// Check if the localized messages json is set, otherwise query the host for it