Repository: pantsbuild/pants

Issue:
Expose or enable Pex's `--resolve-local-platforms`.
Currently Pants exposes Pex's `--platform` through `pex_binary.platforms` but it does not set `--resolve-local-platforms` or expose a knob on `pex_binary` or `[pex-binary-defaults]` to enable this. As a result, situations where the `platform` is not foreign can fail to resolve when they need not.


Discussion:

Resolving patch:
diff --git a/src/python/pants/backend/python/goals/package_pex_binary.py b/src/python/pants/backend/python/goals/package_pex_binary.py
--- a/src/python/pants/backend/python/goals/package_pex_binary.py
+++ b/src/python/pants/backend/python/goals/package_pex_binary.py
@@ -20,6 +20,7 @@
 )
 from pants.backend.python.target_types import PexPlatformsField as PythonPlatformsField
 from pants.backend.python.target_types import (
+    PexResolveLocalPlatformsField,
     PexScriptField,
     PexShebangField,
     PexStripEnvField,
@@ -64,6 +65,7 @@ class PexBinaryFieldSet(PackageFieldSet, RunFieldSet):
     shebang: PexShebangField
     strip_env: PexStripEnvField
     platforms: PythonPlatformsField
+    resolve_local_platforms: PexResolveLocalPlatformsField
     layout: PexLayoutField
     execution_mode: PexExecutionModeField
     include_tools: PexIncludeToolsField
@@ -77,6 +79,8 @@ def generate_additional_args(self, pex_binary_defaults: PexBinaryDefaults) -> Tu
         args = []
         if self.emit_warnings.value_or_global_default(pex_binary_defaults) is False:
             args.append("--no-emit-warnings")
+        if self.resolve_local_platforms.value_or_global_default(pex_binary_defaults) is True:
+            args.append("--resolve-local-platforms")
         if self.ignore_errors.value is True:
             args.append("--ignore-errors")
         if self.inherit_path.value is not None:
diff --git a/src/python/pants/backend/python/target_types.py b/src/python/pants/backend/python/target_types.py
--- a/src/python/pants/backend/python/target_types.py
+++ b/src/python/pants/backend/python/target_types.py
@@ -179,11 +179,27 @@ def register_options(cls, register):
                 "`pex_binary` targets"
             ),
         )
+        register(
+            "--resolve-local-platforms",
+            advanced=True,
+            type=bool,
+            default=False,
+            help=(
+                "Whether to resolve a local interpreter that matches each of the "
+                f"`{PexPlatformsField.alias}` specified for a `{PexBinary.alias}` target.\n\n"
+                f"Can be overridden by specifying the `{PexResolveLocalPlatformsField.alias}` "
+                f"parameter of individual `{PexBinary.alias}` targets."
+            ),
+        )
 
     @property
     def emit_warnings(self) -> bool:
         return cast(bool, self.options.emit_warnings)
 
+    @property
+    def resolve_local_platforms(self) -> bool:
+        return cast(bool, self.options.resolve_local_platforms)
+
 
 # See `target_types_rules.py` for a dependency injection rule.
 class PexBinaryDependencies(Dependencies):
@@ -418,6 +434,22 @@ def value_or_global_default(self, pex_binary_defaults: PexBinaryDefaults) -> boo
         return self.value
 
 
+class PexResolveLocalPlatformsField(TriBoolField):
+    alias = "resolve_local_platforms"
+    help = (
+        f"For each of the `{PexPlatformsField.alias}` specified, attempt to find a local "
+        "interpreter that matches.\n\nIf a matching interpreter is found, use the interpreter to "
+        "resolve distributions and build any that are only available in source distribution form. "
+        "If no matching interpreter is found (or if this option is `False`), resolve for the "
+        "platform by accepting only pre-built binary distributions (wheels)."
+    )
+
+    def value_or_global_default(self, pex_binary_defaults: PexBinaryDefaults) -> bool:
+        if self.value is None:
+            return pex_binary_defaults.resolve_local_platforms
+        return self.value
+
+
 class PexExecutionMode(Enum):
     ZIPAPP = "zipapp"
     VENV = "venv"
@@ -491,6 +523,7 @@ class PexBinary(Target):
         PexEntryPointField,
         PexScriptField,
         PexPlatformsField,
+        PexResolveLocalPlatformsField,
         PexInheritPathField,
         PexStripEnvField,
         PexIgnoreErrorsField,


Test patch: