Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure non-ambiguous args/env vars injection into PEXes (pantsbuild#1…
…8861) This fixes pantsbuild#18779 by ensuring that injecting arguments and environment variables into a pex doesn't risk them being interpreted by the pex CLI itself if they start with `-`, and ensuring they're quoted appropriately to match the `shlex.split` within pex (https://github.com/pantsbuild/pex/blob/ff220b9e41484450e0c78136f98d6899f2e2325c/pex/bin/pex.py#L433). The issue noted in pantsbuild#18779 was specifically ambiguity with `args`, but the same problem will apply to `env` and so that is fixed here too (although it's rather less common to have env vars starting with `-`). NB. `shlex.quote`ing alone won't handle the `-` ambiguity, `shlex.quote("--foo") == "--foo"`. For a target like `pex_binary(..., args=["--foo"], env={"--bar": "baz"})`: - Previously, this would invoke `pex ... --inject-args --foo --inject-env --bar=baz`, and argparse within the pex CLI will intepret `--foo` and `--bar=...` as independent parameters, rather than arguments to `--inject-args`/`--inject-env`. - After this change (as suggested by @jsirois), the invocation will be `pex ... --inject-args=--foo --inject-env=--bar=baz` and there's no risk of misinterpretation. For a target like `pex_binary(..., args=["spaces 'n' quotes"])`: - Previously this would invoke with (equivalently) an arg `--inject-args=spaces 'n' quotes`, which would `shlex.split` to three arguments in the PEX, the same as passing `args=["spaces", "n", "quotes"])` - Now, this will invoke with `--inject-args='spaces '"'"'n'"'"' quotes'`, which, is correctly `shlex.split` to the single value: `spaces 'n' quotes`.
- Loading branch information