Skip to content
Open
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
38 changes: 38 additions & 0 deletions inspections.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Experiments with Bazel queries and aspects to inspect targets
=============================================================

- Exploring targets

.. code-block:: bash

bazel query //tools/sphinx/codelinks/...

- Find all ``codelinks_analyse`` rule type

.. code-block:: bash

bazel query 'kind(codelinks_analyse, //...)'

- Print out the source files of ``codelinks_analyse`` target in ``//projects/acdc/ac``

.. code-block:: bash

bazel build //projects/acdc/ac:codelinks_analyse --aspects print.bzl%print_aspect

- print out the source files of all ``codelinks_analyse`` targets used in the repo

.. code-block:: bash

bazel build $(bazel query "kind(codelinks_analyse, //...)") --aspects //:print.bzl%print_aspect

- Find all targets that depends on file ``projects/acdc/ac:src/ac.c``

.. code-block:: bash

bazel query "rdeps(//..., //projects/acdc/ac:src/ac.c)"

- Find which targets of a specific kind depend on a certain file

.. code-block:: bash

bazel query "kind(codelinks_analyse, rdeps(//..., //projects/acdc/ac:codelinks.toml))"
31 changes: 31 additions & 0 deletions print.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Reference: https://bazel.build/extending/aspects

def _print_aspect_impl(target, ctx):

Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary blank line for cleaner code formatting.

Suggested change

Copilot uses AI. Check for mistakes.
print("+ print_aspect_impl")
print("Target: " + str(target.label))
print(ctx.rule.kind)
target_kind = getattr(ctx.attr, "target_kind", "")
print("Target kind from attr: " + target_kind)
# Make sure the rule has a srcs attribute.
if target_kind != ctx.rule.kind:
return []

if hasattr(ctx.rule.attr, 'srcs'):
# Iterate through the files that make up the sources and
# print their paths.
for src in ctx.rule.attr.srcs:
for f in src.files.to_list():
print(f.path)
print("- print_aspect_impl")
return []

print_aspect = aspect(
implementation = _print_aspect_impl,
attr_aspects = ['srcs'], # the edge to track
attrs = {
'target_kind': attr.string(
default = 'codelinks_analyse',),
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing comma after the closing parenthesis - it's not needed when there are no additional parameters.

Suggested change
default = 'codelinks_analyse',),
default = 'codelinks_analyse'),

Copilot uses AI. Check for mistakes.
}
# required_providers = [CcInfo],
)