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

Relocate environment variable injection to before the interpreter is run #2260

Merged
merged 10 commits into from
Nov 7, 2023

Conversation

erikwilliamson
Copy link
Contributor

As per #2224

@jsirois
Copy link
Member

jsirois commented Oct 2, 2023

@erikwilliamson thanks for working on this. Please add a test to prove this works. You likely can just pile in the original PR test: https://github.com/pantsbuild/pex/pull/1948/files#diff-2b86577053b9bb459d54416c397bf7d1fa598becf7a5cc9e856290fb0427881e

You can then tox -eformat-run,typecheck to run black + mypy and tox -epyXY-integration -- -ktest_inject_env_and_args to run your tests via pytest. Here XY is the Python major minor of a Python you have installed, for example tox -epy311-integration. You can use ./dtox instead of tox with the rest of the command line being the same to run the tests in a docker environment that has all the Pythons Pex supports pre-installed. The 1st time you run, using BASE_MODE=pull ./dtox will likely speed things up by pulling the base docker image instead of building it.

@ghost
Copy link

ghost commented Oct 16, 2023

@jsirois I'm sorry that this is taking me so long. I'm having trouble getting any tests to run successfully, even from the main branch. I'm totally new to Tox, too which isn't speeding things up.

I'm getting the following when running dtox.sh:

erik@mini pex % BASE_MODE=pull ./dtox.sh -epy311-integration
1ff12c11995a5b21b7b2774ccc974664997c955b: Pulling from pantsbuild/pex/base
... truncated the output ...
#6 0.182 + addgroup --gid=20 staff
#6 0.216 addgroup: The group `staff' already exists.

And while I know what this means, I'm not sure what the appropriate fix is - should I create a separate user and a different group for it to be in on my dev machine?

@jsirois
Copy link
Member

jsirois commented Oct 17, 2023

This is the script that's failing: https://github.com/pantsbuild/pex/blob/main/docker/user/create_docker_image_user.sh

It runs in the context of building a docker image via: https://github.com/pantsbuild/pex/blob/a43c48c2a4b18b352be2c3175e0ae363ff8876d7/docker/user/Dockerfile#L4-L11

And those Dockerfile ARGs are fed here: https://github.com/pantsbuild/pex/blob/a43c48c2a4b18b352be2c3175e0ae363ff8876d7/dtox.sh#L41-L51

So, if you want to mess around and get that working for you, have at it.

That said, using dtox.sh is just intended to make development easier. It's giving you troubles though; so perhaps just install tox on your machine however you normally install Python tools and run tox -e... instead of ./dtox.sh -e....

I'll continue to be out of country and AFK through November 2nd. Hopefully you've found success by then, but, if not, I'll check back in.

@jsirois
Copy link
Member

jsirois commented Nov 6, 2023

@erikwilliamson I wanted to check in and make sure you had what you needed to run tox -epyXY-integration -- -ktest_inject_env_and_args (#2260 (comment)). No rush of course, but without being able to run that it might be hard to work on adding a test.

@erikwilliamson
Copy link
Contributor Author

Hey, I tried to join the Pants/Pex Slack but after filling out the questionnaire it's saying that the link has expired - I wondered if Slack might be a good place for me to ask a few questions rather than here. I'm able to run the tests no problem; however knowing whether the failures are valid now or if the tests need to be altered is perplexing :-)

@jsirois
Copy link
Member

jsirois commented Nov 6, 2023

I'm not a part of the Pants slack. Since you now can run tests, if you can run tox -epyXY-integration -- -k your_new_test_name, even if that fails for perplexing reasons, it's probably time to add the test to this PR. Once you do that I'll approve the CI workflow and we can have a shared failure experience to talk about.

@erikwilliamson
Copy link
Contributor Author

Learning as I go here! I saw that that there's pex/venv/installer.py which had environment variable injection happening. Tests are passing for me now - Thanks for your patience and I look forward to your thoughts.

Copy link
Member

@jsirois jsirois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikwilliamson this LGTM mod commented out tests. I'll let CI fly, but please circle back to get those un-commented / updated to work with this change.

@jsirois
Copy link
Member

jsirois commented Nov 6, 2023

For the formatting shard, tox -eformat-run,typecheck is what you want to pass locally.

@erikwilliamson
Copy link
Contributor Author

I'm getting this on OSX 14.1 - I can fire up a linux VM to make it go away in the meantime though

Typechecking 338 files using Python 3.11.6 against Python 3.12 ...
pex/compatibility.py: note: In function "cpu_count":
pex/compatibility.py:163:23: error: Module has no attribute
"sched_getaffinity" [attr-defined]
cpu_set = os.sched_getaffinity(0)

@jsirois
Copy link
Member

jsirois commented Nov 6, 2023

@erikwilliamson if that's the only error, you should be good to go since it was only the format check that failed in CI (https://github.com/pantsbuild/pex/actions/runs/6776741887/job/18420675544?pr=2260#step:4:160) and not the type check.

... and I <3 macOS.

@kaos
Copy link
Collaborator

kaos commented Nov 6, 2023

... and I <3 macOS.

soon I'll believe you 😂

Copy link
Member

@jsirois jsirois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @erikwilliamson, this looks good with two small fixes.

assert {"args": ["foo", "bar"], "MESSAGE": None} == json.loads(
# Switching away from the built-in entrypoint should disable injected args but not the en env.
assert {"args": ["foo", "bar"], "MESSAGE": "Hello, world!"} == json.loads(
# assert {"MESSAGE": "Hello, world!"} == json.loads(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill this commented out line.

@@ -240,8 +240,9 @@ def assert_message(
assert_message(b"Hello, world!")
assert_message(b"42", MESSAGE="42")

# Switching away from the built-in entrypoint should disable injected args and env.
assert {"args": ["foo", "bar"], "MESSAGE": None} == json.loads(
# Switching away from the built-in entrypoint should disable injected args but not the en env.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo at the end of the sentence. Should be: ... not the env.

@jsirois jsirois mentioned this pull request Nov 7, 2023
2 tasks
pex/venv/installer.py Outdated Show resolved Hide resolved
@jsirois jsirois merged commit 4089022 into pex-tool:main Nov 7, 2023
24 checks passed
@jsirois
Copy link
Member

jsirois commented Nov 7, 2023

Alright @erikwilliamson this is now available in Pex 2.1.151:

Thanks for your contribution.

@erikwilliamson
Copy link
Contributor Author

Thanks so much for guiding me, @jsirois ! Your help and patience were greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants