Skip to content

Commit

Permalink
don't cache environment PATH at the module level
Browse files Browse the repository at this point in the history
Fixes golang#2617

Change-Id: Iea940a855f5d20d713b575eb971405fe30859766
GitHub-Last-Rev: 4307d4c
GitHub-Pull-Request: golang#2772
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/494555
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
niklaskorz authored and hyangah committed May 15, 2023
1 parent 9547119 commit 0755de9
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/commands/getConfiguredGoTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { inspectGoToolVersion } from '../goInstallTools';
import { outputChannel } from '../goStatus';
import { getConfiguredTools } from '../goTools';
import { getBinPath, getCurrentGoPath, getGoEnv, getGoVersion, getToolsGopath } from '../util';
import { envPath, getCurrentGoRoot } from '../utils/pathUtils';
import { getEnvPath, initialEnvPath, getCurrentGoRoot } from '../utils/pathUtils';

export const getConfiguredGoTools: CommandFactory = () => {
return async () => {
Expand All @@ -31,10 +31,10 @@ export const getConfiguredGoTools: CommandFactory = () => {
outputChannel.appendLine('toolsGopath: ' + getToolsGopath());
outputChannel.appendLine('gopath: ' + getCurrentGoPath());
outputChannel.appendLine('GOROOT: ' + getCurrentGoRoot());
const currentEnvPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
const currentEnvPath = getEnvPath();
outputChannel.appendLine('PATH: ' + currentEnvPath);
if (currentEnvPath !== envPath) {
outputChannel.appendLine(`PATH (vscode launched with): ${envPath}`);
if (currentEnvPath !== initialEnvPath) {
outputChannel.appendLine(`PATH (vscode launched with): ${initialEnvPath}`);
}
outputChannel.appendLine('');

Expand Down
4 changes: 2 additions & 2 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
import { parseEnvFiles } from '../utils/envUtils';
import {
correctBinname,
envPath,
getEnvPath,
expandFilePathInOutput,
fixDriveCasingInWindows,
getBinPathWithPreferredGopathGoroot,
Expand Down Expand Up @@ -609,7 +609,7 @@ export class Delve {
log(
`Couldn't find dlv at the Go tools path, ${process.env['GOPATH']}${
env['GOPATH'] ? ', ' + env['GOPATH'] : ''
} or ${envPath}`
} or ${getEnvPath()}`
);
return reject(
'Cannot find Delve debugger. Install from https://github.com/go-delve/delve & ensure it is in your Go tools path, "GOPATH/bin" or "PATH".'
Expand Down
4 changes: 2 additions & 2 deletions src/diffUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import jsDiff = require('diff');
import { Position, Range, TextEditorEdit, Uri, WorkspaceEdit } from 'vscode';
import { getBinPathFromEnvVar } from './utils/pathUtils';
import { getBinPathFromEnvVar, getEnvPath } from './utils/pathUtils';

let diffToolAvailable: boolean | null = null;

export function isDiffToolAvailable(): boolean {
if (diffToolAvailable == null) {
const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
const envPath = getEnvPath();
if (!envPath) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/goBrowsePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import vscode = require('vscode');
import { CommandFactory } from './commands';
import { getAllPackages } from './goPackages';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import { getEnvPath, getCurrentGoRoot } from './utils/pathUtils';

export const browsePackages: CommandFactory = () => () => {
let workDir = '';
Expand Down Expand Up @@ -43,7 +43,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(${getCurrentGoRoot()}) or PATH(${envPath})`
`Failed to run "go list" to fetch packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()})`
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/goDebugFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as net from 'net';
import { Logger, logVerbose, TimestampedLogger } from './goLogging';
import { DebugProtocol } from 'vscode-debugprotocol';
import { getWorkspaceFolderPath } from './util';
import { envPath, getBinPathFromEnvVar } from './utils/pathUtils';
import { getEnvPath, getBinPathFromEnvVar } from './utils/pathUtils';

export function activate(ctx: vscode.ExtensionContext) {
const debugOutputChannel = vscode.window.createOutputChannel('Go Debug');
Expand Down Expand Up @@ -460,7 +460,7 @@ export class DelveDAPOutputAdapter extends ProxyDebugAdapter {
let sudoPath: string | null | undefined = undefined;
function getSudo(): string | null {
if (sudoPath === undefined) {
sudoPath = getBinPathFromEnvVar('sudo', envPath, false);
sudoPath = getBinPathFromEnvVar('sudo', getEnvPath(), false);
}
return sudoPath;
}
Expand Down Expand Up @@ -625,7 +625,7 @@ function getSpawnConfig(launchAttachArgs: vscode.DebugConfiguration, logErr: (ms
const dlvPath = launchAttachArgs.dlvToolPath ?? 'dlv';

if (!fs.existsSync(dlvPath)) {
const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
const envPath = getEnvPath();
logErr(
`Couldn't find ${dlvPath} at the Go tools path, ${process.env['GOPATH']}${
env['GOPATH'] ? ', ' + env['GOPATH'] : ''
Expand Down
4 changes: 2 additions & 2 deletions src/goGetPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CommandFactory } from './commands';
import { buildCode } from './goBuild';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import { getEnvPath, getCurrentGoRoot } from './utils/pathUtils';

export const goGetPackage: CommandFactory = (ctx, goCtx) => () => {
const editor = vscode.window.activeTextEditor;
Expand All @@ -26,7 +26,7 @@ export const goGetPackage: CommandFactory = (ctx, goCtx) => () => {
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(${getCurrentGoRoot()}) or PATH(${envPath})`
`Failed to run "go get" to get package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()})`
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { promptForMissingTool } from './goInstallTools';
import { documentSymbols, GoOutlineImportsOptions } from './language/legacy/goOutline';
import { getImportablePackages } from './goPackages';
import { getBinPath, getImportPath, parseFilePrelude } from './util';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import { getEnvPath, getCurrentGoRoot } from './utils/pathUtils';
import { GoExtensionContext } from './context';
import { CommandFactory } from './commands';

Expand Down Expand Up @@ -251,7 +251,7 @@ export const addImportToWorkspace: CommandFactory = () => () => {
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(${getCurrentGoRoot()}) 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(${getEnvPath()})`
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/goInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { toolExecutionEnvironment } from './goEnv';
import { isModSupported } from './goModules';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getModuleCache } from './util';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
import { getEnvPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';

export const installCurrentPackage: CommandFactory = () => async () => {
const editor = vscode.window.activeTextEditor;
Expand All @@ -30,7 +30,7 @@ export const installCurrentPackage: CommandFactory = () => async () => {
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(${getCurrentGoRoot()}) 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(${getEnvPath()})`
);
return;
}
Expand Down
12 changes: 9 additions & 3 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ import {
GoVersion,
rmdirRecursive
} from './util';
import { correctBinname, envPath, executableFileExists, getCurrentGoRoot, setCurrentGoRoot } from './utils/pathUtils';
import {
correctBinname,
getEnvPath,
executableFileExists,
getCurrentGoRoot,
setCurrentGoRoot
} from './utils/pathUtils';
import util = require('util');
import vscode = require('vscode');
import { RestartReason } from './language/goLanguageServer';
Expand Down Expand Up @@ -376,7 +382,7 @@ export async function promptForMissingTool(toolName: string) {
const tool = getTool(toolName);
if (!tool) {
vscode.window.showWarningMessage(
`${toolName} is not found. Please make sure it is installed and available in the PATH ${envPath}`
`${toolName} is not found. Please make sure it is installed and available in the PATH ${getEnvPath()}`
);
return;
}
Expand Down Expand Up @@ -661,7 +667,7 @@ let suggestedDownloadGo = false;

async function suggestDownloadGo() {
const msg =
`Failed to find the "go" binary in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath}). ` +
`Failed to find the "go" binary in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()}). ` +
'Check PATH, or Install Go and reload the window. ' +
"If PATH isn't what you expected, see https://github.com/golang/vscode-go/issues/971";

Expand Down
6 changes: 3 additions & 3 deletions src/goModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import { outputChannel } from './goStatus';
import { getTool } from './goTools';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getGoVersion, getModuleCache, getWorkspaceFolderPath } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/pathUtils';
import { getEnvPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/pathUtils';
import { CommandFactory } from './commands';
export let GO111MODULE: string | undefined;

export async function runGoEnv(uri?: vscode.Uri, envvars: string[] = []): Promise<any> {
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(${getCurrentGoRoot()}) 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(${getEnvPath()})`
);
return {};
}
Expand Down Expand Up @@ -160,7 +160,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(${getCurrentGoRoot()}) 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(${getEnvPath()})`
);
return '';
}
Expand Down
11 changes: 8 additions & 3 deletions src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { promisify } from 'util';
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { getBinPath, getCurrentGoPath } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
import {
getEnvPath,
fixDriveCasingInWindows,
getCurrentGoRoot,
getCurrentGoWorkspaceFromGOPATH
} from './utils/pathUtils';

type GoListPkgsDone = (res: Map<string, PackageInfo>) => void;
interface Cache {
Expand Down Expand Up @@ -51,7 +56,7 @@ async function goListPkgs(workDir?: string): Promise<Map<string, PackageInfo>> {
const goBin = getBinPath('go');
if (!goBin) {
vscode.window.showErrorMessage(
`Failed to run "go list" to fetch packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
`Failed to run "go list" to fetch packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()})`
);
return pkgs;
}
Expand Down Expand Up @@ -263,7 +268,7 @@ export function getImportPathToFolder(targets: string[], cwd?: string): Promise<
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(${getCurrentGoRoot()}) PATH(${envPath})`
`Failed to run "go list" to find packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) PATH(${getEnvPath()})`
);
return Promise.resolve(new Map());
}
Expand Down
4 changes: 2 additions & 2 deletions src/language/legacy/goImplementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getGoConfig } from '../../config';
import { toolExecutionEnvironment } from '../../goEnv';
import { promptForMissingTool } from '../../goInstallTools';
import { byteOffsetAt, canonicalizeGOPATHPrefix, getBinPath, getWorkspaceFolderPath } from '../../util';
import { envPath, getCurrentGoRoot } from '../../utils/pathUtils';
import { getEnvPath, getCurrentGoRoot } from '../../utils/pathUtils';
import { killProcessTree } from '../../utils/processUtils';

interface GoListOutput {
Expand Down Expand Up @@ -53,7 +53,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(${getCurrentGoRoot()}) 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(${getEnvPath()})`
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pickProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cp = require('child_process');
import { QuickPickItem } from 'vscode';
import { getBinPath } from './util';
import { lsofDarwinCommand, parseLsofProcesses } from './utils/lsofProcessParser';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import { getEnvPath, getCurrentGoRoot } from './utils/pathUtils';
import { parsePsProcesses, psDarwinCommand, psLinuxCommand } from './utils/psProcessParser';
import { parseWmicProcesses, wmicCommand } from './utils/wmicProcessParser';
import vscode = require('vscode');
Expand Down Expand Up @@ -99,7 +99,7 @@ async function getGoProcesses(): Promise<AttachItem[]> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go version" as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
`Failed to run "go version" as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()})`
);
return processes;
}
Expand Down
9 changes: 7 additions & 2 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { GoDocumentSymbolProvider } from './goDocumentSymbols';
import { getNonVendorPackages } from './goPackages';
import { getBinPath, getCurrentGoPath, getTempFilePath, LineBuffer, resolvePath } from './util';
import { parseEnvFile } from './utils/envUtils';
import { envPath, expandFilePathInOutput, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
import {
getEnvPath,
expandFilePathInOutput,
getCurrentGoRoot,
getCurrentGoWorkspaceFromGOPATH
} from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';
import { GoExtensionContext } from './context';

Expand Down Expand Up @@ -276,7 +281,7 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go test" as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
`Failed to run "go test" as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()})`
);
return Promise.resolve(false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getCurrentPackage } from './goModules';
import { outputChannel } from './goStatus';
import { getFromWorkspaceState } from './stateUtils';
import {
envPath,
getEnvPath,
fixDriveCasingInWindows,
getBinPathWithPreferredGopathGorootWithExplanation,
getCurrentGoRoot,
Expand Down Expand Up @@ -337,7 +337,7 @@ export async function getGoVersion(goBinPath?: string): Promise<GoVersion> {
};

if (!goRuntimePath) {
throw error(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${envPath})`);
throw error(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${getEnvPath()})`);
}
if (cachedGoBinPath === goRuntimePath && cachedGoVersion) {
if (cachedGoVersion.isValid()) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { logVerbose } from '../goLogging';

let binPathCache: { [bin: string]: string } = {};

export const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
export const getEnvPath = () => process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
export const initialEnvPath = getEnvPath();

// find the tool's path from the given PATH env var, or null if the tool is not found.
export function getBinPathFromEnvVar(
Expand Down Expand Up @@ -101,7 +102,7 @@ export function getBinPathWithPreferredGopathGorootWithExplanation(
}

// Finally search PATH parts
const pathFromPath = getBinPathFromEnvVar(binname, envPath, false);
const pathFromPath = getBinPathFromEnvVar(binname, getEnvPath(), false);
if (pathFromPath) {
binPathCache[toolName] = pathFromPath;
return { binPath: pathFromPath, why: found('path') };
Expand Down

0 comments on commit 0755de9

Please sign in to comment.