Skip to content

Commit b55b69d

Browse files
JSGettecopybara-github
authored andcommitted
Fix NPE caused by --repo_env with no value set (#28606)
This is to address #28605. bazel crashes with `NullPointerException` in case `--repo_env=NON_EXISTENT` is used with no value set in user's local environment. It worked in `8.5.1` but doesn't in `9.0.0` so it seems like a regression. Closes #28606. PiperOrigin-RevId: 868871905 Change-Id: I0ecd9a4b76b7795e1f00583050aaf343fe930832
1 parent 795af54 commit b55b69d

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,11 @@ public void exit(AbruptExitException exception) {
400400
nonstrictRepoEnvBuilder.put(name, value);
401401
}
402402
case Converters.EnvVar.Inherit(String name) -> {
403-
repoEnvBuilder.put(name, clientEnv.get(name));
404-
nonstrictRepoEnvBuilder.put(name, clientEnv.get(name));
403+
String value = clientEnv.get(name);
404+
if (value != null) {
405+
repoEnvBuilder.put(name, value);
406+
nonstrictRepoEnvBuilder.put(name, value);
407+
}
405408
}
406409
case Converters.EnvVar.Unset(String name) -> {
407410
repoEnvBuilder.remove(name);

src/test/py/bazel/bazel_external_repository_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,43 @@ def testRepoEnv(self):
535535
self.assertIn('FOO bar', os.linesep.join(stderr))
536536
self.assertNotIn('null value in entry: FOO=null', os.linesep.join(stderr))
537537

538+
def testRepoEnvMissingVariable(self):
539+
# Test that --repo_env=VAR for a variable not set in the environment
540+
# is silently ignored and doesn't crash (regression test for NPE fix).
541+
# See https://github.com/bazelbuild/bazel/issues/28605
542+
self.ScratchFile(
543+
'MODULE.bazel',
544+
[
545+
'dump_env = use_repo_rule("//:main.bzl", "dump_env")',
546+
'dump_env(',
547+
' name = "debug"',
548+
')',
549+
],
550+
)
551+
self.ScratchFile('BUILD')
552+
self.ScratchFile(
553+
'main.bzl',
554+
[
555+
'def _dump_env(ctx):',
556+
' val = ctx.os.environ.get("NONEXISTENT_VAR", "not_found")',
557+
' print("NONEXISTENT_VAR", val)',
558+
' ctx.file("BUILD")',
559+
'dump_env = repository_rule(',
560+
' implementation = _dump_env,',
561+
' local = True,',
562+
')',
563+
],
564+
)
565+
566+
_, _, stderr = self.RunBazel(
567+
args=['build', '@debug//:all', '--repo_env=NONEXISTENT_VAR'],
568+
env_remove=['NONEXISTENT_VAR'],
569+
)
570+
stderr_str = os.linesep.join(stderr)
571+
self.assertNotIn('NullPointerException', stderr_str)
572+
self.assertNotIn('null value in entry', stderr_str)
573+
self.assertIn('NONEXISTENT_VAR not_found', stderr_str)
574+
538575
def testRepoEnvUnset(self):
539576
self.ScratchFile(
540577
'MODULE.bazel',

0 commit comments

Comments
 (0)