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

Commit e51a915

Browse files
authored
Use default gopath supported in Go 1.8 (#820)
* Use default gopath supported in Go 1.8 * Handle error when go env fails * Take care of \r while splitting stdout
1 parent 0605428 commit e51a915

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go:
44
- 1.5
55
- 1.6
66
- 1.7
7+
- 1.8
78

89
sudo: false
910

src/goInstallTools.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
141141
}
142142

143143
// If the go.toolsGopath is set, use
144-
// its value as the GOPATH for the "go get" child process.
144+
// its value as the GOPATH for the "go get" child process.
145145
let toolsGopath = getToolsGopath();
146146
let envWithSeparateGoPathForTools = null;
147147
if (toolsGopath) {
148-
envWithSeparateGoPathForTools = Object.assign({}, envForTools, {GOPATH: toolsGopath});
148+
envWithSeparateGoPathForTools = Object.assign({}, envForTools, { GOPATH: toolsGopath });
149149
}
150150

151151
missing.reduce((res: Promise<string[]>, tool: string) => {
@@ -199,7 +199,7 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
199199
});
200200
}
201201

202-
export function updateGoPathGoRootFromConfig() {
202+
export function updateGoPathGoRootFromConfig(): Promise<void> {
203203
let goroot = vscode.workspace.getConfiguration('go')['goroot'];
204204
if (goroot) {
205205
process.env['GOROOT'] = goroot;
@@ -220,10 +220,28 @@ export function updateGoPathGoRootFromConfig() {
220220
process.env['GOPATH'] = vscode.workspace.rootPath.substr(0, dirs.slice(0, srcIdx).join(path.sep).length);
221221
}
222222
}
223+
224+
if (process.env['GOPATH']) {
225+
return Promise.resolve();
226+
}
227+
228+
// From Go 1.8 onwards, when there is no GOPATH set, there is default GOPATH used which can be got from running `go env`
229+
let goRuntimePath = getGoRuntimePath();
230+
return new Promise<void>((resolve, reject) => {
231+
cp.execFile(goRuntimePath, ['env'], (err, stdout, stderr) => {
232+
if (err) {
233+
return reject();
234+
}
235+
let gopathOutput = stdout.split('\n').find((value, index) => { return value.startsWith('GOPATH="') && value.trim().endsWith('"'); });
236+
if (gopathOutput) {
237+
process.env['GOPATH'] = gopathOutput.trim().substring('GOPATH="'.length, gopathOutput.length - 1);
238+
}
239+
return resolve();
240+
});
241+
});
223242
}
224243

225-
export function setupGoPathAndOfferToInstallTools() {
226-
updateGoPathGoRootFromConfig();
244+
export function offerToInstallTools() {
227245
isVendorSupported();
228246

229247
getGoVersion().then(goVersion => {

src/goMain.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { GoSignatureHelpProvider } from './goSignature';
2020
import { GoWorkspaceSymbolProvider } from './goSymbol';
2121
import { GoCodeActionProvider } from './goCodeAction';
2222
import { check, ICheckResult, removeTestStatus } from './goCheck';
23-
import { updateGoPathGoRootFromConfig, setupGoPathAndOfferToInstallTools } from './goInstallTools';
23+
import { updateGoPathGoRootFromConfig, offerToInstallTools } from './goInstallTools';
2424
import { GO_MODE } from './goMode';
2525
import { showHideStatus } from './goStatus';
2626
import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover';
@@ -78,7 +78,14 @@ export function activate(ctx: vscode.ExtensionContext): void {
7878
vscode.window.onDidChangeActiveTextEditor(showHideStatus, null, ctx.subscriptions);
7979
vscode.window.onDidChangeActiveTextEditor(getCodeCoverage, null, ctx.subscriptions);
8080

81-
setupGoPathAndOfferToInstallTools();
81+
updateGoPathGoRootFromConfig().then(() => {
82+
if (vscode.window.activeTextEditor && isGoPathSet()) {
83+
let goConfig = vscode.workspace.getConfiguration('go');
84+
runBuilds(vscode.window.activeTextEditor.document, goConfig);
85+
}
86+
});
87+
88+
offerToInstallTools();
8289
startBuildOnSaveWatcher(ctx.subscriptions);
8390

8491
ctx.subscriptions.push(vscode.commands.registerCommand('go.gopath', () => {
@@ -190,11 +197,6 @@ export function activate(ctx: vscode.ExtensionContext): void {
190197
},
191198
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
192199
});
193-
194-
if (vscode.window.activeTextEditor && isGoPathSet()) {
195-
let goConfig = vscode.workspace.getConfiguration('go');
196-
runBuilds(vscode.window.activeTextEditor.document, goConfig);
197-
}
198200
}
199201

200202
function deactivate() {

0 commit comments

Comments
 (0)