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

Pants plugins: pack_metadata/python_imports #6260

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

cognifloyd
Copy link
Member

This PR has commits extracted from #6202 where I'm working on getting all of our unit tests to run under pants+pytest.

Overview

This has pants+pytest run pack tests directly instead of using st2-run-pack-tests, which has some issues detailed below.

This enhances pants-plugins/pack_metadata so that it can inject the custom PYTHONPATH entries that st2 adds when running python in packs like actions and sensors. We have several special lib directories to support:

  • <pack>/lib is the shared lib dir that gets added for both actions and sensors when [packs].enable_common_libs = True in st2.conf.
  • <pack>/actions/lib for actions (allowing import foo to import <pack>/actions/lib/foo.py in an action)
  • The entry_point parent directory for sensors and actions

That last point is actually not handled correctly in st2-run-pack-tests which only adds <pack>/actions/ and <pack>/sensors/ to PYTHONPATH, which assumes that the python file is directly in those directories. The python files can, however be in a sub directory, and that sub directory gets added to PYTHONPATH. So, this pants-plugins/pack_metadata enhancement allows pants to accurately reflect the logic st2 actually uses.

Another reason that this does not use st2-run-pack-tests, is that it obscures the PYTHONPATH logic that pants needs to carefully manage. Pants uses PEX to make sure that tests are run in a hermetic sandbox that cannot be influenced by things like packages installed with pip install --user. So, the python dependencies detected by this plugin actually get passed as PEX_EXTRA_SYS_PATH not PYTHONPATH which is ignored when running python hermetically.

Eventually, I would like to publish pants-plugins/pack_metadata so it can be used in pack repos to run tests with pants+pytest instead of st2-run-pack-tests.

TODO expand your PR description with a guided tour of the plugin components.

Now pack_metadata targets will generate pack_content_resource instead of just resource.
pack_content_resource is still a resource, but this setup allows us to find
the generated resource targets more simply.

This also harmonizes the implementation of pack_metadata to follow the fields
definition of resources (esp moving dependencies into moved_fields instead of core_fields).
…esource targets

This will allow rules to look up just action and sensor metadata (for example).
… module mapping

Only handles the actual action/sensor python files. It does not yet handle:
- <pack>/lib
- <pack>/actions/lib
…thon module mapping

This makes dependency inference aware of these which may be on the PYTHONPATH.
- <pack>/lib
- <pack>/actions/lib
The pack_metadata plugin now handles identifying these imports for dep inference.
Next step, modify the PYTHONPATH as well.
…TRA_SYS_PATH for tests

This won't work until pants gets support for injecting path entries.
…xture

Writes various test scenarios in the test sandbox.
This should avoid repeating the setup in every test.
…ules

And fix the identified mistake in conftest.
@pull-request-size pull-request-size bot added the size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. label Oct 5, 2024
@cognifloyd cognifloyd added this to the pants milestone Oct 5, 2024
@cognifloyd cognifloyd self-assigned this Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pantsbuild python3 size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant