From 47774b9a21948507c0ec0c8219b3a4c895fe4728 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Mon, 3 Jan 2022 16:59:12 -0800 Subject: [PATCH] Added `native.bazel_version` for use in BUILD and bzl files --- .../packages/BazelStarlarkEnvironment.java | 6 ++- .../StarlarkRepositoryIntegrationTest.java | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/packages/BazelStarlarkEnvironment.java b/src/main/java/com/google/devtools/build/lib/packages/BazelStarlarkEnvironment.java index a0af5ec1c3c433..e28530554a7fee 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/BazelStarlarkEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/packages/BazelStarlarkEnvironment.java @@ -70,7 +70,7 @@ public final class BazelStarlarkEnvironment { this.ruleFunctions = ruleFunctions; this.uninjectedBuildBzlNativeBindings = createUninjectedBuildBzlNativeBindings( - ruleFunctions, packageFunction, environmentExtensions); + ruleFunctions, packageFunction, environmentExtensions, version); this.workspaceBzlNativeBindings = createWorkspaceBzlNativeBindings(ruleClassProvider, version); this.uninjectedBuildBzlEnv = createUninjectedBuildBzlEnv(ruleClassProvider, uninjectedBuildBzlNativeBindings); @@ -143,7 +143,8 @@ public ImmutableMap getBzlmodBzlEnv() { private static ImmutableMap createUninjectedBuildBzlNativeBindings( Map ruleFunctions, Object packageFunction, - List environmentExtensions) { + List environmentExtensions, + String version) { ImmutableMap.Builder env = new ImmutableMap.Builder<>(); env.putAll(StarlarkNativeModule.BINDINGS_FOR_BUILD_FILES); env.putAll(ruleFunctions); @@ -151,6 +152,7 @@ private static ImmutableMap createUninjectedBuildBzlNativeBindin for (PackageFactory.EnvironmentExtension ext : environmentExtensions) { ext.updateNative(env); } + env.put("bazel_version", version); return env.build(); } diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryIntegrationTest.java index 8968e41573dc03..e1e0d6692b0434 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryIntegrationTest.java @@ -537,4 +537,46 @@ public void testPackageAndRepositoryNameFunctionsInExternalRepository() throws E getConfiguredTarget("@foo//p:BUILD"); // (loadPackage(@foo//p) would suffice) assertContainsEvent("repo=@foo pkg=p"); } + + @Test + public void testNativeBazelVersion() throws Exception { + reporter.removeHandler(failFastHandler); + scratch.file( + "repo_bzl_ver.bzl", + "def _repo_impl(repository_ctx):", + " repository_ctx.file('BUILD', '')", + " repository_ctx.file('defs.bzl', 'BAZEL_VERSION = \"' + native.bazel_version + '\"')", + " repository_ctx.file('WORKSPACE', '')", + "", + "repo = repository_rule(", + " implementation=_repo_impl,", + " local=True)"); + scratch.file( + "bzl_ver.bzl", + "load('@repo2//:defs.bzl', 'BAZEL_VERSION')", + "def _impl(ctx):", + " if BAZEL_VERSION == native.bazel_version:", + " print('native.bazel_version matches')", + " else:", + " fail(BAZEL_VERSION + ' does not equal ' + native.bazel_version)", + " output = ctx.actions.declare_file(ctx.label.name + '.txt')", + " ctx.actions.write(output = output, content = native.bazel_version)", + " return DefaultInfo(files = depset([output]))", + "", + "check = rule(", + " implementation=_impl)"); + scratch.file("BUILD", "load('@//:bzl_ver.bzl', 'check')", "check(name = 'check')"); + scratch.overwriteFile( + rootDirectory.getRelative("WORKSPACE").getPathString(), + new ImmutableList.Builder() + .addAll(analysisMock.getWorkspaceContents(mockToolsConfig)) + .add("load('@//:repo_bzl_ver.bzl', 'repo')") + .add("repo(name = 'repo2')") + .build()); + + invalidatePackages(); + + getConfiguredTarget("//:check"); + assertContainsEvent("native.bazel_version matches"); + } }