From d4663a1c950d618c5b15a3e00fb733987cbf45cc Mon Sep 17 00:00:00 2001 From: Chenchu K Date: Tue, 5 Jul 2022 15:04:59 -0500 Subject: [PATCH] Add repo env test (#15768) Fix and test for null value bug when using --repo_env [Issue Link](https://github.com/bazelbuild/bazel/issues/15430) Closes #15618. PiperOrigin-RevId: 457728307 Change-Id: I3d57ae4ac95de09215281bec51329b7baafd53af Co-authored-by: Googler --- .../build/lib/runtime/CommandEnvironment.java | 4 ++-- .../bazel/bazel_external_repository_test.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java index 7cf73865591cae..9d6919cf83c10d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java @@ -262,8 +262,8 @@ public void exit(AbruptExitException exception) { value = System.getenv(name); } if (value != null) { - repoEnv.put(entry.getKey(), entry.getValue()); - repoEnvFromOptions.put(entry.getKey(), entry.getValue()); + repoEnv.put(name, value); + repoEnvFromOptions.put(name, value); } } } diff --git a/src/test/py/bazel/bazel_external_repository_test.py b/src/test/py/bazel/bazel_external_repository_test.py index 3cdd45dfa9ea06..c97ba3fcf7c651 100644 --- a/src/test/py/bazel/bazel_external_repository_test.py +++ b/src/test/py/bazel/bazel_external_repository_test.py @@ -427,5 +427,27 @@ def testExternalBazelignoreContainingRepoName(self): args=['build', '@other_repo//:file'], cwd=work_dir) self.AssertExitCode(exit_code, 0, stderr) + def testRepoEnv(self): + # Testing fix for issue: https://github.com/bazelbuild/bazel/issues/15430 + + self.ScratchFile('WORKSPACE', [ + 'load("//:main.bzl", "dump_env")', 'dump_env(', ' name = "debug"', + ')' + ]) + self.ScratchFile('BUILD') + self.ScratchFile('main.bzl', [ + 'def _dump_env(ctx):', ' val = ctx.os.environ.get("FOO", "nothing")', + ' print("FOO", val)', ' ctx.file("BUILD")', + 'dump_env = repository_rule(', ' implementation = _dump_env,', + ' local = True,', ')' + ]) + + exit_code, _, stderr = self.RunBazel( + args=['build', '@debug//:all', '--repo_env=FOO'], + env_add={'FOO': 'bar'}) + self.AssertExitCode(exit_code, 0, stderr) + self.assertIn('FOO bar', os.linesep.join(stderr)) + self.assertNotIn('null value in entry: FOO=null', os.linesep.join(stderr)) + if __name__ == '__main__': unittest.main()