From 90d2e33e0e53832015fd01afacb1941e717162a3 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 3 Aug 2017 17:26:32 -0700 Subject: [PATCH] Fix some possible TSServer spawn issues **Bug** A few users are seeing issues spaning the tsserver. The most common error seems to be ERRNO when spawning it **Fix** - Ensure we always try to set `env.PATH` - Use `require.resolve` to check the existance of the `electronForkStart` helper before calling spawn --- .../typescript/src/typescriptServiceClient.ts | 4 --- extensions/typescript/src/utils/electron.ts | 30 +++++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/extensions/typescript/src/typescriptServiceClient.ts b/extensions/typescript/src/typescriptServiceClient.ts index 61df7b0ca4333..bf68bc742f9e9 100644 --- a/extensions/typescript/src/typescriptServiceClient.ts +++ b/extensions/typescript/src/typescriptServiceClient.ts @@ -599,10 +599,6 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient } private get mainWorkspaceRootPath(): string | undefined { - if (workspace.rootPath) { - return workspace.rootPath; - } - if (workspace.workspaceFolders && workspace.workspaceFolders.length) { return workspace.workspaceFolders[0].uri.fsPath; } diff --git a/extensions/typescript/src/utils/electron.ts b/extensions/typescript/src/utils/electron.ts index 46ad56c464abb..10961a57b939e 100644 --- a/extensions/typescript/src/utils/electron.ts +++ b/extensions/typescript/src/utils/electron.ts @@ -18,7 +18,7 @@ export function makeRandomHexString(length: number): string { let chars = ['0', '1', '2', '3', '4', '5', '6', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; let result = ''; for (let i = 0; i < length; i++) { - let idx = Math.floor(chars.length * Math.random()); + const idx = Math.floor(chars.length * Math.random()); result += chars[idx]; } return result; @@ -27,6 +27,7 @@ export function makeRandomHexString(length: number): string { function generatePipeName(): string { return getPipeName(makeRandomHexString(40)); } + function getPipeName(name: string): string { const fullName = 'vscode-' + name; if (process.platform === 'win32') { @@ -43,19 +44,22 @@ export function getTempFile(name: string): string { } -function generatePatchedEnv(env: any, stdInPipeName: string, stdOutPipeName: string, stdErrPipeName: string): any { - // Set the two unique pipe names and the electron flag as process env - - var newEnv: any = {}; - for (var key in env) { - newEnv[key] = env[key]; - } +function generatePatchedEnv( + env: any, + stdInPipeName: string, + stdOutPipeName: string, + stdErrPipeName: string +): any { + const newEnv = Object.assign({}, env); + // Set the two unique pipe names and the electron flag as process env newEnv['STDIN_PIPE_NAME'] = stdInPipeName; newEnv['STDOUT_PIPE_NAME'] = stdOutPipeName; newEnv['STDERR_PIPE_NAME'] = stdErrPipeName; newEnv['ELECTRON_RUN_AS_NODE'] = '1'; + // Ensure we always have a PATH set + newEnv['PATH'] = newEnv['PATH'] || process.env.PATH; return newEnv; } @@ -67,7 +71,7 @@ export function fork( callback: (error: any, cp: cp.ChildProcess | null) => void, ): void { - var callbackCalled = false; + let callbackCalled = false; const resolve = (result: cp.ChildProcess) => { if (callbackCalled) { return; @@ -90,7 +94,7 @@ export function fork( const newEnv = generatePatchedEnv(process.env, stdInPipeName, stdOutPipeName, stdErrPipeName); - var childProcess: cp.ChildProcess; + let childProcess: cp.ChildProcess; // Begin listening to stderr pipe let stdErrServer = net.createServer((stdErrStream) => { @@ -115,7 +119,7 @@ export function fork( }); stdOutServer.listen(stdOutPipeName); - var serverClosed = false; + let serverClosed = false; const closeServer = () => { if (serverClosed) { return; @@ -128,8 +132,8 @@ export function fork( // Create the process logger.info('Forking TSServer', `PATH: ${newEnv['PATH']}`); - const bootstrapperPath = path.join(__dirname, 'electronForkStart'); - childProcess = cp.fork(bootstrapperPath, [modulePath].concat(args), { + const bootstrapperPath = require.resolve('./electronForkStart'); + childProcess = cp.fork(bootstrapperPath, [modulePath].concat(args), { silent: true, cwd: options.cwd, env: newEnv,