From 4d4a2454c588dab27853ccffb03fcb9205001192 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 15 Sep 2022 16:21:13 +0200 Subject: [PATCH] Parse Go SDK version out of `go version` instead of `VERSION` (#3296) The top-level `VERSION` file does not exist in all Go SDK distributions, e.g. those shipped by Debian or Fedora. --- go/private/sdk.bzl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/go/private/sdk.bzl b/go/private/sdk.bzl index ce4cb3855e..3bc23d9fa8 100644 --- a/go/private/sdk.bzl +++ b/go/private/sdk.bzl @@ -302,13 +302,18 @@ def _detect_sdk_platform(ctx, goroot): return platforms[0] def _detect_sdk_version(ctx, goroot): - path = goroot + "/VERSION" - version_contents = ctx.read(path) - - # VERSION file has version prefixed by go, eg. go1.18.3 - version = version_contents[2:] + go_binary_path = goroot + "/bin/go" + result = ctx.execute([go_binary_path, "version"]) + if result.return_code != 0: + fail("Could not detect SDK version: '%s version' exited with exit code %d" % (go_binary_path, result.return_code)) + + # go version output is of the form "go version go1.18.3 linux/amd64" + output_parts = result.stdout.split(" ") + if len(output_parts) < 3 or not output_parts[2].startswith("go"): + fail("Could not parse SDK version from '%s version' output: %s" % (go_binary_path, result.stdout)) + version = output_parts[2][len("go"):] if _parse_version(version) == None: - fail("Could not parse SDK version from version file (%s): %s" % (path, version_contents)) + fail("Could not parse SDK version from '%s version' output: %s" % (go_binary_path, result.stdout)) return version def _parse_versions_json(data):