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

Mask the environment name in graph and specs_rules #17179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/python/pants/backend/explorer/graphql/query/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pants.backend.explorer.graphql.setup import create_schema
from pants.backend.explorer.rules import validate_explorer_dependencies
from pants.backend.project_info import peek
from pants.engine.environment import EnvironmentName
from pants.engine.explorer import RequestState
from pants.engine.target import RegisteredTargetTypes
from pants.engine.unions import UnionMembership
Expand All @@ -28,7 +29,10 @@ def schema():
def context(all_help_info: AllHelpInfo, rule_runner: RuleRunner) -> dict:
return dict(
pants_request_state=RequestState(
all_help_info, rule_runner.build_config, rule_runner.scheduler
all_help_info,
rule_runner.build_config,
rule_runner.scheduler,
env_name=EnvironmentName(None),
)
)

Expand Down
5 changes: 4 additions & 1 deletion src/python/pants/bsp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from pants.bsp.context import BSPContext
from pants.bsp.spec.notification import BSPNotification
from pants.core.util_rules.environments import determine_bootstrap_environment
from pants.engine.environment import EnvironmentName
from pants.engine.fs import Workspace
from pants.engine.internals.scheduler import SchedulerSession
Expand Down Expand Up @@ -83,6 +84,8 @@ def __init__(
max_workers: int = 5,
) -> None:
self._scheduler_session = scheduler_session
# TODO: We might eventually want to make this configurable.
self._env_name = determine_bootstrap_environment(self._scheduler_session)
self._inbound = JsonRpcStreamReader(inbound)
self._outbound = JsonRpcStreamWriter(outbound)
self._context: BSPContext = context
Expand Down Expand Up @@ -154,7 +157,7 @@ def _handle_inbound_message(self, *, method_name: str, params: Any):
self._scheduler_session.new_run_id()

