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

Add support for the pex --executable argument #20497

Merged
merged 15 commits into from
Feb 9, 2024
Merged

Conversation

cognifloyd
Copy link
Member

@cognifloyd cognifloyd commented Feb 6, 2024

This PR adds support for pex --executable <path to script> added in pex 2.1.93:
pex-tool/pex#1807

With this change, users should be able to do pants run bin/my-python-script.py.

Closes #18290

@cognifloyd cognifloyd added category:new feature backend: Python Python backend-related issues labels Feb 6, 2024
@cognifloyd cognifloyd self-assigned this Feb 6, 2024
@@ -213,6 +213,52 @@ def test_local_dist() -> None:
assert result.stdout == "LOCAL DIST\n"


def test_local_dist_with_executable_main() -> None:
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a copy of test_local_dist, but using an executable main instead of an entry point.

@cognifloyd cognifloyd marked this pull request as ready for review February 8, 2024 00:48
@cognifloyd cognifloyd changed the title plumb pex --executable arg Add support for the pex --executable argument Feb 8, 2024
Copy link
Sponsor Contributor

@benjyw benjyw left a comment

Choose a reason for hiding this comment

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

LG! Just a few nits...

@@ -67,7 +69,7 @@ async def _create_python_source_run_request(
include_source_files=False,
include_local_dists=True,
# `PEX_EXTRA_SYS_PATH` should contain this entry_point's module.
main=console_script or entry_point.val,
main=executable or console_script or entry_point.val,
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Why the different precedence here than above? Only one should be set, so it may not matter in practice, but it's good to be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

This function always gets an entry_point, whereas the others can be None. So we check the Nones first.

Oddly enough, nothing in pants actually passes a ConsoleScript to this function. This PR adds a heuristic that might pass an Executable.

So, would you prefer:

Suggested change
main=executable or console_script or entry_point.val,
main=console_script or executable or entry_point.val,

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

I guess it's fine the way it is, it just scans oddly.

src/python/pants/backend/python/util_rules/pex.py Outdated Show resolved Hide resolved
src/python/pants/backend/python/util_rules/pex.py Outdated Show resolved Hide resolved
src/python/pants/backend/python/target_types.py Outdated Show resolved Hide resolved
@@ -141,7 +143,7 @@ async def package_pex_binary(
request = PexFromTargetsRequest(
addresses=[field_set.address],
internal_only=False,
main=resolved_entry_point.val or field_set.script.value,
main=resolved_entry_point.val or field_set.script.value or field_set.executable.value,
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Re my question on precedence, does this always get an entry point? Can its .val be None? How is this case different than the one below?

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case, yes, as this is the general purpose case and can be anything.

In the other file, it is a utility function only used for the run goal, so it always has an entry point.

cognifloyd and others added 2 commits February 8, 2024 23:10
Co-authored-by: Benjy Weinberger <benjyw@gmail.com>
@cognifloyd cognifloyd merged commit 882ffe6 into main Feb 9, 2024
24 checks passed
@cognifloyd cognifloyd deleted the cognifloyd/pex-exe-arg branch February 9, 2024 16:20
if not all(part.isidentifier() for part in source_name.split(".")):
# If the python source is not importable (python modules can't be named with '-'),
# then it must be an executable script.
executable = Executable(self.source.value)
Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. self.source.value is relative to the BUILD file, but Executable needs to be relative to the workspace root, similar to the logic in PexExecutableField.

executable = Executable(os.path.join(self.address.spec_path, self.source.value).lstrip(os.path.sep))

Copy link
Member Author

Choose a reason for hiding this comment

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

The integration tests are only testing running a pex_binary, not python_source.
So, we need to add another parameter to this test to use app.py or my-app.py so it uses the Executable main:
https://github.com/pantsbuild/pants/blob/main/src/python/pants/backend/python/goals/run_python_source_integration_test.py#L114-L137

cognifloyd added a commit that referenced this pull request Jun 18, 2024
This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 18, 2024
This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 18, 2024
This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 18, 2024
This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 18, 2024
This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 19, 2024
…) (#21084)

This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 19, 2024
…) (#21085)

This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
cognifloyd added a commit that referenced this pull request Jun 19, 2024
…) (#21086)

This fixes the feature added in #20497 as it broke using `pants run` on
a python_source if the file name has `-` or other invalid characters.
Bug reported here:

https://pantsbuild.slack.com/archives/C046T6T9U/p1717624913138789?thread_ts=1717624913.138789&cid=C046T6T9U

The other integration test for the pex --executable feature only tested
running pex_binary, not python_source. So, I missed applying some of the
path logic to both cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Python Python backend-related issues category:new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for the pex --executable argument
2 participants