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

Read GOPATH, GOROOT from go env #876

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,23 @@ export function updateGoPathGoRootFromConfig(): Promise<void> {
}
}

if (process.env['GOPATH']) {
if (process.env['GOPATH'] && process.env['GOROOT']) {
return Promise.resolve();
}

// From Go 1.8 onwards, when there is no GOPATH set, there is default GOPATH used which can be got from running `go env`
// If GOPATH and GOROOT are still not set, then use the ones from `go env`
let goRuntimePath = getGoRuntimePath();
return new Promise<void>((resolve, reject) => {
cp.execFile(goRuntimePath, ['env'], (err, stdout, stderr) => {
cp.execFile(goRuntimePath, ['env', 'GOROOT', 'GOPATH'], (err, stdout, stderr) => {
if (err) {
return reject();
}
let gopathOutput = stdout.split('\n').find((value, index) => { return value.startsWith('GOPATH="') && value.trim().endsWith('"'); });
if (gopathOutput) {
process.env['GOPATH'] = gopathOutput.trim().substring('GOPATH="'.length, gopathOutput.length - 1);
let envOutput = stdout.split('\n');
if (!process.env['GOROOT'] && envOutput[0].trim()) {
process.env['GOROOT'] = envOutput[0].trim();
}
if (!process.env['GOPATH'] && envOutput[1].trim()) {
process.env['GOPATH'] = envOutput[1].trim();
}
return resolve();
});
Expand Down