workspace = Workspace(self._scheduler_session)
params = Params(request, workspace)
params = Params(request, workspace, self._env_name)
execution_request = self._scheduler_session.execution_request(
requests=[(method_mapping.response_type, params)],
)
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/build_graph/build_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def register_rules(self, plugin_or_backend: str, rules: Iterable[Rule | UnionRul

# "Index" the rules to normalize them and expand their dependencies.
rule_index = RuleIndex.create(rules)
rules_and_queries = (*rule_index.rules, *rule_index.queries)
rules_and_queries: tuple[Rule, ...] = (*rule_index.rules, *rule_index.queries)
for rule in rules_and_queries:
self._rule_to_providers[rule].append(plugin_or_backend)
for union_rule in rule_index.union_rules:
Expand Down
19 changes: 6 additions & 13 deletions src/python/pants/core/util_rules/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from pants.build_graph.address import Address, AddressInput
from pants.engine.engine_aware import EngineAwareParameter
from pants.engine.environment import LOCAL_ENVIRONMENT_MATCHER as LOCAL_ENVIRONMENT_MATCHER
from pants.engine.environment import ChosenLocalEnvironmentName as ChosenLocalEnvironmentName
from pants.engine.environment import EnvironmentName as EnvironmentName
from pants.engine.internals.docker import DockerResolveImageRequest, DockerResolveImageResult
from pants.engine.internals.graph import WrappedTargetForBootstrap
Expand Down Expand Up @@ -76,8 +78,6 @@ class EnvironmentsSubsystem(Subsystem):
# Environment targets
# -------------------------------------------------------------------------------------------

LOCAL_ENVIRONMENT_MATCHER = "__local__"


class EnvironmentField(StringField):
alias = "environment"
Expand Down Expand Up @@ -347,7 +347,7 @@ def determine_bootstrap_environment(session: SchedulerSession) -> EnvironmentNam
ChosenLocalEnvironmentName,
session.product_request(ChosenLocalEnvironmentName, [Params()])[0],
)
return EnvironmentName(local_env.val)
return local_env.val


class AmbiguousEnvironmentError(Exception):
Expand All @@ -366,13 +366,6 @@ class AllEnvironmentTargets(FrozenDict[str, Target]):
"""A mapping of environment names to their corresponding environment target."""


@dataclass(frozen=True)
class ChosenLocalEnvironmentName:
"""Which environment name from `[environments-preview].names` that __local__ resolves to."""

val: str | None


@dataclass(frozen=True)
class EnvironmentTarget:
val: Target | None
Expand Down Expand Up @@ -452,11 +445,11 @@ async def determine_local_environment(
]
if not compatible_name_and_targets:
# That is, use the values from the options system instead, rather than from fields.
return ChosenLocalEnvironmentName(None)
return ChosenLocalEnvironmentName(EnvironmentName(None))

if len(compatible_name_and_targets) == 1:
result_name, _tgt = compatible_name_and_targets[0]
return ChosenLocalEnvironmentName(result_name)
return ChosenLocalEnvironmentName(EnvironmentName(result_name))

raise AmbiguousEnvironmentError(
softwrap(
Expand Down Expand Up @@ -517,7 +510,7 @@ async def resolve_environment_name(
) -> EnvironmentName:
if request.raw_value == LOCAL_ENVIRONMENT_MATCHER:
local_env_name = await Get(ChosenLocalEnvironmentName, {})
return EnvironmentName(local_env_name.val)
return local_env_name.val
if request.raw_value not in environments_subsystem.names:
raise UnrecognizedEnvironmentError(
softwrap(
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/core/util_rules/environments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def rule_runner() -> RuleRunner:
QueryRule(EnvironmentName, [EnvironmentNameRequest]),
],
target_types=[LocalEnvironmentTarget, DockerEnvironmentTarget, RemoteEnvironmentTarget],
singleton_environment=None,
inherent_environment=None,
)


Expand Down Expand Up @@ -192,7 +192,7 @@ def test_choose_local_environment(rule_runner: RuleRunner) -> None:

def get_env() -> EnvironmentTarget:
name = rule_runner.request(ChosenLocalEnvironmentName, [])
return rule_runner.request(EnvironmentTarget, [EnvironmentName(name.val)])
return rule_runner.request(EnvironmentTarget, [name.val])

# If `--names` is not set, do not choose an environment.
assert get_env().val is None
Expand Down Expand Up @@ -284,7 +284,7 @@ def get_env_name(
MockGet(
output_type=ChosenLocalEnvironmentName,
input_types=(),
mock=lambda: ChosenLocalEnvironmentName(None),
mock=lambda: ChosenLocalEnvironmentName(EnvironmentName(None)),
),
MockGet(
output_type=EnvironmentTarget,
Expand Down
9 changes: 9 additions & 0 deletions src/python/pants/engine/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pants.engine.engine_aware import EngineAwareParameter
from pants.engine.env_vars import CompleteEnvironmentVars, EnvironmentVars, EnvironmentVarsRequest

LOCAL_ENVIRONMENT_MATCHER = "__local__"


@dataclass(frozen=True)
class EnvironmentName(EngineAwareParameter):
Expand All @@ -25,6 +27,13 @@ def debug_hint(self) -> str | None:
return f"environment:{self.val}" if self.val else None


@dataclass(frozen=True)
class ChosenLocalEnvironmentName:
"""Which environment name from `[environments-preview].names` that __local__ resolves to."""

val: EnvironmentName


def __getattr__(name):
if name == "EnvironmentName":
return EnvironmentName
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/engine/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class RequestState:
all_help_info: AllHelpInfo
build_configuration: BuildConfiguration
scheduler_session: SchedulerSession
env_name: EnvironmentName

def product_request(
self,
Expand All @@ -60,7 +61,7 @@ def product_request(
) -> T:
result = self.scheduler_session.product_request(
product,
[Params(*subjects)],
[Params(*subjects, self.env_name)],
poll=poll,
timeout=timeout,
)
Expand Down
Loading