src/goInstallTools: handle unknown tools

For example, when the extension detects that the specified custom formatting
tool does not exist, it will attempt to install or prompt the user for installing
the missing tool. However, it's likely the tool is not known to our extension,
so getTool won't be able to return a valid Tool. Handle this case by checking
the return value of getTool.

I expect there are still many corner cases and UX bugs in handling custom
formatter errors. But, assuming use of custom formatters is rare, polishing
the custom formatter UX is a low priority.

For golang/vscode-go#1238

Change-Id: If6deedf9285c6d070ad4d960f78c5430be54afd5
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/446299
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts
index d607d57..b09d2a2 100644
--- a/src/goInstallTools.ts
+++ b/src/goInstallTools.ts
@@ -374,6 +374,12 @@
 
 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}`
+		);
+		return;
+	}
 
 	// If user has declined to install this tool, don't prompt for it.
 	if (declinedToolInstall(toolName)) {
@@ -444,6 +450,9 @@
 	message?: string
 ) {
 	const tool = getTool(toolName);
+	if (!tool) {
+		return; // not a tool known to us.
+	}
 	const toolVersion = { ...tool, version: newVersion }; // ToolWithVersion
 
 	// If user has declined to update, then don't prompt.
@@ -737,9 +746,9 @@
 			dep     github.com/BurntSushi/toml      v0.3.1  h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 
 		   if the binary was built with a dev version of go, in module mode.
-		    /Users/hakim/go/bin/gopls: devel go1.18-41f485b9a7 Mon Jan 31 13:43:52 2022 +0000
+			/Users/hakim/go/bin/gopls: devel go1.18-41f485b9a7 Mon Jan 31 13:43:52 2022 +0000
 			path    golang.org/x/tools/gopls
-            mod     golang.org/x/tools/gopls        v0.8.0-pre.1    h1:6iHi9bCJ8XndQtBEFFG/DX+eTJrf2lKFv4GI3zLeDOo=
+			mod     golang.org/x/tools/gopls        v0.8.0-pre.1    h1:6iHi9bCJ8XndQtBEFFG/DX+eTJrf2lKFv4GI3zLeDOo=
 			...
 		*/
 		const lines = stdout.split('\n', 3);
diff --git a/src/goTools.ts b/src/goTools.ts
index 8db1dc8..7a47263 100644
--- a/src/goTools.ts
+++ b/src/goTools.ts
@@ -188,9 +188,9 @@
 			break;
 	}
 
-	// Only add format tools if the language server is disabled and the
+	// Only add format tools if the language server is disabled or the
 	// format tool is known to us.
-	if (goConfig['useLanguageServer'] === false && !usingCustomFormatTool(goConfig)) {
+	if (goConfig['useLanguageServer'] === false || usingCustomFormatTool(goConfig)) {
 		maybeAddTool(getFormatTool(goConfig));
 	}