From 94960c70aef4ec3aeceba78f1201c79acc245fdb Mon Sep 17 00:00:00 2001 From: Vincent Chinedu Okonkwo Date: Fri, 3 Feb 2017 23:27:44 +0000 Subject: [PATCH] added go.inferGopath. Similar to GoSublime's use_gs_gopath (#762) * added go.inferGopath. Similar to GoSublime's use_gs_gopath. see #757 * fix lint problems --- README.md | 1 + package.json | 5 +++++ src/goInstallTools.ts | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 25e0466f3..8c0a27327 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ The following Visual Studio Code settings are available for the Go extension. T "go.formatFlags": [], "go.goroot": "/usr/local/go", "go.gopath": "/Users/lukeh/go", + "go.inferGopath": false, "go.gocodeAutoBuild": false } ``` diff --git a/package.json b/package.json index 87320f1be..11670210a 100644 --- a/package.json +++ b/package.json @@ -337,6 +337,11 @@ "default": false, "description": "Complete functions with their parameter signature" }, + "go.inferGopath": { + "type": "boolean", + "default": false, + "description": "use workspace root to find appropriate GOPATH" + }, "go.gopath": { "type": [ "string", diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index 5ff6489be..2f0d208cb 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -209,6 +209,17 @@ export function updateGoPathGoRootFromConfig() { if (gopath) { process.env['GOPATH'] = gopath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath); } + + let inferGoPath = vscode.workspace.getConfiguration('go')['inferGopath']; + if (inferGoPath) { + let dirs = vscode.workspace.rootPath.toLowerCase().split(path.sep); + // find src directory closest to workspace root + let srcIdx = dirs.lastIndexOf('src'); + + if (srcIdx > 0) { + process.env['GOPATH'] = vscode.workspace.rootPath.substr(0, dirs.slice(0, srcIdx).join(path.sep).length); + } + } } export function setupGoPathAndOfferToInstallTools() {