From 0866b107ced6be1b3ee39337b4cb31f701ed49df Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 18 Jan 2017 21:00:25 -0800 Subject: [PATCH] Don't send empty environment vars when not Windows PowerShell dev build This change fixes an issue where $env:PROCESSOR_ARCHITECTURE is not being set properly in the language server's powershell.exe process which caused an exception when gathering that variable. The problem was caused by the environment variable setting behavior when the Windows PowerShell developer build setting is set to false. In this case, an empty environment variable object was being passed to the process, effectively clearing its environment variables. This is one half of the fix to PowerShell/PowerShellEditorServices#346. --- src/session.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/session.ts b/src/session.ts index 64c3d6e259..179afd0d20 100644 --- a/src/session.ts +++ b/src/session.ts @@ -254,12 +254,23 @@ export class SessionManager { "-Command", "& '" + startScriptPath + "' " + startArgs); + // Set DEVPATH environment variable if necessary + if (isWindowsDevBuild) { + // The development build looks for this environment variable to + // know where to find its assemblies + process.env.DEVPATH = path.dirname(powerShellExePath); + } + else { + // It's safe to delete this variable even if it doesn't exist + delete process.env.DEVPATH; + } + // Launch PowerShell as child process this.powerShellProcess = cp.spawn( powerShellExePath, powerShellArgs, - { env: isWindowsDevBuild ? { "DEVPATH": path.dirname(powerShellExePath) } : {} }); + { env: process.env }); var decoder = new StringDecoder('utf8'); this.powerShellProcess.stdout.on( @@ -545,15 +556,17 @@ export class SessionManager { } private resolvePowerShellPath(powerShellExePath: string): string { + var resolvedPath = path.resolve(__dirname, powerShellExePath); + // If the path does not exist, show an error - if (!utils.checkIfFileExists(powerShellExePath)) { + if (!utils.checkIfFileExists(resolvedPath)) { this.setSessionFailure( - "powershell.exe cannot be found or is not accessible at path " + powerShellExePath); + "powershell.exe cannot be found or is not accessible at path " + resolvedPath); return null; } - return powerShellExePath; + return resolvedPath; } private showSessionMenu() {