Skip to content

Use node ipc for TS Server #46418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ namespace ts.server {
}
return;
}
this.writeMessage(msg);
}

protected writeMessage(msg: protocol.Message) {
const msgText = formatMessage(msg, this.logger, this.byteLength, this.host.newLine);
perfLogger.logEvent(`Response message size: ${msgText.length}`);
this.host.write(msgText);
Expand Down
32 changes: 30 additions & 2 deletions src/tsserver/nodeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ namespace ts.server {

// Override sys.write because fs.writeSync is not reliable on Node 4
sys.write = (s: string) => writeMessage(sys.bufferFrom!(s, "utf8") as globalThis.Buffer);
// REVIEW: for now this implementation uses polling.
// REVIEW: for now this implementation uses polling.
// The advantage of polling is that it works reliably
// on all os and with network mounted files.
// For 90 referenced files, the average time to detect
Expand Down Expand Up @@ -759,12 +759,40 @@ namespace ts.server {
}
}

class IpcIOSession extends IOSession {

protected writeMessage(msg: protocol.Message): void {
const verboseLogging = logger.hasLevel(LogLevel.verbose);
if (verboseLogging) {
const json = JSON.stringify(msg);
logger.info(`${msg.type}:${indent(json)}`);
}

process.send!(msg);
}

protected parseMessage(message: any): protocol.Request {
return message as protocol.Request;
}

protected toStringMessage(message: any) {
return JSON.stringify(message, undefined, 2);
}

public listen() {
process.on("message", (e: any) => {
this.onMessage(e);
});
}
}

const eventPort: number | undefined = parseEventPort(findArgument("--eventPort"));
const typingSafeListLocation = findArgument(Arguments.TypingSafeListLocation)!; // TODO: GH#18217
const typesMapLocation = findArgument(Arguments.TypesMapLocation) || combinePaths(getDirectoryPath(sys.getExecutingFilePath()), "typesMap.json");
const npmLocation = findArgument(Arguments.NpmLocation);
const validateDefaultNpmLocation = hasArgument(Arguments.ValidateDefaultNpmLocation);
const disableAutomaticTypingAcquisition = hasArgument("--disableAutomaticTypingAcquisition");
const useNodeIpc = hasArgument("--useNodeIpc");
const telemetryEnabled = hasArgument(Arguments.EnableTelemetry);
const commandLineTraceDir = findArgument("--traceDirectory");
const traceDir = commandLineTraceDir
Expand All @@ -774,7 +802,7 @@ namespace ts.server {
startTracing("server", traceDir);
}

const ioSession = new IOSession();
const ioSession = useNodeIpc ? new IpcIOSession() : new IOSession();
process.on("uncaughtException", err => {
ioSession.logError(err, "unknown");
});
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10470,6 +10470,7 @@ declare namespace ts.server {
logError(err: Error, cmd: string): void;
private logErrorWorker;
send(msg: protocol.Message): void;
protected writeMessage(msg: protocol.Message): void;
event<T extends object>(body: T, eventName: string): void;
/** @deprecated */
output(info: any, cmdName: string, reqSeq?: number, errorMsg?: string): void;
Expand Down