Skip to content

Commit

Permalink
Create bazel_version repo rule and check version
Browse files Browse the repository at this point in the history
  • Loading branch information
ttpathan authored and fmeum committed Jan 22, 2023
1 parent 4f5c07b commit 3bf2657
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ load("//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("//proto:gogo.bzl", "gogo_special_proto")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# native.bazel_version is only available in WORKSPACE macros
# https://github.com/bazelbuild/bazel/issues/8305
# Needed for guarding env_inherit support https://github.com/bazelbuild/rules_go/pull/3256 which requires 5.2.0+
def _bazel_version_repository_impl(repository_ctx):
repository_ctx.file("bazel_version.bzl", "bazel_version = \"{}\"".format(native.bazel_version))
repository_ctx.file("BUILD", "")

_bazel_version_repository = repository_rule(
implementation = _bazel_version_repository_impl,
local = True,
)

def go_rules_dependencies(force = False):
"""Declares workspaces the Go rules depend on. Workspaces that use
rules_go should call this.
Expand All @@ -38,6 +50,8 @@ def go_rules_dependencies(force = False):
if getattr(native, "bazel_version", None):
versions.check(MINIMUM_BAZEL_VERSION, bazel_version = native.bazel_version)

_bazel_version_repository(name = "rules_go_bazel_version")

if force:
wrapper = _always
else:
Expand Down
4 changes: 2 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ load(
"@bazel_skylib//lib:structs.bzl",
"structs",
)
load("@rules_go_bazel_version//:bazel_version.bzl", "bazel_version")
load(
"//go/private/skylib/lib:versions.bzl",
"versions",
Expand Down Expand Up @@ -171,8 +172,7 @@ def _go_test_impl(ctx):
if ctx.attr.env_inherit:
# inherited_environment is only available in Bazel 5.2.0+
# https://github.com/bazelbuild/rules_go/pull/3256
if not getattr(native, "bazel_version", None) or not versions.check("5.2.0", bazel_version = native.bazel_version):
fail("env_inherit is only available in Bazel 5.2.0 or higher")
versions.check("5.2.0", bazel_version = bazel_version)
test_environment = testing.TestEnvironment(env, ctx.attr.env_inherit)

# Bazel only looks for coverage data if the test target has an
Expand Down

0 comments on commit 3bf2657

Please sign in to comment.