Skip to content

Commit

Permalink
goroot: remove references to GOROOT env variable
Browse files Browse the repository at this point in the history
The extension currently makes use of an environment variable, "GOROOT",
which is set to match go.root from the user's settings. This change
removes all references to that environment variable and replaces it with
a helper function `getCurrentGoRoot` which uses the go config section of
the package.json

Updates golang#146

Change-Id: Ia4280cd6195e8ff559869fd8c3d8a4cf602ac548
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/237819
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
mcjcloud authored and hyangah committed Jun 16, 2020
1 parent 51b6e00 commit 91c626c
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/goBrowsePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cp = require('child_process');
import path = require('path');
import vscode = require('vscode');
import { getAllPackages } from './goPackages';
import { envPath } from './goPath';
import { envPath, getCurrentGoRoot } from './goPath';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';

export function browsePackages() {
Expand Down Expand Up @@ -40,7 +40,7 @@ function showPackageFiles(pkg: string, showAllPkgsIfPkgNotFound: boolean, workDi
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
return vscode.window.showErrorMessage(
`Failed to run "go list" to fetch packages as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go list" to fetch packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/goGetPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cp = require('child_process');
import vscode = require('vscode');
import { buildCode } from './goBuild';
import { envPath } from './goPath';
import { envPath, getCurrentGoRoot } from './goPath';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';

Expand All @@ -26,7 +26,7 @@ export function goGetPackage() {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
return vscode.window.showErrorMessage(
`Failed to run "go get" to get package as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go get" to get package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/goImplementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import path = require('path');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { envPath } from './goPath';
import { envPath, getCurrentGoRoot } from './goPath';
import {
byteOffsetAt,
canonicalizeGOPATHPrefix,
getBinPath,
getGoConfig,
getWorkspaceFolderPath,
killTree
killTree,
} from './util';

interface GoListOutput {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class GoImplementationProvider implements vscode.ImplementationProvider {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go list" to get the scope to find implementations as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go list" to get the scope to find implementations as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { documentSymbols, GoOutlineImportsOptions } from './goOutline';
import { getImportablePackages } from './goPackages';
import { envPath } from './goPath';
import { envPath, getCurrentGoRoot } from './goPath';
import { getBinPath, getImportPath, parseFilePrelude } from './util';

const missingToolMsg = 'Missing tool: ';
Expand Down Expand Up @@ -180,7 +180,7 @@ export function addImportToWorkspace() {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go list" to find the package as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go list" to find the package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/goInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path = require('path');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { isModSupported } from './goModules';
import { envPath, getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getGoConfig, getModuleCache } from './util';

Expand All @@ -28,7 +28,7 @@ export async function installCurrentPackage(): Promise<void> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go install" to install the package as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go install" to install the package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down
18 changes: 6 additions & 12 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import vscode = require('vscode');
import { toolInstallationEnvironment } from './goEnv';
import { getLanguageServerToolPath } from './goLanguageServer';
import { restartLanguageServer } from './goMain';
import { envPath, getToolFromToolPath } from './goPath';
import { envPath, getCurrentGoRoot, getToolFromToolPath, setCurrentGoRoot } from './goPath';
import { hideGoStatus, outputChannel, showGoStatus } from './goStatus';
import {
containsTool,
Expand All @@ -33,8 +33,7 @@ import {
getGoVersion,
getTempFilePath,
GoVersion,
resolvePath,
rmdirRecursive
rmdirRecursive,
} from './util';

// declinedUpdates tracks the tools that the user has declined to update.
Expand Down Expand Up @@ -336,20 +335,15 @@ export async function promptForUpdatingTool(toolName: string, newVersion?: SemVe
}

export function updateGoVarsFromConfig(): Promise<void> {
const goroot = getGoConfig()['goroot'];
if (goroot) {
process.env['GOROOT'] = resolvePath(goroot);
}

if (process.env['GOPATH'] && process.env['GOROOT'] && process.env['GOPROXY'] && process.env['GOBIN']) {
if (getCurrentGoRoot() && process.env['GOPATH'] && process.env['GOPROXY'] && process.env['GOBIN']) {
return Promise.resolve();
}

// If GOPATH is still not set, then use the one from `go env`
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go env" to find GOPATH as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go env" to find GOPATH as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down Expand Up @@ -381,8 +375,8 @@ export function updateGoVarsFromConfig(): Promise<void> {
if (!process.env['GOPATH'] && envOutput[0].trim()) {
process.env['GOPATH'] = envOutput[0].trim();
}
if (!process.env['GOROOT'] && envOutput[1] && envOutput[1].trim()) {
process.env['GOROOT'] = envOutput[1].trim();
if (!getCurrentGoRoot() && envOutput[1] && envOutput[1].trim()) {
setCurrentGoRoot(envOutput[1].trim());
}
if (!process.env['GOPROXY'] && envOutput[2] && envOutput[2].trim()) {
process.env['GOPROXY'] = envOutput[2].trim();
Expand Down
24 changes: 18 additions & 6 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { lintCode } from './goLint';
import { GO_MODE } from './goMode';
import { addTags, removeTags } from './goModifytags';
import { GO111MODULE, isModSupported } from './goModules';
import { clearCacheForTools, fileExists } from './goPath';
import { clearCacheForTools, fileExists, getCurrentGoRoot, setCurrentGoRoot } from './goPath';
import { playgroundCommand } from './goPlayground';
import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
Expand All @@ -48,8 +48,16 @@ import {
} from './stateUtils';
import { cancelRunningTests, showTestOutput } from './testUtils';
import {
cleanupTempDir, getBinPath, getCurrentGoPath, getExtensionCommands, getGoConfig,
getGoVersion, getToolsGopath, getWorkspaceFolderPath, handleDiagnosticErrors, isGoPathSet
cleanupTempDir,
getBinPath,
getCurrentGoPath,
getExtensionCommands,
getGoConfig,
getGoVersion,
getToolsGopath,
getWorkspaceFolderPath,
handleDiagnosticErrors,
isGoPathSet,
} from './util';

export let buildDiagnosticCollection: vscode.DiagnosticCollection;
Expand All @@ -64,6 +72,10 @@ export let restartLanguageServer = () => { return; };
export function activate(ctx: vscode.ExtensionContext): void {
setGlobalState(ctx.globalState);
setWorkspaceState(ctx.workspaceState);
const configGOROOT = getGoConfig()['goroot'];
if (!!configGOROOT) {
setCurrentGoRoot(configGOROOT);
}

updateGoVarsFromConfig().then(async () => {
const updateToolsCmdText = 'Update tools';
Expand All @@ -77,7 +89,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
toolsGoInfo[toolsGopath] = { goroot: null, version: null };
}
const prevGoroot = toolsGoInfo[toolsGopath].goroot;
const currentGoroot: string = process.env['GOROOT'] && process.env['GOROOT'].toLowerCase();
const currentGoroot: string = getCurrentGoRoot().toLowerCase();
if (prevGoroot && prevGoroot.toLowerCase() !== currentGoroot) {
vscode.window
.showInformationMessage(
Expand All @@ -99,7 +111,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
if (prevVersion) {
vscode.window
.showInformationMessage(
'Your Go version is different than before, few Go tools may need re-compiling',
'Your Go version is different than before, a few Go tools may need re-compiling',
updateToolsCmdText
)
.then((selected) => {
Expand Down Expand Up @@ -212,7 +224,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
outputChannel.appendLine('GOBIN: ' + process.env['GOBIN']);
outputChannel.appendLine('toolsGopath: ' + getToolsGopath());
outputChannel.appendLine('gopath: ' + getCurrentGoPath());
outputChannel.appendLine('GOROOT: ' + process.env['GOROOT']);
outputChannel.appendLine('GOROOT: ' + getCurrentGoRoot());
outputChannel.appendLine('PATH: ' + process.env['PATH']);
outputChannel.appendLine('');

Expand Down
6 changes: 3 additions & 3 deletions src/goModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path = require('path');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { installTools } from './goInstallTools';
import { envPath, fixDriveCasingInWindows } from './goPath';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './goPath';
import { getTool } from './goTools';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getGoConfig, getGoVersion, getModuleCache } from './util';
Expand All @@ -19,7 +19,7 @@ async function runGoModEnv(folderPath: string): Promise<string> {
const goExecutable = getBinPath('go');
if (!goExecutable) {
console.warn(
`Failed to run "go env GOMOD" to find mod file as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go env GOMOD" to find mod file as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export async function getCurrentPackage(cwd: string): Promise<string> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
console.warn(
`Failed to run "go list" to find current package as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go list" to find current package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path = require('path');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { envPath, fixDriveCasingInWindows, getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { getBinPath, getCurrentGoPath, getGoVersion, isVendorSupported } from './util';

type GopkgsDone = (res: Map<string, PackageInfo>) => void;
Expand Down Expand Up @@ -72,7 +72,7 @@ function gopkgs(workDir?: string): Promise<Map<string, PackageInfo>> {
);
return resolve(pkgs);
}
const goroot = process.env['GOROOT'];
const goroot = getCurrentGoRoot();
const output = chunks.join('');
if (output.indexOf(';') === -1) {
// User might be using the old gopkgs tool, prompt to update
Expand Down Expand Up @@ -262,7 +262,7 @@ export function getNonVendorPackages(currentFolderPath: string): Promise<Map<str
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
console.warn(
`Failed to run "go list" to find packages as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go list" to find packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) PATH(${envPath})`
);
return;
}
Expand Down
20 changes: 18 additions & 2 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export function getBinPathFromEnvVar(toolName: string, envVarValue: string, appe
return null;
}

export function getBinPathWithPreferredGopath(toolName: string, preferredGopaths: string[], alternateTool?: string) {
export function getBinPathWithPreferredGopath(
toolName: string,
preferredGopaths: string[],
alternateTool?: string
) {
if (binPathCache[toolName]) {
return binPathCache[toolName];
}
Expand Down Expand Up @@ -60,7 +64,7 @@ export function getBinPathWithPreferredGopath(toolName: string, preferredGopaths
}

// Check GOROOT (go, gofmt, godoc would be found here)
const pathFromGoRoot = getBinPathFromEnvVar(binname, process.env['GOROOT'], true);
const pathFromGoRoot = getBinPathFromEnvVar(binname, getCurrentGoRoot(), true);
if (pathFromGoRoot) {
binPathCache[toolName] = pathFromGoRoot;
return pathFromGoRoot;
Expand All @@ -87,6 +91,18 @@ export function getBinPathWithPreferredGopath(toolName: string, preferredGopaths
return toolName;
}

/**
* Returns the goroot path if it exists, otherwise returns an empty string
*/
let currentGoRoot = '';
export function getCurrentGoRoot(): string {
return currentGoRoot || process.env['GOROOT'] || '';
}

export function setCurrentGoRoot(goroot: string) {
currentGoRoot = goroot;
}

function correctBinname(toolName: string) {
if (process.platform === 'win32') {
return toolName + '.exe';
Expand Down
3 changes: 2 additions & 1 deletion src/goSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import cp = require('child_process');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getCurrentGoRoot } from './goPath';
import { getBinPath, getGoConfig, getWorkspaceFolderPath, killTree } from './util';

// Keep in sync with github.com/acroca/go-symbols'
Expand Down Expand Up @@ -93,7 +94,7 @@ export function getWorkspaceSymbols(
calls.push(callGoSymbols([...baseArgs, workspacePath, query], token));

if (gotoSymbolConfig.includeGoroot) {
const goRoot = process.env['GOROOT'];
const goRoot = getCurrentGoRoot();
const gorootCall = callGoSymbols([...baseArgs, goRoot, query], token);
calls.push(gorootCall);
}
Expand Down
6 changes: 3 additions & 3 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { toolExecutionEnvironment } from './goEnv';
import { getCurrentPackage } from './goModules';
import { GoDocumentSymbolProvider } from './goOutline';
import { getNonVendorPackages } from './goPackages';
import { envPath, getCurrentGoWorkspaceFromGOPATH, parseEnvFile } from './goPath';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH, parseEnvFile } from './goPath';
import {
getBinPath,
getCurrentGoPath,
getGoVersion,
getTempFilePath,
killTree,
LineBuffer,
resolvePath
resolvePath,
} from './util';

const outputChannel = vscode.window.createOutputChannel('Go Tests');
Expand Down Expand Up @@ -262,7 +262,7 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {

if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go test" as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`
`Failed to run "go test" as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return Promise.resolve();
}
Expand Down
7 changes: 4 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
envPath,
fixDriveCasingInWindows,
getBinPathWithPreferredGopath,
getCurrentGoRoot,
getInferredGopath,
resolveHomeDir
resolveHomeDir,
} from './goPath';
import { outputChannel } from './goStatus';
import { extensionId } from './telemetry';
Expand Down Expand Up @@ -307,7 +308,7 @@ export async function getGoVersion(): Promise<GoVersion | undefined> {
};

if (!goRuntimePath) {
warn(`unable to locate "go" binary in GOROOT (${process.env['GOROOT']}) or PATH (${envPath})`);
warn(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${envPath})`);
return;
}
if (cachedGoVersion) {
Expand Down Expand Up @@ -438,7 +439,7 @@ export function getBinPath(tool: string): string {
return getBinPathWithPreferredGopath(
tool,
tool === 'go' ? [] : [getToolsGopath(), getCurrentGoPath()],
resolvePath(alternateToolPath)
resolvePath(alternateToolPath),
);
}

Expand Down
Loading

0 comments on commit 91c626c

Please sign in to comment.