diff --git a/go/private/BUILD.sdk.bazel b/go/private/BUILD.sdk.bazel index d33813e098..305fce8c5b 100644 --- a/go/private/BUILD.sdk.bazel +++ b/go/private/BUILD.sdk.bazel @@ -97,6 +97,13 @@ config_setting( }, ) +config_setting( + name = "match_sdk_type", + flag_values = { + sdk_version_label: "{sdk_type}", + }, +) + selects.config_setting_group( name = "sdk_version_setting", match_any = [ @@ -105,6 +112,7 @@ selects.config_setting_group( ":match_major_minor_version", ":match_patch_version", ":match_prerelease_version", + ":match_sdk_type", ], ) diff --git a/go/private/sdk.bzl b/go/private/sdk.bzl index 3bc23d9fa8..9a092e12c3 100644 --- a/go/private/sdk.bzl +++ b/go/private/sdk.bzl @@ -31,7 +31,7 @@ def _go_host_sdk_impl(ctx): goroot = _detect_host_sdk(ctx) platform = _detect_sdk_platform(ctx, goroot) version = _detect_sdk_version(ctx, goroot) - _sdk_build_file(ctx, platform, version) + _sdk_build_file(ctx, platform, version, "host") _local_sdk(ctx, goroot) _go_host_sdk = repository_rule( @@ -231,7 +231,7 @@ def _local_sdk(ctx, path): for entry in ["src", "pkg", "bin", "lib"]: ctx.symlink(path + "/" + entry, entry) -def _sdk_build_file(ctx, platform, version): +def _sdk_build_file(ctx, platform, version, sdk_type = "remote"): ctx.file("ROOT") goos, _, goarch = platform.partition("_") @@ -254,6 +254,7 @@ def _sdk_build_file(ctx, platform, version): "{minor_version}": str(minor), "{patch_version}": str(patch), "{prerelease_suffix}": prerelease, + "{sdk_type}": sdk_type, }, ) diff --git a/go/toolchains.rst b/go/toolchains.rst index 25b7ee84c5..74f01e724b 100644 --- a/go/toolchains.rst +++ b/go/toolchains.rst @@ -76,6 +76,8 @@ the flag ``--@io_bazel_rules_go//go/toolchain:sdk_version="version"`` where ``"version"`` is the SDK version you would like to build with, eg. ``"1.18.3"``. The SDK version can omit the patch, or include a prerelease part, eg. ``"1"``, ``"1.18"``, ``"1.18.0"``, and ``"1.19.0beta1"`` are all valid values for ``sdk_version``. +When ``go_host_sdk`` is used, ``"version"`` can be set to ``host`` to refer to the host Go SDK. +It can also be set ``remote`` to match any non-host version. The toolchain ~~~~~~~~~~~~~ diff --git a/tests/core/go_download_sdk/go_download_sdk_test.go b/tests/core/go_download_sdk/go_download_sdk_test.go index 51dd5c0ae4..4a5999ee10 100644 --- a/tests/core/go_download_sdk/go_download_sdk_test.go +++ b/tests/core/go_download_sdk/go_download_sdk_test.go @@ -90,7 +90,7 @@ go_download_sdk( { desc: "multiple_sdks", rule: ` -load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk") +load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_host_sdk") go_download_sdk( name = "go_sdk", @@ -107,7 +107,8 @@ go_download_sdk( `, optToWantVersion: map[string]string{ "": "go1.16", - "--@io_bazel_rules_go//go/toolchain:sdk_version=1": "go1.16", + "--@io_bazel_rules_go//go/toolchain:sdk_version=remote": "go1.16", + "--@io_bazel_rules_go//go/toolchain:sdk_version=1": "go1.16", "--@io_bazel_rules_go//go/toolchain:sdk_version=1.17": "go1.17", "--@io_bazel_rules_go//go/toolchain:sdk_version=1.17.0": "go1.17", "--@io_bazel_rules_go//go/toolchain:sdk_version=1.17.1": "go1.17.1", @@ -122,7 +123,7 @@ go_download_sdk( i := bytes.Index(origWorkspaceData, []byte("go_rules_dependencies()")) if i < 0 { - t.Fatalf("%s: could not find call to go_rules_dependencies()") + t.Fatal("could not find call to go_rules_dependencies()") } buf := &bytes.Buffer{} @@ -143,17 +144,19 @@ go_register_toolchains() }() for opt, wantVersion := range test.optToWantVersion { - args := []string{ - "test", - "//:version_test", - "--test_arg=-version=" + wantVersion, - } - if opt != "" { - args = append(args, opt) - } - if err := bazel_testing.RunBazel(args...); err != nil { - t.Fatal(err) - } + t.Run(wantVersion, func(t *testing.T) { + args := []string{ + "test", + "//:version_test", + "--test_arg=-version=" + wantVersion, + } + if opt != "" { + args = append(args, opt) + } + if err := bazel_testing.RunBazel(args...); err != nil { + t.Fatal(err) + } + }) } }) }