Skip to content

Instantly share code, notes, and snippets.

@rrbutani
Last active April 2, 2026 06:17
Show Gist options
  • Select an option

  • Save rrbutani/355693fd84a91091f94da87c04f109f7 to your computer and use it in GitHub Desktop.

Select an option

Save rrbutani/355693fd84a91091f94da87c04f109f7 to your computer and use it in GitHub Desktop.

known:

  • ctx.expand_location expands to .path, not short_path
    • there's a hidden kwarg (that we are not allowed to use) short_paths that provides the latter...

to be determined (by this experiment):

  1. are variables provided via the toolchains attr implicitly added to ctx.var?
  2. how do rules, in practice, grab files from elements in toolchains and add them to the inputs of the action(s) crafted?
  3. what targets/files are implicitly available to ctx.expand_location?

answers:

  1. yes, TemplateVariableInfo.variables for items in ctx.attr.toolchains is made available via ctx.var
  2. genrule appears to unconditionally take DefaultInfo.files from ctx.attr.toolchains for both toolchains and toolchain types. this is the case even if a particular toolchain does not provide platform_common.TemplateVariableInfo
  3. predeclared outputs are implicitly available. Targets from srcs, deps, implementation_deps, and tools attributes are also implicitly available.
use nix -p bazel_8
load(":defs.bzl", "my_toolchain", "dump_vars", "expand_locs")
#-------------------------------------------------------------------------------
toolchain_type(name = "my_toolchain_type")
my_toolchain(
name = "my_toolchain",
inputs = [":BUILD.bazel", ":defs.bzl"],
)
toolchain(
name = "toolchain",
toolchain = ":my_toolchain",
toolchain_type = ":my_toolchain_type",
)
#-------------------------------------------------------------------------------
dump_vars(name = "base")
dump_vars(name = "with_toolchain", toolchains = [":my_toolchain"])
expand_locs(name = "exp_base", string = "", data = [])
expand_locs(name = "exp_error", string = "$(location :BUILD.bazel)", data = []) # errors
expand_locs(name = "exp_with_data", string = "$(location :defs.bzl)", data = [":defs.bzl"]) # errors
expand_locs(name = "exp_with_srcs", string = "$(location :defs.bzl)", srcs = [":defs.bzl"])
expand_locs(name = "exp_with_extra_outs", string = "$(location :out4)", extra_outs = ["out4"])
expand_locs(name = "exp_with_toolchains", string = "$(location :defs.bzl)", toolchains = [":my_toolchain"]) # errors
# test case 1: has `toolchains` but no make vars that reference it
genrule(
name = "a",
outs = ["a-out"],
cmd = "touch $@",
toolchains = [":my_toolchain_type"],
)
# test case 2: with `$(COUNT) that references the toolchain
genrule(
name = "b",
outs = ["b-out"],
cmd = "touch $@ && : $(COUNT)",
toolchains = [":my_toolchain_type"],
)
# test case 3: with `$(location)`s that references it? (errors)
genrule(
name = "c",
outs = ["c-out"],
cmd = "touch $@ && : $(location :BUILD.bazel)",
toolchains = [":my_toolchain_type"],
)
MyToolchainInfo = provider(fields = dict(count = "int", files = "depset[File]"))
#-------------------------------------------------------------------------------
def _my_toolchain_impl(ctx):
# type: (ctx) -> list[struct]
files = depset(ctx.files.inputs)
def_info = DefaultInfo(files = files)
my_toolchain = MyToolchainInfo(count = len(ctx.files.inputs), files = files)
vars = platform_common.TemplateVariableInfo(
dict(COUNT = str(len(ctx.files.inputs))),
)
return [
vars,
def_info,
my_toolchain,
platform_common.ToolchainInfo(my_toolchain = my_toolchain),
]
my_toolchain = rule(
implementation = _my_toolchain_impl,
attrs = dict(
inputs = attr.label_list(allow_files = True),
),
)
#-------------------------------------------------------------------------------
def _dump_vars_impl(ctx):
out = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.write(out, json.encode_indent(ctx.var))
return DefaultInfo(files = depset([out]))
dump_vars = rule(implementation = _dump_vars_impl)
#-------------------------------------------------------------------------------
def _expand_locs_impl(ctx):
for out in ctx.outputs.extra_outs:
ctx.actions.write(out, "")
out = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.write(out, ctx.expand_location(ctx.attr.string))
return DefaultInfo(files = depset([out]))
expand_locs = rule(
implementation = _expand_locs_impl,
attrs = dict(
string = attr.string(),
srcs = attr.label_list(allow_files = True),
data = attr.label_list(allow_files = True),
extra_outs = attr.output_list(),
),
)
register_toolchains("//:toolchain")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment