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

Commit

Permalink
Prompt to set GOPATH (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Nov 1, 2016
1 parent 5df5cd2 commit 190819f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
11 changes: 0 additions & 11 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,13 @@ export function updateGoPathGoRootFromConfig() {
let gopath = vscode.workspace.getConfiguration('go')['gopath'];
if (gopath) {
process.env['GOPATH'] = gopath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath);
hideGoStatus();
}
}

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) {
Expand Down
3 changes: 2 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 21 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -114,7 +114,7 @@ export function getGoVersion(): Promise<SemVersion> {
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);
}

Expand Down Expand Up @@ -161,3 +161,21 @@ export function isVendorSupported(): Promise<boolean> {
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;
}

0 comments on commit 190819f

Please sign in to comment.