Skip to content

Commit

Permalink
Better encapsulate logic of spawning different server kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Jun 21, 2019
1 parent 5fc7a8c commit ffecce0
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions extensions/typescript-language-features/src/tsServer/spanwer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Tracer from '../utils/tracer';
import { TypeScriptVersion, TypeScriptVersionProvider } from '../utils/versionProvider';
import { ITypeScriptServer, PipeRequestCanceller, ProcessBasedTsServer, SyntaxRoutingTsServer, TsServerProcess } from './server';

type ServerKind = 'main' | 'syntax' | 'semantic';

export class TypeScriptServerSpawner {
public constructor(
private readonly _versionProvider: TypeScriptVersionProvider,
Expand All @@ -36,12 +38,12 @@ export class TypeScriptServerSpawner {
pluginManager: PluginManager
): ITypeScriptServer {
if (this.shouldUseSeparateSyntaxServer(version)) {
const syntaxServer = this.spawnTsServer('syntax', version, configuration, pluginManager, ['--syntaxOnly', '--disableAutomaticTypingAcquisition']);
const semanticServer = this.spawnTsServer('semantic', version, configuration, pluginManager, []);
const syntaxServer = this.spawnTsServer('syntax', version, configuration, pluginManager);
const semanticServer = this.spawnTsServer('semantic', version, configuration, pluginManager);
return new SyntaxRoutingTsServer(syntaxServer, semanticServer);
}

return this.spawnTsServer('main', version, configuration, pluginManager, []);
return this.spawnTsServer('main', version, configuration, pluginManager);
}

private shouldUseSeparateSyntaxServer(version: TypeScriptVersion): boolean {
Expand All @@ -53,47 +55,47 @@ export class TypeScriptServerSpawner {
}

private spawnTsServer(
serverId: string,
kind: ServerKind,
version: TypeScriptVersion,
configuration: TypeScriptServiceConfiguration,
pluginManager: PluginManager,
extraForkArgs: readonly string[],
): ITypeScriptServer {
const apiVersion = version.apiVersion || API.defaultVersion;

const { args, cancellationPipeName, tsServerLogFile } = this.getTsServerArgs(configuration, version, apiVersion, pluginManager);
const { args, cancellationPipeName, tsServerLogFile } = this.getTsServerArgs(kind, configuration, version, apiVersion, pluginManager);

if (TypeScriptServerSpawner.isLoggingEnabled(apiVersion, configuration)) {
if (tsServerLogFile) {
this._logger.info(`<${serverId}> Log file: ${tsServerLogFile}`);
this._logger.info(`<${kind}> Log file: ${tsServerLogFile}`);
} else {
this._logger.error(`<${serverId}> Could not create log directory`);
this._logger.error(`<${kind}> Could not create log directory`);
}
}

this._logger.info(`<${serverId}> Forking...`);
const childProcess = electron.fork(version.tsServerPath, [...args, ...extraForkArgs], this.getForkOptions());
this._logger.info(`<${serverId}> Starting...`);
this._logger.info(`<${kind}> Forking...`);
const childProcess = electron.fork(version.tsServerPath, args, this.getForkOptions(kind));
this._logger.info(`<${kind}> Starting...`);

return new ProcessBasedTsServer(
serverId,
kind,
new ChildServerProcess(childProcess),
tsServerLogFile,
new PipeRequestCanceller(serverId, cancellationPipeName, this._tracer),
new PipeRequestCanceller(kind, cancellationPipeName, this._tracer),
version,
this._telemetryReporter,
this._tracer);
}

private getForkOptions() {
const debugPort = TypeScriptServerSpawner.getDebugPort();
private getForkOptions(kind: ServerKind) {
const debugPort = TypeScriptServerSpawner.getDebugPort(kind);
const tsServerForkOptions: electron.ForkOptions = {
execArgv: debugPort ? [`--inspect=${debugPort}`] : [],
};
return tsServerForkOptions;
}

private getTsServerArgs(
kind: ServerKind,
configuration: TypeScriptServiceConfiguration,
currentVersion: TypeScriptVersion,
apiVersion: API,
Expand All @@ -103,19 +105,23 @@ export class TypeScriptServerSpawner {
let cancellationPipeName: string | undefined;
let tsServerLogFile: string | undefined;

if (kind === 'syntax') {
args.push('--syntaxOnly');
}

if (apiVersion.gte(API.v206)) {
if (apiVersion.gte(API.v250)) {
args.push('--useInferredProjectPerProjectRoot');
} else {
args.push('--useSingleInferredProject');
}

if (configuration.disableAutomaticTypeAcquisition) {
if (configuration.disableAutomaticTypeAcquisition || kind === 'syntax') {
args.push('--disableAutomaticTypingAcquisition');
}
}

if (apiVersion.gte(API.v208)) {
if (apiVersion.gte(API.v208) && kind !== 'syntax') {
args.push('--enableTelemetry');
}

Expand Down Expand Up @@ -173,7 +179,11 @@ export class TypeScriptServerSpawner {
return { args, cancellationPipeName, tsServerLogFile };
}

private static getDebugPort(): number | undefined {
private static getDebugPort(kind: ServerKind): number | undefined {
if (kind === 'syntax') {
// We typically only want to debug the main semantic server
return undefined;
}
const value = process.env['TSS_DEBUG'];
if (value) {
const port = parseInt(value);
Expand Down

0 comments on commit ffecce0

Please sign in to comment.