Skip to content

Commit

Permalink
BUILD file symbols help info (pantsbuild#18378)
Browse files Browse the repository at this point in the history
Adds `symbols` as a new help topic for BUILD file symbols documentation.

Supports both builtin symbols as well as macros defined in prelude files.

<img width="773" alt="image"
src="https://user-images.githubusercontent.com/72965/222631240-95285468-28b2-4b31-a91a-32bd48884beb.png">
  • Loading branch information
kaos authored Mar 3, 2023
1 parent 48af135 commit dd2eb85
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 91 deletions.
11 changes: 10 additions & 1 deletion src/python/pants/build_graph/build_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@

# No goal or target_type can have a name from this set, so that `./pants help <name>`
# is unambiguous.
_RESERVED_NAMES = {"api-types", "backends", "global", "goals", "subsystems", "targets", "tools"}
_RESERVED_NAMES = {
"api-types",
"backends",
"global",
"goals",
"subsystems",
"symbols",
"targets",
"tools",
}


# Subsystems used outside of any rule.
Expand Down
29 changes: 25 additions & 4 deletions src/python/pants/engine/internals/build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
MaybeBuildFileDependencyRulesImplementation,
)
from pants.engine.internals.mapper import AddressFamily, AddressMap
from pants.engine.internals.parser import BuildFilePreludeSymbols, Parser, error_on_imports
from pants.engine.internals.parser import (
BuildFilePreludeSymbols,
BuildFileSymbolsInfo,
Parser,
error_on_imports,
)
from pants.engine.internals.session import SessionValues
from pants.engine.internals.synthetic_targets import (
SyntheticAddressMaps,
SyntheticAddressMapsRequest,
)
from pants.engine.internals.target_adaptor import TargetAdaptor, TargetAdaptorRequest
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.rules import Get, MultiGet, QueryRule, collect_rules, rule
from pants.engine.target import (
DependenciesRuleApplication,
DependenciesRuleApplicationRequest,
Expand Down Expand Up @@ -89,7 +94,8 @@ async def evaluate_preludes(
globals: dict[str, Any] = {
**{name: getattr(builtins, name) for name in dir(builtins) if name.endswith("Error")},
# Ensure the globals for each prelude includes the builtin symbols (E.g. `python_sources`)
**parser.builtin_symbols,
# and any build file aliases (e.g. from plugins)
**parser.symbols,
}
locals: dict[str, Any] = {}
env_vars: set[str] = set()
Expand All @@ -112,6 +118,15 @@ async def evaluate_preludes(
return BuildFilePreludeSymbols.create(locals, env_vars)


@rule
async def get_all_build_file_symbols_info(
parser: Parser, prelude_symbols: BuildFilePreludeSymbols
) -> BuildFileSymbolsInfo:
return BuildFileSymbolsInfo.from_info(
parser.symbols_info.info.values(), prelude_symbols.info.values()
)


@rule
async def maybe_resolve_address(address_input: AddressInput) -> MaybeAddress:
# Determine the type of the path_component of the input.
Expand Down Expand Up @@ -480,4 +495,10 @@ async def get_dependencies_rule_application(


def rules():
return collect_rules()
return (
*collect_rules(),
# The `BuildFileSymbolsInfo` is consumed by the `HelpInfoExtracter` and uses the scheduler
# session `product_request()` directly so we need an explicit QueryRule to provide this type
# as an valid entrypoint into the rule graph.
QueryRule(BuildFileSymbolsInfo, ()),
)
Loading

0 comments on commit dd2eb85

Please sign in to comment.