diff --git a/src/python/pants/backend/go/util_rules/go_bootstrap.py b/src/python/pants/backend/go/util_rules/go_bootstrap.py index 0d651e169a2..6df516f3db2 100644 --- a/src/python/pants/backend/go/util_rules/go_bootstrap.py +++ b/src/python/pants/backend/go/util_rules/go_bootstrap.py @@ -10,10 +10,11 @@ from pants.backend.go.subsystems.golang import GolangSubsystem from pants.core.util_rules import asdf from pants.core.util_rules.asdf import AsdfToolPathsRequest, AsdfToolPathsResult -from pants.core.util_rules.environments import EnvironmentTarget +from pants.core.util_rules.environments import EnvironmentTarget, LocalEnvironmentTarget from pants.engine.env_vars import EnvironmentVars, EnvironmentVarsRequest from pants.engine.internals.selectors import Get from pants.engine.rules import collect_rules, rule, rule_helper +from pants.util.strutil import softwrap logger = logging.getLogger(__name__) @@ -77,10 +78,47 @@ async def _environment_paths() -> list[str]: return [] +def _error_if_not_compatible_with_asdf( + env_tgt: EnvironmentTarget, + _search_paths: Iterable[str], +) -> None: + """Raises an exception if any special search path strings any are invalid for the environment. + + If the environment is non-local and there are invalid tokens for those environments, raise + `ValueError`. + """ + + env = env_tgt.val + + if env is None or isinstance(env, LocalEnvironmentTarget): + return + + not_allowed = {"", ""} + + any_not_allowed = set(_search_paths) & not_allowed + if any_not_allowed: + env_type = type(env) + raise ValueError( + softwrap( + f"`[python-bootstrap].search_paths` is configured to use local Go discovery " + f"tools, which do not work in {env_type.__name__} runtime environments. To fix " + f"this, set the value of `golang_go_search_paths` in the `{env.alias}` " + f"defined at `{env.address}` to contain only hardcoded paths or the `` " + "special string." + ) + ) + + return + + @rule async def resolve_go_bootstrap( golang_subsystem: GolangSubsystem, golang_env_aware: GolangSubsystem.EnvironmentAware ) -> GoBootstrap: + + _error_if_not_compatible_with_asdf( + golang_env_aware.env_tgt, golang_env_aware.raw_go_search_paths + ) paths = await _go_search_paths( golang_env_aware.env_tgt, golang_subsystem, golang_env_aware.raw_go_search_paths )