From 190819f162aef9977e029c80e185ed29a50d1dc1 Mon Sep 17 00:00:00 2001 From: Ramya Rao Date: Tue, 1 Nov 2016 13:45:53 -0700 Subject: [PATCH] Prompt to set GOPATH (#591) --- src/goInstallTools.ts | 11 ----------- src/goMain.ts | 3 ++- src/util.ts | 24 +++++++++++++++++++++--- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index 25e1cb3d0..cbf44ecde 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -147,7 +147,6 @@ export function updateGoPathGoRootFromConfig() { let gopath = vscode.workspace.getConfiguration('go')['gopath']; if (gopath) { process.env['GOPATH'] = gopath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath); - hideGoStatus(); } } @@ -155,16 +154,6 @@ export function setupGoPathAndOfferToInstallTools() { updateGoPathGoRootFromConfig(); isVendorSupported(); - if (!process.env['GOPATH']) { - let info = 'GOPATH is not set as an environment variable or via `go.gopath` setting in Code'; - showGoStatus('GOPATH not set', 'go.gopathinfo', info); - vscode.commands.registerCommand('go.gopathinfo', () => { - vscode.window.showInformationMessage(info); - hideGoStatus(); - }); - return; - } - getGoVersion().then(goVersion => { getMissingTools(goVersion).then(missing => { if (missing.length > 0) { diff --git a/src/goMain.ts b/src/goMain.ts index 192931ef6..48f70df0d 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -28,6 +28,7 @@ import { testAtCursor, testCurrentPackage, testCurrentFile, testPrevious } from import { generateTestCurrentPackage, generateTestCurrentFile, generateTestCurrentFunction } from './goGenerateTests'; import { addImport } from './goImport'; import { installAllTools } from './goInstallTools'; +import { isGoPathSet } from './util'; let diagnosticCollection: vscode.DiagnosticCollection; let goFormatOnSaveDeprecated = true; @@ -116,7 +117,7 @@ export function activate(ctx: vscode.ExtensionContext): void { wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g, }); - if (vscode.window.activeTextEditor) { + if (vscode.window.activeTextEditor && isGoPathSet()) { let goConfig = vscode.workspace.getConfiguration('go'); runBuilds(vscode.window.activeTextEditor.document, goConfig); } diff --git a/src/util.ts b/src/util.ts index 2c19eebd1..086e375da 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------*/ -import { TextDocument, Position, window } from 'vscode'; +import vscode = require('vscode'); import path = require('path'); import { getGoRuntimePath } from './goPath'; import cp = require('child_process'); @@ -16,7 +16,7 @@ export interface SemVersion { let goVersion: SemVersion = null; let vendorSupport: boolean = null; -export function byteOffsetAt(document: TextDocument, position: Position): number { +export function byteOffsetAt(document: vscode.TextDocument, position: vscode.Position): number { let offset = document.offsetAt(position); let text = document.getText(); let byteOffset = 0; @@ -114,7 +114,7 @@ export function getGoVersion(): Promise { let goRuntimePath = getGoRuntimePath(); if (!goRuntimePath) { - window.showInformationMessage('Cannot find "go" binary. Update PATH or GOROOT appropriately'); + vscode.window.showInformationMessage('Cannot find "go" binary. Update PATH or GOROOT appropriately'); return Promise.resolve(null); } @@ -161,3 +161,21 @@ export function isVendorSupported(): Promise { return vendorSupport; }); } + +/** + * Returns boolean indicating if GOPATH is set or not + * If not set, then prompts user to do set GOPATH + */ +export function isGoPathSet(): boolean { + if (!process.env['GOPATH']) { + vscode.window.showInformationMessage('Set GOPATH environment variable and restart VS Code or set GOPATH in Workspace settings', 'Set GOPATH in Workspace Settings').then(selected => { + if (selected === 'Set GOPATH in Workspace Settings') { + let settingsFilePath = path.join(vscode.workspace.rootPath, '.vscode', 'settings.json'); + vscode.commands.executeCommand('vscode.open', vscode.Uri.file(settingsFilePath)); + } + }); + return false; + } + + return true; +} \ No newline at end of file