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 that the autodetected python runtimes are marked as compatible #20356

Closed
wants to merge 1 commit into from

Conversation

katre
Copy link
Member

@katre katre commented Nov 29, 2023

with the host platform.

This should allow other python runtimes to be registered and used with bzlmod.

Part of changes for #20354.

with the host platform.

This should prevent them from being used inappropriately in remote
execution.

Part of changes for bazelbuild#20354.
@katre katre requested review from lberki and rickeylev November 29, 2023 13:19
@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Nov 29, 2023
@lberki
Copy link
Contributor

lberki commented Nov 29, 2023

This doesn't seem to fix #20354 , should it?

@katre
Copy link
Member Author

katre commented Nov 29, 2023

It addresses the smaller part brought up there, which was the lack of constraints. The prioritization of toolchains is still a problem.

@@ -242,11 +243,13 @@ def define_autodetecting_toolchain(
toolchain = ":_autodetecting_py_runtime_pair",
toolchain_type = ":toolchain_type",
visibility = ["//visibility:public"],
exec_compatible_with = HOST_CONSTRAINTS,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this correct? I would have expected a target_compatible_with constraints, similar to java_runtimes.

Copy link
Contributor

Choose a reason for hiding this comment

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

The auto detecting toolchain is a bit funny because it basically just takes python from PATH. If you unroll the generated code, it basically does this:

py_runtime(
   interpreter = "foo.sh"
)
# foo.sh
py=$(which python)
exec $py $@

So "compatible with every platform" (for both target and exec) is technically true. Usage of this toolchain basically means you're just assuming whatever system it runs on provides python. I'm not saying doing this is a good idea, its just what is historically there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, then the "no constraint" toolchain seems actually correct. The "autodetecting_*" bit threw me off as I thought the detection would apply at repository rule time.

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy to close this, then.

Copy link
Contributor

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

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

I'm not entirely convinced any constraints should be set.

The auto detecting toolchain is a bit funny because it basically just takes python from PATH. If you unroll the generated code, it basically does this:

py_runtime(
   interpreter = "foo.sh"
)
# foo.sh
py=$(which python)
exec $py $@

Stated another way: the python interpreter to use is selected at runtime, not build time. The local system isn't e.g. copying its binary over to run on a remote system.

So "compatible with every platform" (for both target and exec) is technically true. Usage of this toolchain basically means you're just assuming whatever system it runs on provides python. I'm not saying doing this is a good idea, its just what is historically there.

Also, fwiw, I've been trying to get rid of this toolchain and the auto-registration of it, but that got hung up on various test failures.

@@ -14,6 +14,7 @@

"""Definitions related to the Python toolchain."""

load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
Copy link
Contributor

Choose a reason for hiding this comment

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

Whoa, what is this magic? What else is in this local_config_platform? This seems useful, though I'm not sure how yet...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just this, but it is very useful for e.g. marking rules that should always resolve toolchains for the host via exec_compatible_with. We use this trick in rules_go to make bazel run @rules_go//go always run the correct go tool even with --platforms.

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 magic is in LocalConfigPlatformFunction, but it's really easy (and could be fairly trivially migrated to a Starlark repo rule in the builtins, I bet).

The repo creates the following files:

  • WORKSPACE and MODULE.bazel: Mark the repository (and depend on platforms 0.7.0).
  • BUILD.bazel: Declares the host platform, using the HOST_CONSTRAINTS from constraints.bzl
  • constraints.bzl: Creates the HOST_CONSTRAINTS, based on whatever Bazel autodetects as the host environment.

@@ -242,11 +243,13 @@ def define_autodetecting_toolchain(
toolchain = ":_autodetecting_py_runtime_pair",
toolchain_type = ":toolchain_type",
visibility = ["//visibility:public"],
exec_compatible_with = HOST_CONSTRAINTS,
Copy link
Contributor

Choose a reason for hiding this comment

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

The auto detecting toolchain is a bit funny because it basically just takes python from PATH. If you unroll the generated code, it basically does this:

py_runtime(
   interpreter = "foo.sh"
)
# foo.sh
py=$(which python)
exec $py $@

So "compatible with every platform" (for both target and exec) is technically true. Usage of this toolchain basically means you're just assuming whatever system it runs on provides python. I'm not saying doing this is a good idea, its just what is historically there.

@katre
Copy link
Member Author

katre commented Nov 29, 2023

Closing this as unneeded.

@katre katre closed this Nov 29, 2023
@katre katre deleted the autopython-constraints branch November 29, 2023 17:25
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Nov 29, 2023
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.

4 participants