Skip to content

Commit 27e8258

Browse files
committed
src/util.ts: throw error when getGoVersion fails
In most cases the use of getGoVersion is expected to return a value. Updating the function to throw an error in the case that a Go version is not found makes this explicit. For the exceptions where an explicit binary path is requested we handle the error thrown and show the existing warnings. For #57. Change-Id: Ib80ceec0636134b6694b0080ff19e0295d49d59f Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/400996 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Run-TryBot: Jamal Carvalho <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 95b8c3a commit 27e8258

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

src/goEnvironmentStatus.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ export async function setSelectedGo(goOption: vscode.QuickPickItem, promptReload
172172
vscode.window.showErrorMessage(`${newGoBin} is not an executable`);
173173
return false;
174174
}
175-
const newGo = await getGoVersion(newGoBin);
176-
if (!newGo || !newGo.isValid()) {
177-
vscode.window.showErrorMessage(`failed to get "${newGoBin} version", invalid Go binary`);
178-
return false;
175+
let newGo: GoVersion | undefined;
176+
try {
177+
newGo = await getGoVersion(newGoBin);
178+
await updateWorkspaceState('selectedGo', new GoEnvironmentOption(newGo.binaryPath, formatGoVersion(newGo)));
179+
} catch (e) {
180+
if (!newGo || !newGo.isValid()) {
181+
vscode.window.showErrorMessage(`failed to get "${newGoBin} version", invalid Go binary:\n${e}`);
182+
return false;
183+
}
179184
}
180-
await updateWorkspaceState('selectedGo', new GoEnvironmentOption(newGo.binaryPath, formatGoVersion(newGo)));
181185
}
182186
// prompt the user to reload the window.
183187
// promptReload defaults to true and should only be false for tests.

src/goInstallTools.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,24 @@ export async function installAllTools(updateExistingToolsOnly = false) {
9696
);
9797
}
9898

99-
export async function getGoForInstall(goVersion?: GoVersion, silent?: boolean): Promise<GoVersion> {
99+
export async function getGoForInstall(goVersion: GoVersion, silent?: boolean): Promise<GoVersion> {
100100
const configured = getGoConfig().get<string>('toolsManagement.go');
101101
if (!configured) {
102102
return goVersion;
103103
}
104104
if (executableFileExists(configured)) {
105-
const go = await getGoVersion(configured);
106-
if (go) return go;
107-
}
108-
if (!silent) {
109-
outputChannel.appendLine(
110-
`Ignoring misconfigured 'go.toolsManagement.go' (${configured}). Provide a valid Go command.`
111-
);
105+
try {
106+
const go = await getGoVersion(configured);
107+
if (go) return go;
108+
} finally {
109+
if (!silent) {
110+
outputChannel.appendLine(
111+
`Ignoring misconfigured 'go.toolsManagement.go' (${configured}). Provide a valid Go command.`
112+
);
113+
}
114+
}
112115
}
116+
113117
return goVersion;
114118
}
115119

src/util.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -321,54 +321,53 @@ export function getUserNameHash() {
321321

322322
/**
323323
* Gets version of Go based on the output of the command `go version`.
324-
* Returns undefined if go version can't be determined because
325-
* go is not available or `go version` fails.
324+
* Throws if go version can't be determined because go is not available
325+
* or `go version` fails.
326326
*/
327-
export async function getGoVersion(goBinPath?: string): Promise<GoVersion | undefined> {
327+
export async function getGoVersion(goBinPath?: string): Promise<GoVersion> {
328328
// TODO(hyangah): limit the number of concurrent getGoVersion call.
329329
// When the extension starts, at least 4 concurrent calls race
330330
// and end up calling `go version`.
331331

332332
const goRuntimePath = goBinPath ?? getBinPath('go');
333333

334-
const warn = (msg: string) => {
334+
const error = (msg: string) => {
335335
outputChannel.appendLine(msg);
336336
console.warn(msg);
337+
return new Error(msg);
337338
};
338339

339340
if (!goRuntimePath) {
340-
warn(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${envPath})`);
341-
return;
341+
throw error(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${envPath})`);
342342
}
343343
if (cachedGoBinPath === goRuntimePath && cachedGoVersion) {
344344
if (cachedGoVersion.isValid()) {
345345
return Promise.resolve(cachedGoVersion);
346346
}
347-
warn(`cached Go version (${JSON.stringify(cachedGoVersion)}) is invalid, recomputing`);
347+
// Don't throw an the error. Continue and recompute go version.
348+
error(`cached Go version (${JSON.stringify(cachedGoVersion)}) is invalid, recomputing`);
348349
}
349350
const docUri = vscode.window.activeTextEditor?.document.uri;
350351
const cwd = getWorkspaceFolderPath(docUri && docUri.fsPath.endsWith('.go') ? docUri : undefined);
351352

352-
let goVersion: GoVersion;
353+
let goVersion: GoVersion | undefined;
353354
try {
354355
const env = toolExecutionEnvironment();
355356
const execFile = util.promisify(cp.execFile);
356357
const { stdout, stderr } = await execFile(goRuntimePath, ['version'], { env, cwd });
357358
if (stderr) {
358-
warn(`failed to run "${goRuntimePath} version": stdout: ${stdout}, stderr: ${stderr}`);
359-
return;
359+
error(`failed to run "${goRuntimePath} version": stdout: ${stdout}, stderr: ${stderr}`);
360360
}
361361
goVersion = new GoVersion(goRuntimePath, stdout);
362362
} catch (err) {
363-
warn(`failed to run "${goRuntimePath} version": ${err} cwd: ${cwd}`);
364-
return;
363+
throw error(`failed to run "${goRuntimePath} version": ${err} cwd: ${cwd}`);
365364
}
366365
if (!goBinPath) {
367366
// if getGoVersion was called with a given goBinPath, don't cache the result.
368367
cachedGoBinPath = goRuntimePath;
369368
cachedGoVersion = goVersion;
370369
if (!cachedGoVersion.isValid()) {
371-
warn(`unable to determine version from the output of "${goRuntimePath} version": "${goVersion.svString}"`);
370+
error(`unable to determine version from the output of "${goRuntimePath} version": "${goVersion.svString}"`);
372371
}
373372
}
374373
return goVersion;

0 commit comments

Comments
 (0)