Skip to content

Commit bfee565

Browse files
committed
Improve missing-Go prompt with PATH diagnostics
1 parent 60c3930 commit bfee565

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

extension/src/goInstallTools.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -720,24 +720,67 @@ function getMissingTools(matcher?: (value: Tool) => boolean): Promise<Tool[]> {
720720

721721
let suggestedDownloadGo = false;
722722

723+
// Actions shown when the extension cannot locate the go binary.
724+
const SHOW_PATH_ENTRIES_ACTION = 'Show PATH Entries';
725+
const GO_DOWNLOAD_PAGE_ACTION = 'Go to Download Page';
726+
const GO_DOWNLOAD_URL = 'https://golang.org/dl/';
727+
728+
// Split PATH using the platform delimiter and drop empty entries for display.
729+
function getPathEntries(): string[] {
730+
return (getEnvPath() ?? '')
731+
.split(path.delimiter)
732+
.map((entry: string) => entry.trim())
733+
.filter(Boolean);
734+
}
735+
736+
async function handleDownloadGoChoice(choice: string | undefined): Promise<void> {
737+
if (choice === SHOW_PATH_ENTRIES_ACTION) {
738+
await showPathEntriesQuickPick();
739+
return;
740+
}
741+
742+
if (choice === GO_DOWNLOAD_PAGE_ACTION) {
743+
await vscode.env.openExternal(vscode.Uri.parse(GO_DOWNLOAD_URL));
744+
}
745+
return;
746+
}
747+
748+
async function showPathEntriesQuickPick() {
749+
const pathEntries = getPathEntries();
750+
751+
if (!pathEntries.length) {
752+
await vscode.window.showInformationMessage('PATH is empty.');
753+
return;
754+
}
755+
756+
await vscode.window.showQuickPick(pathEntries, {
757+
title: 'PATH Entries Searched for go',
758+
placeHolder: 'Entries from PATH used when searching for the go binary',
759+
ignoreFocusOut: true
760+
});
761+
}
762+
723763
async function suggestDownloadGo() {
724-
const msg =
725-
`Failed to find the "go" binary in either GOROOT(${getCurrentGoRoot()}) or PATH(${getEnvPath()}). ` +
726-
'Check PATH, or Install Go and reload the window. ' +
727-
"If PATH isn't what you expected, see https://github.com/golang/vscode-go/issues/971";
764+
const goRoot = getCurrentGoRoot() ?? '(not set)';
765+
const message =
766+
`Failed to find the "go" binary in either GOROOT(${goRoot}) or PATH. ` +
767+
'Check PATH, or install Go and reload the window.';
768+
const choice = await vscode.window.showErrorMessage(
769+
message,
770+
SHOW_PATH_ENTRIES_ACTION,
771+
GO_DOWNLOAD_PAGE_ACTION
772+
);
728773

729774
if (suggestedDownloadGo) {
730-
vscode.window.showErrorMessage(msg);
775+
await handleDownloadGoChoice(choice);
731776
return;
732777
}
733778

734-
const choice = await vscode.window.showErrorMessage(msg, 'Go to Download Page');
735-
if (choice === 'Go to Download Page') {
736-
vscode.env.openExternal(vscode.Uri.parse('https://golang.org/dl/'));
737-
}
779+
await handleDownloadGoChoice(choice);
738780
suggestedDownloadGo = true;
739781
}
740782

783+
741784
/**
742785
* Interface for the expected JSON output from `go list -m -versions -json`.
743786
* See https://go.dev/ref/mod#go-list-m for details.

0 commit comments

Comments
 (0)