Skip to content

Commit

Permalink
Windows: Make environment variables case-insensitive when creating Wi…
Browse files Browse the repository at this point in the history
…ndows process

If users pass the same environment variables with different cases,
the last one will override the previous ones.

Fixed #5285

Change-Id: Ieec857553bc54a4ad9809455687551303037e240
PiperOrigin-RevId: 198303684
  • Loading branch information
meteorcloudy authored and Copybara-Service committed May 28, 2018
1 parent a9c0d4e commit 4015862
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private String getSystemRoot(Map<String, String> env) {
* Converts an environment map to the format expected in lpEnvironment by CreateProcess().
*/
private byte[] convertEnvToNative(Map<String, String> env) throws IOException {
Map<String, String> realEnv = new TreeMap<>();
Map<String, String> realEnv = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
realEnv.putAll(env == null ? System.getenv() : env);
if (getSystemRoot(realEnv) == null) {
// Some versions of MSVCRT.DLL require SystemRoot to be set. It's quite a common library to
Expand Down
32 changes: 32 additions & 0 deletions src/test/py/bazel/bazel_windows_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ def testWindowsCompilesAssembly(self):
self.AssertExitCode(exit_code, 0, stderr)
self.assertTrue(os.path.exists(os.path.join(bazel_bin, 'x.exe')))

def testWindowsEnvironmentVariablesSetting(self):
self.ScratchFile('BUILD')
self.ScratchFile('WORKSPACE', [
'load(":repo.bzl", "my_repo")',
'my_repo(name = "env_test")',
])
self.ScratchFile('repo.bzl', [
'def my_repo_impl(repository_ctx):',
' repository_ctx.file("env.bat", "set FOO\\n")',
' env = {"foo" : "bar2", "Foo": "bar3",}',
' result = repository_ctx.execute(["./env.bat"], environment = env)',
' print(result.stdout)',
' repository_ctx.file("BUILD")',
'',
'my_repo = repository_rule(',
' implementation = my_repo_impl,',
')',
])

exit_code, _, stderr = self.RunBazel(
[
'build',
'@env_test//...',
],
env_add={'FOO': 'bar1'},
)
self.AssertExitCode(exit_code, 0, stderr)
result_in_lower_case = ''.join(stderr).lower()
self.assertNotIn('foo=bar1', result_in_lower_case)
self.assertNotIn('foo=bar2', result_in_lower_case)
self.assertIn('foo=bar3', result_in_lower_case)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4015862

Please sign in to comment.