Skip to content

Commit 9156fb4

Browse files
committed
src/goMain: use inspectGoToolVersion instead of runGoVersionM
And reduce the verbosity of `Go: Locate Configured Go Tools`. With go1.18 which includes more details in go version -m output, reporting everything from `go version -m` will be too excessive. `Go: Locate Configured Go Tools` used runGoVersionM to retrieve the build info of each located tool. However, the default inspectGoToolVersion also utilizes `go version -m`, so, they overlap with each other. As we also want to reduce the verbosity of the `Go: Locate Configured Go Tools` output, runGoVersionM is not much different from inspectGoToolVersion any more. This CL unifies them. Since go1.18 changes the build info encoding, we will see failed `go version -m` more often. So, let's not print the error output in the output channel. For #1939 Change-Id: I1f674b030ca97804d34df63c66272cddb42b1c65 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/382159 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
1 parent 3e9fb0e commit 9156fb4

File tree

3 files changed

+15
-28
lines changed

3 files changed

+15
-28
lines changed

src/goInstallTools.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,15 @@ export async function latestToolVersion(tool: Tool, includePrerelease?: boolean)
677677
// inspectGoToolVersion reads the go version and module version
678678
// of the given go tool using `go version -m` command.
679679
export const inspectGoToolVersion = defaultInspectGoToolVersion;
680-
async function defaultInspectGoToolVersion(binPath: string): Promise<{ goVersion?: string; moduleVersion?: string }> {
680+
async function defaultInspectGoToolVersion(
681+
binPath: string
682+
): Promise<{ goVersion?: string; moduleVersion?: string; debugInfo?: string }> {
681683
const goCmd = getBinPath('go');
682684
const execFile = util.promisify(cp.execFile);
685+
let debugInfo = 'go version -m failed';
683686
try {
684687
const { stdout } = await execFile(goCmd, ['version', '-m', binPath]);
688+
debugInfo = stdout;
685689
/* The output format will look like this
686690
687691
if the binary was built in module mode.
@@ -705,11 +709,9 @@ async function defaultInspectGoToolVersion(binPath: string): Promise<{ goVersion
705709
const moduleVersion = lines[2].split(/\s+/)[3];
706710
return { goVersion, moduleVersion };
707711
} catch (e) {
708-
outputChannel.appendLine(
709-
`Failed to determine the version of ${binPath}. For debugging, run "go version -m ${binPath}"`
710-
);
711-
// either go version failed or stdout is not in the expected format.
712-
return {};
712+
// either go version failed (e.g. the tool was compiled with a more recent version of go)
713+
// or stdout is not in the expected format.
714+
return { debugInfo };
713715
}
714716
}
715717

src/goMain.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { implCursor } from './goImpl';
3838
import { addImport, addImportToWorkspace } from './goImport';
3939
import { installCurrentPackage } from './goInstall';
4040
import {
41+
inspectGoToolVersion,
4142
installAllTools,
4243
installTools,
4344
offerToInstallTools,
@@ -94,11 +95,9 @@ import {
9495
getGoVersion,
9596
getToolsGopath,
9697
getWorkspaceFolderPath,
97-
GoVersion,
9898
handleDiagnosticErrors,
9999
isGoPathSet,
100-
resolvePath,
101-
runGoVersionM
100+
resolvePath
102101
} from './util';
103102
import { clearCacheForTools, fileExists, getCurrentGoRoot, dirExists, envPath } from './utils/pathUtils';
104103
import { WelcomePanel } from './welcome';
@@ -955,11 +954,11 @@ async function getConfiguredGoToolsCommand() {
955954
if (goVersionTooOld) {
956955
return `\t${tool.name}:\t${toolPath}: unknown version`;
957956
}
958-
try {
959-
const out = await runGoVersionM(toolPath);
960-
return `\t${tool.name}:${out.replace(/^/gm, '\t')}`;
961-
} catch (e) {
962-
return `\t${tool.name}:\t${toolPath}: go version -m failed: ${e}`;
957+
const { goVersion, moduleVersion, debugInfo } = await inspectGoToolVersion(toolPath);
958+
if (goVersion || moduleVersion) {
959+
return `\t${tool.name}:\t${toolPath}\t(version: ${moduleVersion} built with go: ${goVersion})`;
960+
} else {
961+
return `\t${tool.name}:\t${toolPath}\t(version: unknown - ${debugInfo})`;
963962
}
964963
})
965964
);

src/util.ts

-14
Original file line numberDiff line numberDiff line change
@@ -389,20 +389,6 @@ export async function getGoEnv(cwd?: string): Promise<string> {
389389
return stdout;
390390
}
391391

392-
/**
393-
* Returns the output of `go version -m` with the toolPath.
394-
*/
395-
export async function runGoVersionM(toolPath: string): Promise<string> {
396-
const goRuntime = getBinPath('go');
397-
const execFile = util.promisify(cp.execFile);
398-
const opts = { env: toolExecutionEnvironment() };
399-
const { stdout, stderr } = await execFile(goRuntime, ['version', '-m', toolPath], opts);
400-
if (stderr) {
401-
throw new Error(`failed to run 'go version -m ${toolPath}': ${stderr}`);
402-
}
403-
return stdout;
404-
}
405-
406392
/**
407393
* Returns boolean indicating if GOPATH is set or not
408394
* If not set, then prompts user to do set GOPATH

0 commit comments

Comments
 (0)