diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index c89129d69..87aa7ff9e 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -2,7 +2,7 @@ tasks: ubuntu1804_bazel400: platform: ubuntu1804 - bazel: 5.1.0 # test minimum supported version of bazel + bazel: 5.2.0 # test minimum supported version of bazel shell_commands: - tests/core/cgo/generate_imported_dylib.sh build_targets: diff --git a/README.rst b/README.rst index 954cc2d9e..f32d2c67c 100644 --- a/README.rst +++ b/README.rst @@ -193,7 +193,7 @@ The Go rules are tested and supported on the following host platforms: Users have reported success on several other platforms, but the rules are only tested on those listed above. -Note: Since version v0.35.0, rules_go requires Bazel ≥ 5.1.0 to work. +Note: Since version v0.38.0, rules_go requires Bazel ≥ 5.2.0 to work. The ``master`` branch is only guaranteed to work with the latest version of Bazel. diff --git a/docs/go/core/rules.md b/docs/go/core/rules.md index e0a70e662..10f4a9b6d 100644 --- a/docs/go/core/rules.md +++ b/docs/go/core/rules.md @@ -331,8 +331,8 @@ This declares a set of source files and related dependencies that can be embedde
 go_test(name, cdeps, cgo, clinkopts, copts, cppopts, cxxopts, data, deps, embed, embedsrcs, env,
-        gc_goopts, gc_linkopts, goarch, goos, gotags, importpath, linkmode, msan, pure, race, rundir,
-        srcs, static, x_defs)
+        env_inherit, gc_goopts, gc_linkopts, goarch, goos, gotags, importpath, linkmode, msan, pure,
+        race, rundir, srcs, static, x_defs)
 
This builds a set of tests that can be run with `bazel test`.

@@ -383,6 +383,7 @@ This builds a set of tests that can be run with `bazel test`.

| embed | List of Go libraries whose sources should be compiled together with this package's sources. Labels listed here must name go_library, go_proto_library, or other compatible targets with the [GoLibrary] and [GoSource] providers. Embedded libraries must have the same importpath as the embedding library. At most one embedded library may have cgo = True, and the embedding library may not also have cgo = True. See [Embedding] for more information. | List of labels | optional | [] | | embedsrcs | The list of files that may be embedded into the compiled package using //go:embed directives. All files must be in the same logical directory or a subdirectory as source files. All source files containing //go:embed directives must be in the same logical directory. It's okay to mix static and generated source files and static and generated embeddable files. | List of labels | optional | [] | | env | Environment variables to set for the test execution. The values (but not keys) are subject to [location expansion](https://docs.bazel.build/versions/main/skylark/macros.html) but not full [make variable expansion](https://docs.bazel.build/versions/main/be/make-variables.html). | Dictionary: String -> String | optional | {} | +| env_inherit | Environment variables to inherit from the external environment. | List of strings | optional | [] | | gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | | gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | | goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | diff --git a/go/private/common.bzl b/go/private/common.bzl index 7a1c4135e..d0efaaf95 100644 --- a/go/private/common.bzl +++ b/go/private/common.bzl @@ -169,7 +169,7 @@ def get_versioned_shared_lib_extension(path): # something like 1.2.3, or so.1.2, or dylib.1.2, or foo.1.2 return "" -MINIMUM_BAZEL_VERSION = "5.1.0" +MINIMUM_BAZEL_VERSION = "5.2.0" def as_list(v): """Returns a list, tuple, or depset as a list.""" diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index d0e11a854..d30704d00 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -163,6 +163,8 @@ def _go_test_impl(ctx): for k, v in ctx.attr.env.items(): env[k] = ctx.expand_location(v, ctx.attr.data) + test_environment = testing.TestEnvironment(env, ctx.attr.env_inherit) + # Bazel only looks for coverage data if the test target has an # InstrumentedFilesProvider. If the provider is found and at least one # source file is present, Bazel will set the COVERAGE_OUTPUT_FILE @@ -184,7 +186,7 @@ def _go_test_impl(ctx): dependency_attributes = ["data", "deps", "embed", "embedsrcs"], extensions = ["go"], ), - testing.TestEnvironment(env), + test_environment, ] _go_test_kwargs = { @@ -244,6 +246,10 @@ _go_test_kwargs = { [make variable expansion](https://docs.bazel.build/versions/main/be/make-variables.html). """, ), + "env_inherit": attr.string_list( + doc = """Environment variables to inherit from the external environment. + """, + ), "importpath": attr.string( doc = """The import path of this test. Tests can't actually be imported, but this may be used by [go_path] and other tools to report the location of source diff --git a/tests/core/go_test/BUILD.bazel b/tests/core/go_test/BUILD.bazel index 829ca8d59..ac36f9310 100644 --- a/tests/core/go_test/BUILD.bazel +++ b/tests/core/go_test/BUILD.bazel @@ -243,3 +243,8 @@ go_test( "@io_bazel_rules_go//go/tools/bazel", ], ) + +go_bazel_test( + name = "env_inherit_test", + srcs = ["env_inherit_test.go"], +) diff --git a/tests/core/go_test/env_inherit_test.go b/tests/core/go_test/env_inherit_test.go new file mode 100644 index 000000000..b425abe2d --- /dev/null +++ b/tests/core/go_test/env_inherit_test.go @@ -0,0 +1,61 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package env_inherit_test + +import ( + "os" + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, bazel_testing.Args{ + Main: ` +-- src/BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_test") +go_test( + name = "main", + srcs = ["env_inherit.go"], + env_inherit = ["INHERITEDVAR"], +) +-- src/env_inherit.go -- +package env_inherit_test + +import ( + "os" + "testing" +) + +func TestInherit(t *testing.T) { + v := os.Getenv("INHERITEDVAR") + if v != "b" { + t.Fatalf("INHERITEDVAR was not equal to b") + } +} +`, + + SetUp: func() error { + os.Setenv("INHERITEDVAR", "b") + return nil + }, + }) +} + +func TestInheritedEnvVar(t *testing.T) { + if err := bazel_testing.RunBazel("test", "//src:main"); err != nil { + t.Fatal(err) + } +}