Skip to content

Commit 7b2f165

Browse files
committed
[release] src/goEnvironmentStatus.ts: handle failed getGoVersion call
getGoVersion can return undefined. Handle the case and display `Go (unknown)` in the status item. Made formatGoVersion to take GoVersion or undefined. Also, utilize GoVersion.format(includePrerelease) to choose the correct version string. It turned out GoVersion.format uses sesmver.format, which reformats version strings like 1.14rc1 to 1.14.0. That's not ideal for go version display purpose. Change-Id: I923a9efff90abaa4b6b4a4018ed402a04161169e Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/245439 Reviewed-by: Rebecca Stambler <[email protected]> (cherry picked from commit d8f9560) Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/245601
1 parent 0bcbca0 commit 7b2f165

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/goEnvironmentStatus.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import WebRequest = require('web-request');
1515
import { toolInstallationEnvironment } from './goEnv';
1616
import { outputChannel } from './goStatus';
1717
import { getFromWorkspaceState, updateWorkspaceState } from './stateUtils';
18-
import { getBinPath, getGoVersion, getTempFilePath, rmdirRecursive } from './util';
18+
import { getBinPath, getGoVersion, getTempFilePath, rmdirRecursive, GoVersion } from './util';
1919
import { correctBinname, getBinPathFromEnvVar, getCurrentGoRoot, pathExists } from './utils/goPath';
2020

2121
export class GoEnvironmentOption {
@@ -46,7 +46,7 @@ export async function initGoStatusBar() {
4646
}
4747
// set Go version and command
4848
const version = await getGoVersion();
49-
const goOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version.format()));
49+
const goOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
5050

5151
hideGoStatusBar();
5252
goEnvStatusbarItem.text = goOption.label;
@@ -367,16 +367,16 @@ export function getGoEnvironmentStatusbarItem(): vscode.StatusBarItem {
367367
return goEnvStatusbarItem;
368368
}
369369

370-
export function formatGoVersion(version: string): string {
371-
const versionWords = version.split(' ');
372-
if (versionWords[0] === 'devel') {
370+
export function formatGoVersion(version?: GoVersion): string {
371+
if (!version || !version.isValid()) {
372+
return `Go (unknown)`;
373+
}
374+
const versionStr = version.format(true);
375+
const versionWords = versionStr.split(' ');
376+
if (versionWords.length > 1 && versionWords[0] === 'devel') {
373377
// Go devel +hash
374-
return `Go ${versionWords[0]} ${versionWords[4]}`;
375-
} else if (versionWords.length > 0) {
376-
// some other version format
377-
return `Go ${version.substr(0, 8)}`;
378+
return `Go ${versionWords[1]}`;
378379
} else {
379-
// default semantic version format
380380
return `Go ${versionWords[0]}`;
381381
}
382382
}
@@ -411,7 +411,7 @@ export async function getDefaultGoOption(): Promise<GoEnvironmentOption> {
411411
const version = await getGoVersion();
412412
return new GoEnvironmentOption(
413413
path.join(goroot, 'bin', correctBinname('go')),
414-
formatGoVersion(version.format()),
414+
formatGoVersion(version),
415415
);
416416
}
417417

test/integration/statusbar.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('#initGoStatusBar()', function () {
4343

4444
it('should create a status bar item with a label matching go.goroot version', async () => {
4545
const version = await ourutil.getGoVersion();
46-
const versionLabel = formatGoVersion(version.format());
46+
const versionLabel = formatGoVersion(version);
4747
assert.equal(
4848
getGoEnvironmentStatusbarItem().text,
4949
versionLabel,
@@ -58,7 +58,7 @@ describe('#setSelectedGo()', async function () {
5858
let goOption: GoEnvironmentOption;
5959
let defaultMemento: vscode.Memento;
6060
const version = await ourutil.getGoVersion();
61-
const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version.format()));
61+
const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
6262

6363
this.beforeAll(async () => {
6464
defaultMemento = getWorkspaceState();
@@ -116,7 +116,7 @@ describe('#updateGoVarsFromConfig()', async function () {
116116
let tmpRootBin: string | undefined;
117117
let sandbox: sinon.SinonSandbox | undefined;
118118
const version = await ourutil.getGoVersion();
119-
const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version.format()));
119+
const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
120120

121121
this.beforeAll(async () => {
122122
defaultMemento = getWorkspaceState();

0 commit comments

Comments
 (0)