Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure non-ambiguous args/env vars injection into PEXes #18861

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_layout(rule_runner: PythonRuleRunner, layout: PexLayout) -> None:
import os
import sys
print(f"FOO={os.environ.get('FOO')}")
print(f"BAR={os.environ.get('BAR')}")
print(f"--inject-arg={os.environ.get('--inject-arg')}")
print(f"ARGV={sys.argv[1:]}")
"""
),
Expand All @@ -190,8 +190,8 @@ def test_layout(rule_runner: PythonRuleRunner, layout: PexLayout) -> None:
python_sources(name="lib")
pex_binary(
entry_point="app.py",
args=['123', 'abc'],
env={{'FOO': 'xxx', 'BAR': 'yyy'}},
args=['123', 'abc', '--inject-env'],
env={{'FOO': 'xxx', '--inject-arg': 'yyy'}},
layout="{layout.value}",
)
"""
Expand All @@ -215,8 +215,8 @@ def test_layout(rule_runner: PythonRuleRunner, layout: PexLayout) -> None:
stdout = dedent(
"""\
FOO=xxx
BAR=yyy
ARGV=['123', 'abc']
--inject-arg=yyy
ARGV=['123', 'abc', '--inject-env']
"""
).encode()
assert stdout == subprocess.run([executable], check=True, stdout=subprocess.PIPE).stdout
Expand Down
6 changes: 2 additions & 4 deletions src/python/pants/backend/python/util_rules/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,8 @@ async def build_pex(
if request.main is not None:
argv.extend(request.main.iter_pex_args())

for injected_arg in request.inject_args:
argv.extend(["--inject-args", str(injected_arg)])
for k, v in sorted(request.inject_env.items()):
argv.extend(["--inject-env", f"{k}={v}"])
argv.extend(f"--inject-args={injected_arg}" for injected_arg in request.inject_args)
argv.extend(f"--inject-env={k}={v}" for k, v in sorted(request.inject_env.items()))

# TODO(John Sirois): Right now any request requirements will shadow corresponding pex path
# requirements, which could lead to problems. Support shading python binaries.
Expand Down