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

Commit

Permalink
Use toolsEnvVars while running Go tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed May 1, 2017
1 parent 506cc99 commit bca4dd5
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/goCheck.ts
Expand Up @@ -15,7 +15,7 @@ import { getCoverage } from './goCover';
import { outputChannel } from './goStatus';
import { promptForMissingTool } from './goInstallTools';
import { goTest } from './goTest';
import { getBinPath, parseFilePrelude, getCurrentGoWorkspaceFromGOPATH } from './util';
import { getBinPath, parseFilePrelude, getCurrentGoWorkspaceFromGOPATH, getToolsEnvVars } from './util';

let statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.command = 'go.test.showOutput';
Expand Down Expand Up @@ -110,7 +110,7 @@ export function check(filename: string, goConfig: vscode.WorkspaceConfiguration)
outputChannel.clear();
let runningToolsPromises = [];
let cwd = path.dirname(filename);
let env = Object.assign({}, process.env, goConfig['toolsEnvVars']);
let env = getToolsEnvVars();
let goRuntimePath = getGoRuntimePath();

if (!goRuntimePath) {
Expand Down
17 changes: 9 additions & 8 deletions src/goDeclaration.ts
Expand Up @@ -10,7 +10,7 @@ import cp = require('child_process');
import path = require('path');
import { byteOffsetAt, getBinPath } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getGoVersion, SemVersion, goKeywords, isPositionInString } from './util';
import { getGoVersion, SemVersion, goKeywords, isPositionInString, getToolsEnvVars } from './util';

export interface GoDefinitionInformation {
file: string;
Expand All @@ -34,22 +34,23 @@ export function definitionLocation(document: vscode.TextDocument, position: vsco
}
let toolForDocs = goConfig['docsTool'] || 'godoc';
let offset = byteOffsetAt(document, position);
let env = getToolsEnvVars();
return getGoVersion().then((ver: SemVersion) => {
// If no Go version can be parsed, it means it's a non-tagged one.
// Assume it's > Go 1.5
if (toolForDocs === 'godoc' || (ver && (ver.major < 1 || (ver.major === 1 && ver.minor < 6)))) {
return definitionLocation_godef(document, position, offset, includeDocs);
return definitionLocation_godef(document, position, offset, includeDocs, env);
}
return definitionLocation_gogetdoc(document, position, offset);
return definitionLocation_gogetdoc(document, position, offset, env);
});
}

