Skip to content

Commit

Permalink
Pass env vars through in ./pants run for python (#4606)
Browse files Browse the repository at this point in the history
### Problem

As explained in #4590, environment variables are not passed to the pex-invoked subprocess during `./pants run` in the python backend. That is a behaviour change from `python_old`.

### Solution

Copy and propagate the environment into `pex.run`.

### Result

Environment variables are passed through to the subprocess during `./pants run`. Fixes #4590.
  • Loading branch information
Stu Hood committed May 18, 2017
1 parent a49a1b2 commit 78be2f6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/python/pants/backend/python/tasks2/python_run.py
Expand Up @@ -5,6 +5,7 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import os
import signal

from pants.backend.python.targets.python_binary import PythonBinary
Expand Down Expand Up @@ -40,7 +41,7 @@ def execute(self):
for arg in self.get_options().args:
args.extend(safe_shlex_split(arg))
args += self.get_passthru_args()
po = pex.run(blocking=False, args=args)
po = pex.run(blocking=False, args=args, env=os.environ.copy())
try:
result = po.wait()
if result != 0:
Expand Down
4 changes: 4 additions & 0 deletions testprojects/src/python/print_env/BUILD
@@ -0,0 +1,4 @@
python_binary(
entry_point = 'print_env.main',
source = 'main.py',
)
14 changes: 14 additions & 0 deletions testprojects/src/python/print_env/main.py
@@ -0,0 +1,14 @@
# coding=utf-8
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import os
import sys


if __name__ == '__main__':
# Print the content of the env var given on the command line.
print(os.environ[sys.argv[1]])
Expand Up @@ -43,6 +43,18 @@ def test_die(self):
pants_run = self.run_pants(command=command)
assert pants_run.returncode == 57

def test_get_env_var(self):
var_key = 'SOME_MAGICAL_VAR'
var_val = 'a value'
command = ['-q',
'run',
'testprojects/src/python/print_env',
'--',
var_key]
pants_run = self.run_pants(command=command, extra_env={var_key: var_val})
self.assert_success(pants_run)
self.assertEquals(var_val, pants_run.stdout_data.strip())

def _maybe_run_version(self, version):
if self.has_python_version(version):
print('Found python {}. Testing running on it.'.format(version))
Expand Down

0 comments on commit 78be2f6

Please sign in to comment.