Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Ensure Go binary is in the PATH when running Go tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed May 17, 2019
1 parent 9f5f1a9 commit d93a0ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ export function updateGoPathGoRootFromConfig(): Promise<void> {
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]
&& (<string>process.env[pathEnvVar]).split(path.delimiter).indexOf(goRuntimeBasePath) === -1
) {
process.env[pathEnvVar] += path.delimiter + goRuntimeBasePath;
}


return new Promise<void>((resolve, reject) => {
cp.execFile(goRuntimePath, ['env', 'GOPATH', 'GOROOT'], (err, stdout, stderr) => {
if (err) {
Expand Down
12 changes: 0 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] && (<string>envVars[pathEnvVar]).split(path.delimiter).indexOf(goroot) === -1) {
envVars[pathEnvVar] += path.delimiter + path.join(goroot, 'bin');
}

return envVars;
}

Expand Down

0 comments on commit d93a0ae

Please sign in to comment.