modern-smartphone-82928
02/22/2024, 2:23 AMfresh-mechanic-68429
02/22/2024, 3:09 AMEnvironmentVarsRequest
https://github.com/pantsbuild/pants/blob/62828b7f56abeeea79b9249b8d97ae7d78847785/src/python/pants/backend/go/util_rules/cgo.py#L390modern-smartphone-82928
02/22/2024, 3:27 AMEnvironmentVarsRequest
With the example seen here: https://www.pantsbuild.org/2.19/docs/writing-plugins/the-rules-api/processes#environment-variables
TOKEN=asdad pants package ::
The token is used to dynamically create synthetic targets.modern-smartphone-82928
02/22/2024, 9:03 AMtoken = await Get(EnvironmentVars, EnvironmentVarsRequest(["TOKEN"]))
20:01:50.78 [ERROR] Encountered 14 rule graph errors:
No installed rules return the type EnvironmentNameRequest, and it was not provided by potential callers of @rule(pants.core.util_rules.environments:702:resolve_environment_name(EnvironmentNameRequest, EnvironmentsSubsystem, GlobalOptions) -> EnvironmentName, gets=[ChosenLocalEnvironmentName, Get(EnvironmentTarget, [EnvironmentName]), Get(EnvironmentName, [EnvironmentNameRequest])]).
If that type should be computed by a rule, ensure that that rule is installed.
If it should be provided by a caller, ensure that it is included in any relevant Query or Get.
Doing something wrong here?curved-television-6568
02/22/2024, 9:26 AM@union(in_scope_types=[EnvironmentName])
like so: https://github.com/pantsbuild/pants/blob/42ff1dd86a7c3b370a2802df272d27ff0bb42ce3/src/python/pants/engine/target.py#L1122curved-television-6568
02/22/2024, 9:28 AMmodern-smartphone-82928
02/22/2024, 9:43 AMimport requests
from typing import Iterable
from dataclasses import dataclass
from pants.engine.internals.synthetic_targets import (
SyntheticTargetsRequest,
SyntheticAddressMaps
)
from pants.engine.internals.target_adaptor import TargetAdaptor
from pants.engine.unions import UnionRule, union
from pants.engine.environment import EnvironmentName
from pants.engine.env_vars import EnvironmentVars, EnvironmentVarsRequest
from pants.engine.rules import collect_rules, Get, rule
from pants.util.logging import LogLevel
@union(in_scope_types=[EnvironmentName])
@dataclass(frozen=True)
class SynthenticFooRequest(SyntheticTargetsRequest):
path: str = SyntheticTargetsRequest.SINGLE_REQUEST_FOR_ALL_TARGETS
pass
def generate_some_targets(versions: Iterable[str]) -> Iterable[TargetAdaptor]:
"""_summary_
Args:
versions (Iterable[str]): versions eg, a.b.c
Returns:
Iterable[TargetAdaptor]: Iterable pants targets
"""
target_adapters = []
for index, version in enumerate(versions):
adapter = TargetAdaptor(
"docker_image", name=f"test_{index}",
__description_of_origin__= "some origin",
source = "Dockerfile",
dependencies = [
":some-other-docker",
],
tags = [
"some-test"
],
extra_build_args=[f"BASE_IMAGE=some/image:{version}"]
)
target_adapters.append(adapter)
return target_adapters
def get_ids_from_url(token: str) -> list[str]:
url = "https:/some-url/"
headers = {"TOKEN": token}
ids = requests.get(url=url, headers=headers, timeout=30).json()
return ids
async def get_ids() -> list[str]:
envs = await Get(EnvironmentVars, EnvironmentVarsRequest(["TOKEN"]))
token = envs.get("TOKEN")
ids = get_ids_from_url(token)
return ids
@rule(level=LogLevel.DEBUG)
async def synthetic_targets(request: SynthenticFooRequest) -> SyntheticAddressMaps:
iids = get_ids()
return SyntheticAddressMaps.for_targets_request(
request,
[
(
# Address
"somePath//BUILD.synthetic-targets",
(
generate_some_targets(iids)
),
),
]
)
def rules():
return (
*collect_rules(),
UnionRule(SyntheticTargetsRequest, SynthenticFooRequest),
)
@curved-television-6568 Thanks for helping out.modern-smartphone-82928
02/22/2024, 11:24 AMcurved-television-6568
02/22/2024, 12:03 PMSessionValues
directly, indexing that using CompleteEnvironmentVars
as here: https://github.com/pantsbuild/pants/blob/42ff1dd86a7c3b370a2802df272d27ff0bb42ce3/src/python/pants/engine/internals/platform_rules.py#L46-L72
This is on the edge of being hacky though.. as I don't think we'd consider this part of the PluginAPI, so it might break unexpectedly between pants releases if it is changed for some reason.. just beware 😉curved-television-6568
02/22/2024, 12:04 PMdef rules():
return (
*collect_rules(),
*SynthenticFooRequest.rules(),
)
curved-television-6568
02/22/2024, 12:05 PMmodern-smartphone-82928
02/22/2024, 9:00 PM