Skip to content

Instantly share code, notes, and snippets.

@rrbutani
Created September 29, 2024 02:22
Show Gist options
  • Save rrbutani/99833daab2b1447ff5deedcaa89f4d63 to your computer and use it in GitHub Desktop.
Save rrbutani/99833daab2b1447ff5deedcaa89f4d63 to your computer and use it in GitHub Desktop.

takeaways:

  • actions made by an aspect get to make files in the package of the target they are visiting
  • have "anonymous" action-like semantics, kind of
    • will not get built multiple times if the same aspect for the same target is requested by two different consuming rules
bazel build //:yo @foo//:all
use nix -p bazel_7
.direnv
/bazel-*
MODULE.bazel.lock
load(":defs.bzl", "example", "frob", "flub")
example(name = "a", deps = [])
example(name = "b", deps = [":a"])
example(name = "c", deps = [":b", ":a"])
example(name = "d", deps = [":c", ":a"])
example(name = "e", deps = [":d", ":b", "@foo//:hey"], visibility = ["//visibility:public"])
example(name = "f", deps = [":a"])
frob(name = "yo1", inner = ":e")
flub(name = "yo2", inner = ":f")
filegroup(
name = "yo",
srcs = [":yo1", ":yo2"],
)
MyInfo = provider(fields = {})
def _example_implementation(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(out, "")
return [MyInfo(), DefaultInfo(files = depset([out]))]
example = rule(
implementation = _example_implementation,
attrs = {
"deps": attr.label_list(
providers = [MyInfo],
),
},
provides = [MyInfo],
)
################################################################################
ExampleInfo = provider(fields = dict(original = "depset[File]", markers = "depset[File]"))
def _aspect_implementation(target, actx):
print(target)
print(actx.label)
marker = actx.actions.declare_file(actx.label.name + ".marker")
actx.actions.write(marker, actx.label.name)
original = depset(
direct = [target[DefaultInfo].files.to_list()[0]],
transitive = [
d[ExampleInfo].original
for d in actx.rule.attr.deps
],
)
markers = depset(
direct = [marker],
transitive = [
d[ExampleInfo].markers
for d in actx.rule.attr.deps
],
)
return [ExampleInfo(original = original, markers = markers)]
example_aspect = aspect(
implementation = _aspect_implementation,
attr_aspects = ["deps"],
attrs = {},
required_providers = [[MyInfo]],
# required_aspect_providers,
provides = [ExampleInfo],
# requires,
)
################################################################################
frob = rule(
implementation = lambda ctx: [DefaultInfo(
files = ctx.attr.inner[ExampleInfo].markers
)],
attrs = {
"inner": attr.label(providers = [MyInfo], aspects = [example_aspect]),
},
)
flub = rule(
implementation = lambda ctx: [DefaultInfo(
files = ctx.attr.inner[ExampleInfo].markers
)],
attrs = {
"inner": attr.label(providers = [MyInfo], aspects = [example_aspect]),
},
)
load("@//:defs.bzl", "example", "frob")
package(default_visibility = ["//visibility:public"])
example(name = "hey")
frob(name = "test", inner = "@//:e")
foo_repo = repository_rule(
implementation = lambda rctx: rctx.file("BUILD.bazel", rctx.read(rctx.attr.build_file)),
attrs = dict(build_file = attr.label()),
)
foo = module_extension(
implementation = lambda mctx: foo_repo(name = "foo", build_file = "//:foo.BUILD.bazel"),
)
use_repo(use_extension("//:foo_repo.bzl", "foo"), "foo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment