Skip to content

Commit ea788d6

Browse files
committed
[bazel] use rctx.getenv instead of repository environ
Bazel documentation says that `environ` is deprecated. This change removes the usage of it and replace `rctx.os.environ` with `rctx.getenv` which is the recommended way that automatically registered a dependency. This change also fixes the missing dependency on `HOME` and `PATH`. Suggested-by: Amaury Pouly <[email protected]> Signed-off-by: Gary Guo <[email protected]>
1 parent d6be5b8 commit ea788d6

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

rules/bitstreams.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ design (i.e. mix-and-match).
5151
def _bitstreams_repo_impl(rctx):
5252
# First, check if an existing pre-built bitstream cache repo exists, and if
5353
# so, use it instead of building one.
54-
cache_path = rctx.os.environ.get(
54+
cache_path = rctx.getenv(
5555
"BAZEL_BITSTREAMS_CACHE",
5656
rctx.attr.cache,
5757
)
58+
59+
# Used by the cache manager
60+
rctx.getenv("BITSTREAM")
5861
rctx.watch(rctx.attr.python_interpreter)
5962
rctx.watch(rctx.attr._cache_manager)
6063
result = rctx.execute(
@@ -121,7 +124,6 @@ bitstreams_repo = repository_rule(
121124
allow_files = True,
122125
),
123126
},
124-
environ = ["BAZEL_BITSTREAMS_CACHE", "BITSTREAM"],
125127
# This rule depends on the Git repository, but there's no ergonomic way to
126128
# encode the dependency in Bazel. Instead, indicate that the rule depends on
127129
# something outside of Bazel's dependency graph and rely on the user calling

rules/nonhermetic.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ to Bazel rules as possible, thus improves reproducibility and cacheability.
3333
"""
3434

3535
def _nonhermetic_repo_impl(rctx):
36-
env = "\n".join([" \"{}\": \"{}\",".format(v, rctx.os.environ.get(v, "")) for v in NONHERMETIC_ENV_VARS])
37-
home = rctx.os.environ.get("HOME", "")
36+
env = "\n".join([" \"{}\": \"{}\",".format(v, rctx.getenv(v, "")) for v in NONHERMETIC_ENV_VARS])
37+
home = rctx.getenv("HOME", "")
3838

39+
# Declare sensitivity on PATH, this is not implied by `rctx.which`
40+
path = rctx.getenv("PATH")
3941
bins = {name: rctx.which(name) for name in NONHERMETIC_BINS}
4042
bin_paths = "\n".join([" \"{}\": \"{}\",".format(name, rctx.path(path).dirname if path != None else "/no-such-path") for name, path in bins.items()])
4143

@@ -45,5 +47,4 @@ def _nonhermetic_repo_impl(rctx):
4547
nonhermetic_repo = repository_rule(
4648
implementation = _nonhermetic_repo_impl,
4749
attrs = {},
48-
environ = NONHERMETIC_ENV_VARS,
4950
)

0 commit comments

Comments
 (0)