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

Use default gopath supported in Go 1.8 #820

Merged
merged 4 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go:
- 1.5
- 1.6
- 1.7
- 1.8

sudo: false

Expand Down
28 changes: 23 additions & 5 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
}

// If the go.toolsGopath is set, use
// its value as the GOPATH for the "go get" child process.
// its value as the GOPATH for the "go get" child process.
let toolsGopath = getToolsGopath();
let envWithSeparateGoPathForTools = null;
if (toolsGopath) {
envWithSeparateGoPathForTools = Object.assign({}, envForTools, {GOPATH: toolsGopath});
envWithSeparateGoPathForTools = Object.assign({}, envForTools, { GOPATH: toolsGopath });
}

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

export function updateGoPathGoRootFromConfig() {
export function updateGoPathGoRootFromConfig(): Promise<void> {
let goroot = vscode.workspace.getConfiguration('go')['goroot'];
if (goroot) {
process.env['GOROOT'] = goroot;
Expand All @@ -220,10 +220,28 @@ export function updateGoPathGoRootFromConfig() {
process.env['GOPATH'] = vscode.workspace.rootPath.substr(0, dirs.slice(0, srcIdx).join(path.sep).length);
}
}

if (process.env['GOPATH']) {
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`
let goRuntimePath = getGoRuntimePath();
return new Promise<void>((resolve, reject) => {
cp.execFile(goRuntimePath, ['env'], (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);
}
return resolve();
});
});
}

export function setupGoPathAndOfferToInstallTools() {
updateGoPathGoRootFromConfig();
export function offerToInstallTools() {
isVendorSupported();

getGoVersion().then(goVersion => {
Expand Down
16 changes: 9 additions & 7 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { GoSignatureHelpProvider } from './goSignature';
import { GoWorkspaceSymbolProvider } from './goSymbol';
import { GoCodeActionProvider } from './goCodeAction';
import { check, ICheckResult, removeTestStatus } from './goCheck';
import { updateGoPathGoRootFromConfig, setupGoPathAndOfferToInstallTools } from './goInstallTools';
import { updateGoPathGoRootFromConfig, offerToInstallTools } from './goInstallTools';
import { GO_MODE } from './goMode';
import { showHideStatus } from './goStatus';
import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover';
Expand Down Expand Up @@ -78,7 +78,14 @@ export function activate(ctx: vscode.ExtensionContext): void {
vscode.window.onDidChangeActiveTextEditor(showHideStatus, null, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(getCodeCoverage, null, ctx.subscriptions);

setupGoPathAndOfferToInstallTools();
updateGoPathGoRootFromConfig().then(() => {
if (vscode.window.activeTextEditor && isGoPathSet()) {
let goConfig = vscode.workspace.getConfiguration('go');
runBuilds(vscode.window.activeTextEditor.document, goConfig);
}
});

offerToInstallTools();
startBuildOnSaveWatcher(ctx.subscriptions);

ctx.subscriptions.push(vscode.commands.registerCommand('go.gopath', () => {
Expand Down Expand Up @@ -190,11 +197,6 @@ export function activate(ctx: vscode.ExtensionContext): void {
},
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
});

if (vscode.window.activeTextEditor && isGoPathSet()) {
let goConfig = vscode.workspace.getConfiguration('go');
runBuilds(vscode.window.activeTextEditor.document, goConfig);
}
}

function deactivate() {
Expand Down