function definitionLocation_godef(document: vscode.TextDocument, position: vscode.Position, offset: number, includeDocs = true): Promise<GoDefinitionInformation> {
function definitionLocation_godef(document: vscode.TextDocument, position: vscode.Position, offset: number, includeDocs: boolean, env: any): Promise<GoDefinitionInformation> {
return new Promise<GoDefinitionInformation>((resolve, reject) => {
let godef = getBinPath('godef');

// Spawn `godef` process
let p = cp.execFile(godef, ['-t', '-i', '-f', document.fileName, '-o', offset.toString()], {}, (err, stdout, stderr) => {
let p = cp.execFile(godef, ['-t', '-i', '-f', document.fileName, '-o', offset.toString()], {env}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('godef');
Expand Down Expand Up @@ -82,7 +83,7 @@ function definitionLocation_godef(document: vscode.TextDocument, position: vscod
if (!includeDocs) {
return resolve(definitionInformation);
}
cp.execFile(godoc, [pkgPath], {}, (err, stdout, stderr) => {
cp.execFile(godoc, [pkgPath], {env}, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
vscode.window.showInformationMessage('The "godoc" command is not available.');
}
Expand Down Expand Up @@ -112,10 +113,10 @@ function definitionLocation_godef(document: vscode.TextDocument, position: vscod
});
}

function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position, offset: number): Promise<GoDefinitionInformation> {
function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position, offset: number, env: any): Promise<GoDefinitionInformation> {
return new Promise<GoDefinitionInformation>((resolve, reject) => {
let gogetdoc = getBinPath('gogetdoc');
let p = cp.execFile(gogetdoc, ['-u', '-json', '-modified', '-pos', document.fileName + ':#' + offset.toString()], {}, (err, stdout, stderr) => {
let p = cp.execFile(gogetdoc, ['-u', '-json', '-modified', '-pos', document.fileName + ':#' + offset.toString()], {env}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gogetdoc');
Expand Down
5 changes: 3 additions & 2 deletions src/goFormat.ts
Expand Up @@ -10,7 +10,7 @@ import cp = require('child_process');
import path = require('path');
import { isDiffToolAvailable, getEdits, getEditsFromUnifiedDiffStr } from './diffUtils';
import { promptForMissingTool } from './goInstallTools';
import { sendTelemetryEvent, getBinPath } from './util';
import { sendTelemetryEvent, getBinPath, getToolsEnvVars } from './util';

export class Formatter {
public formatDocument(document: vscode.TextDocument): Thenable<vscode.TextEdit[]> {
Expand All @@ -28,7 +28,8 @@ export class Formatter {
formatFlags.splice(formatFlags.indexOf('-w'), 1);
}
let t0 = Date.now();
cp.execFile(formatCommandBinPath, [...formatFlags, filename], {}, (err, stdout, stderr) => {
let env = getToolsEnvVars();
cp.execFile(formatCommandBinPath, [...formatFlags, filename], {env}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool(formatTool);
Expand Down
5 changes: 3 additions & 2 deletions src/goLiveErrors.ts
@@ -1,7 +1,7 @@
'use strict';

import vscode = require('vscode');
import { byteOffsetAt, getBinPath } from './util';
import { byteOffsetAt, getBinPath, getToolsEnvVars } from './util';
import cp = require('child_process');
import path = require('path');
import { promptForMissingTool } from './goInstallTools';
Expand Down Expand Up @@ -58,7 +58,8 @@ function processFile(e: vscode.TextDocumentChangeEvent) {
let fileContents = e.document.getText();
let fileName = e.document.fileName;
let args = ['-e', '-a', '-lf=' + fileName, path.dirname(fileName)];
let p = cp.execFile(gotypeLive, args, (err, stdout, stderr) => {
let env = getToolsEnvVars();
let p = cp.execFile(gotypeLive, args, {env}, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gotype-live');
return;
Expand Down
3 changes: 0 additions & 3 deletions src/goMain.ts
Expand Up @@ -6,9 +6,6 @@
'use strict';

import vscode = require('vscode');
import fs = require('fs');
import path = require('path');
import cp = require('child_process');
import { GoCompletionItemProvider } from './goSuggest';
import { GoHoverProvider } from './goExtraInfo';
import { GoDefinitionProvider } from './goDeclaration';
Expand Down
6 changes: 3 additions & 3 deletions src/goReferences.ts
Expand Up @@ -8,7 +8,7 @@
import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import { getBinPath, byteOffsetAt, canonicalizeGOPATHPrefix, getFileArchive } from './util';
import { getBinPath, byteOffsetAt, canonicalizeGOPATHPrefix, getFileArchive, getToolsEnvVars } from './util';
import { promptForMissingTool } from './goInstallTools';

export class GoReferenceProvider implements vscode.ReferenceProvider {
Expand All @@ -29,11 +29,11 @@ export class GoReferenceProvider implements vscode.ReferenceProvider {
}

let offset = byteOffsetAt(document, position);

let env = getToolsEnvVars();
let goGuru = getBinPath('guru');
let buildTags = '"' + vscode.workspace.getConfiguration('go')['buildTags'] + '"';

let process = cp.execFile(goGuru, ['-modified', '-tags', buildTags, 'referrers', `${filename}:#${offset.toString()}`], {}, (err, stdout, stderr) => {
let process = cp.execFile(goGuru, ['-modified', '-tags', buildTags, 'referrers', `${filename}:#${offset.toString()}`], {env}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('guru');
Expand Down
6 changes: 3 additions & 3 deletions src/goRename.ts
Expand Up @@ -7,7 +7,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath, byteOffsetAt, canonicalizeGOPATHPrefix } from './util';
import { getBinPath, byteOffsetAt, canonicalizeGOPATHPrefix, getToolsEnvVars } from './util';
import { getEditsFromUnifiedDiffStr, isDiffToolAvailable, FilePatch, Edit } from './diffUtils';
import { promptForMissingTool } from './goInstallTools';

Expand All @@ -25,7 +25,7 @@ export class GoRenameProvider implements vscode.RenameProvider {
let range = document.getWordRangeAtPosition(position);
let pos = range ? range.start : position;
let offset = byteOffsetAt(document, pos);

let env = getToolsEnvVars();
let gorename = getBinPath('gorename');
let buildTags = '"' + vscode.workspace.getConfiguration('go')['buildTags'] + '"';
let gorenameArgs = ['-offset', filename + ':#' + offset, '-to', newName, '-tags', buildTags];
Expand All @@ -34,7 +34,7 @@ export class GoRenameProvider implements vscode.RenameProvider {
gorenameArgs.push('-d');
}

cp.execFile(gorename, gorenameArgs, {}, (err, stdout, stderr) => {
cp.execFile(gorename, gorenameArgs, {env}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gorename');
Expand Down
7 changes: 4 additions & 3 deletions src/goSuggest.ts
Expand Up @@ -8,7 +8,7 @@
import vscode = require('vscode');
import cp = require('child_process');
import { dirname, basename } from 'path';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords } from './util';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars } from './util';
import { promptForMissingTool } from './goInstallTools';
import { listPackages, getTextEditForAddImport } from './goImport';

Expand Down Expand Up @@ -129,7 +129,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
// Unset GOOS and GOARCH for the `gocode` process to ensure that GOHOSTOS and GOHOSTARCH
// are used as the target operating system and architecture. `gocode` is unable to provide
// autocompletion when the Go environment is configured for cross compilation.
let env = Object.assign({}, process.env, { GOOS: '', GOARCH: '' });
let env = Object.assign({}, getToolsEnvVars(), { GOOS: '', GOARCH: '' });
let stdout = '';
let stderr = '';

Expand Down Expand Up @@ -241,7 +241,8 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}
let gocode = getBinPath('gocode');
let autobuild = vscode.workspace.getConfiguration('go')['gocodeAutoBuild'];
cp.execFile(gocode, ['set', 'propose-builtins', 'true'], {}, (err, stdout, stderr) => {
let env = getToolsEnvVars();
cp.execFile(gocode, ['set', 'propose-builtins', 'true'], {env}, (err, stdout, stderr) => {
cp.execFile(gocode, ['set', 'autobuild', autobuild], {}, (err, stdout, stderr) => {
resolve();
});
Expand Down
5 changes: 3 additions & 2 deletions src/goSymbol.ts
Expand Up @@ -6,7 +6,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath } from './util';
import { getBinPath, getToolsEnvVars } from './util';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';

// Keep in sync with github.com/acroca/go-symbols'
Expand Down Expand Up @@ -66,8 +66,9 @@ export function getWorkspaceSymbols(workspacePath: string, query: string, goConf
args.push(workspacePath);
args.push(query);
let gosyms = getBinPath('go-symbols');
let env = getToolsEnvVars();
return new Promise((resolve, reject) => {
let p = cp.execFile(gosyms, args, { maxBuffer: 1024 * 1024 }, (err, stdout, stderr) => {
let p = cp.execFile(gosyms, args, { maxBuffer: 1024 * 1024, env }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-symbols');
Expand Down
3 changes: 2 additions & 1 deletion src/goTest.ts
Expand Up @@ -12,6 +12,7 @@ import util = require('util');
import os = require('os');
import { getGoRuntimePath } from './goPath';
import { GoDocumentSymbolProvider } from './goOutline';
import { getToolsEnvVars } from './util';

let outputChannel = vscode.window.createOutputChannel('Go Tests');

Expand Down Expand Up @@ -203,7 +204,7 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> {

let buildTags: string = testconfig.goConfig['buildTags'];
let args = ['test', ...testconfig.flags, '-timeout', testconfig.goConfig['testTimeout'], '-tags', buildTags];
let testEnvVars = Object.assign({}, process.env, testconfig.goConfig['testEnvVars']);
let testEnvVars = Object.assign({}, getToolsEnvVars(), testconfig.goConfig['testEnvVars']);
let goRuntimePath = getGoRuntimePath();

if (!goRuntimePath) {
Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Expand Up @@ -285,4 +285,12 @@ export function getCurrentGoWorkspaceFromGOPATH(currentFileDirPath: string): str
export function getFileArchive(document: vscode.TextDocument): string {
let fileContents = document.getText();
return document.fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
}

export function getToolsEnvVars(): any {
let toolsEnvVars = vscode.workspace.getConfiguration('go')['toolsEnvVars'];
if (!toolsEnvVars || typeof toolsEnvVars !== 'object' || Object.keys(toolsEnvVars).length === 0) {
return process.env;
}
return Object.assign({}, process.env, toolsEnvVars);
}

0 comments on commit bca4dd5

Please sign in to comment.