From d93a0aec2a1a57e820cff3a9511cedbc7f13b61c Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Fri, 17 May 2019 11:46:06 -0700 Subject: [PATCH] Ensure Go binary is in the PATH when running Go tools --- src/goInstallTools.ts | 18 ++++++++++++++++++ src/util.ts | 12 ------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index c10beb673..59dbbb42c 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -422,6 +422,24 @@ export function updateGoPathGoRootFromConfig(): Promise { if (!goRuntimePath) { return Promise.reject(new Error('Cannot find "go" binary. Update PATH or GOROOT appropriately')); } + const goRuntimeBasePath = path.basename(goRuntimePath); + + // cgo and a few other Go tools expect Go binary to be in the path + let pathEnvVar: string; + if (process.env.hasOwnProperty('PATH')) { + pathEnvVar = 'PATH'; + } else if (process.platform === 'win32' && process.env.hasOwnProperty('Path')) { + pathEnvVar = 'Path'; + } + if (goRuntimeBasePath + && pathEnvVar + && process.env[pathEnvVar] + && (process.env[pathEnvVar]).split(path.delimiter).indexOf(goRuntimeBasePath) === -1 + ) { + process.env[pathEnvVar] += path.delimiter + goRuntimeBasePath; + } + + return new Promise((resolve, reject) => { cp.execFile(goRuntimePath, ['env', 'GOPATH', 'GOROOT'], (err, stdout, stderr) => { if (err) { diff --git a/src/util.ts b/src/util.ts index e3a83d9cb..7903df0d0 100644 --- a/src/util.ts +++ b/src/util.ts @@ -390,18 +390,6 @@ export function getToolsEnvVars(): any { Object.keys(toolsEnvVars).forEach(key => envVars[key] = typeof toolsEnvVars[key] === 'string' ? resolvePath(toolsEnvVars[key]) : toolsEnvVars[key]); } - // cgo expects go to be in the path - const goroot: string = envVars['GOROOT']; - let pathEnvVar: string; - if (envVars.hasOwnProperty('PATH')) { - pathEnvVar = 'PATH'; - } else if (process.platform === 'win32' && envVars.hasOwnProperty('Path')) { - pathEnvVar = 'Path'; - } - if (goroot && pathEnvVar && envVars[pathEnvVar] && (envVars[pathEnvVar]).split(path.delimiter).indexOf(goroot) === -1) { - envVars[pathEnvVar] += path.delimiter + path.join(goroot, 'bin'); - } - return envVars; }