src/goInstallTools: fix gocode-gomod installation path

Should consider GOBIN, and then GOPATH.

Gets rid of the 'unexpected directory layout' condition handling.
The code path isn't reachable if the go command ever fails with the error.
(the first execFile will fail and causes an exception).

While we are here, improve the failed tool installation error message
by outputting the details of the failure including whether the command
failed with signals, what's code exit code, etc.
And, also set the default output to be 'no output', since otherwise
users will see 'undefined' as an error message which carries no info.

Change-Id: I2e14d2e4aa5f64831a5bb8b6487a670ddfa014f4
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/275877
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts
index 5c64e84..cb23c3a 100644
--- a/src/goInstallTools.ts
+++ b/src/goInstallTools.ts
@@ -198,7 +198,7 @@
 		const writeFile = util.promisify(fs.writeFile);
 		await writeFile(tmpGoModFile, 'module tools');
 	} else {
-		envForTools['GO111MODULE'] = 'off';
+		env['GO111MODULE'] = 'off';
 	}
 	// Some users use direnv-like setup where the choice of go is affected by
 	// the current directory path. In order to avoid choosing a different go,
@@ -225,7 +225,7 @@
 	}
 	args.push(importPath);
 
-	let output: string;
+	let output: string = 'no output';
 	let result: string = '';
 	try {
 		const opts = {
@@ -237,13 +237,10 @@
 		output = `${stdout} ${stderr}`;
 		logVerbose(`install: %s %s\n%s%s`, goBinary, args.join(' '), stdout, stderr);
 
-		// TODO(rstambler): Figure out why this happens and maybe delete it.
-		if (stderr.indexOf('unexpected directory layout:') > -1) {
-			await execFile(goBinary, args, opts);
-		} else if (hasModSuffix(tool)) {
-			const gopath = env['GOPATH'];
+		if (hasModSuffix(tool)) {  // Actual installation of the -gomod tool is done by running go build.
+			const gopath = env['GOBIN'] ?? env['GOPATH'];
 			if (!gopath) {
-				return `GOPATH not configured in environment`;
+				return `GOBIN/GOPATH not configured in environment`;
 			}
 			const destDir = gopath.split(path.delimiter)[0];
 			const outputFile = path.join(destDir, 'bin', process.platform === 'win32' ? `${tool.name}.exe` : tool.name);
@@ -253,7 +250,8 @@
 		outputChannel.appendLine(`Installing ${importPath} (${toolInstallPath}) SUCCEEDED`);
 	} catch (e) {
 		outputChannel.appendLine(`Installing ${importPath} FAILED`);
-		result = `failed to install ${tool.name}(${importPath}): ${e} ${output} `;
+		outputChannel.appendLine(`${JSON.stringify(e, null, 1)}`);
+		result = `failed to install ${tool.name}(${importPath}): ${e} ${output}`;
 	}
 
 	// Delete the temporary installation directory.