Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for env_inherit #3256

Merged
merged 14 commits into from
Jan 22, 2023
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions docs/go/core/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ This declares a set of source files and related dependencies that can be embedde

<pre>
go_test(<a href="#go_test-name">name</a>, <a href="#go_test-cdeps">cdeps</a>, <a href="#go_test-cgo">cgo</a>, <a href="#go_test-clinkopts">clinkopts</a>, <a href="#go_test-copts">copts</a>, <a href="#go_test-cppopts">cppopts</a>, <a href="#go_test-cxxopts">cxxopts</a>, <a href="#go_test-data">data</a>, <a href="#go_test-deps">deps</a>, <a href="#go_test-embed">embed</a>, <a href="#go_test-embedsrcs">embedsrcs</a>, <a href="#go_test-env">env</a>,
<a href="#go_test-gc_goopts">gc_goopts</a>, <a href="#go_test-gc_linkopts">gc_linkopts</a>, <a href="#go_test-goarch">goarch</a>, <a href="#go_test-goos">goos</a>, <a href="#go_test-gotags">gotags</a>, <a href="#go_test-importpath">importpath</a>, <a href="#go_test-linkmode">linkmode</a>, <a href="#go_test-msan">msan</a>, <a href="#go_test-pure">pure</a>, <a href="#go_test-race">race</a>, <a href="#go_test-rundir">rundir</a>,
<a href="#go_test-srcs">srcs</a>, <a href="#go_test-static">static</a>, <a href="#go_test-x_defs">x_defs</a>)
<a href="#go_test-env_inherit">env_inherit</a>, <a href="#go_test-gc_goopts">gc_goopts</a>, <a href="#go_test-gc_linkopts">gc_linkopts</a>, <a href="#go_test-goarch">goarch</a>, <a href="#go_test-goos">goos</a>, <a href="#go_test-gotags">gotags</a>, <a href="#go_test-importpath">importpath</a>, <a href="#go_test-linkmode">linkmode</a>, <a href="#go_test-msan">msan</a>, <a href="#go_test-pure">pure</a>,
<a href="#go_test-race">race</a>, <a href="#go_test-rundir">rundir</a>, <a href="#go_test-srcs">srcs</a>, <a href="#go_test-static">static</a>, <a href="#go_test-x_defs">x_defs</a>)
</pre>

This builds a set of tests that can be run with `bazel test`.<br><br>
Expand Down Expand Up @@ -383,6 +383,7 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
| <a id="go_test-embed"></a>embed | List of Go libraries whose sources should be compiled together with this package's sources. Labels listed here must name <code>go_library</code>, <code>go_proto_library</code>, or other compatible targets with the [GoLibrary] and [GoSource] providers. Embedded libraries must have the same <code>importpath</code> as the embedding library. At most one embedded library may have <code>cgo = True</code>, and the embedding library may not also have <code>cgo = True</code>. See [Embedding] for more information. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="go_test-embedsrcs"></a>embedsrcs | The list of files that may be embedded into the compiled package using <code>//go:embed</code> directives. All files must be in the same logical directory or a subdirectory as source files. All source files containing <code>//go:embed</code> directives must be in the same logical directory. It's okay to mix static and generated source files and static and generated embeddable files. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="go_test-env"></a>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). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="go_test-env_inherit"></a>env_inherit | Environment variables to inherit from the external environment. | List of strings | optional | [] |
| <a id="go_test-gc_goopts"></a>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 | [] |
| <a id="go_test-gc_linkopts"></a>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 | [] |
| <a id="go_test-goarch"></a>goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with <code>--platforms</code>.<br><br> This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set <code>pure</code> = <code>off</code>.<br><br> See [Cross compilation] for more information. | String | optional | "auto" |
Expand Down
2 changes: 1 addition & 1 deletion go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
8 changes: 7 additions & 1 deletion go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/core/go_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
61 changes: 61 additions & 0 deletions tests/core/go_test/env_inherit_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}