Public API for expanding variables



Template expansion

This performs a simple search over the template file for the keys in substitutions, and replaces them with the corresponding values.

Values may also use location templates as documented in expand_locations as well as configuration variables such as $(BINDIR), $(TARGET_CPU), and $(COMPILATION_MODE).

Example usage (generated)

load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_template")

    # A unique name for this target.
    name = "",
    # Where to write the expanded file.
    out = "",
    # Mapping of strings to substitutions.
    substitutions = {},
    # The template file to expand.
    template = "",


A unique name for this target.


List of targets for additional lookup information.


Whether to mark the output file as executable.


Where to write the expanded file.


Mapping of strings to substitutions.


The template file to expand.

Macros and Functions


Expand location templates.

Expands all $(execpath ...), $(rootpath ...) and legacy $(location ...) templates in the given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies of this rule or that are explicitly listed in the optional argument targets.


Use $(rootpath) and $(rootpaths) to expand labels to the runfiles path that a built binary can use to find its dependencies. This path is of the format:

  • ./file
  • path/to/file
  • ../external_repo/path/to/file

Use $(execpath) and $(execpaths) to expand labels to the execroot (where Bazel runs build actions). This is of the format:

  • ./file
  • path/to/file
  • external/external_repo/path/to/file
  • <bin_dir>/path/to/file
  • <bin_dir>/external/external_repo/path/to/file

The legacy $(location) and $(locations) expansions are deprecated as they return the runfiles manifest path of the format repo/path/to/file which behave differently than the built-in $(location) expansion in args of *_binary and *_test rules which returns the rootpath. See

The legacy $(location) and $(locations) expansion also differs from how the builtin ctx.expand_location() expansions of $(location) and $(locations) behave as that function returns either the execpath or rootpath depending on the context. See

The behavior of $(location) and $(locations) expansion will be fixed in a future major release to match the to default Bazel behavior and return the same path as ctx.expand_location() returns for these.

The recommended approach is to now use $(rootpath) where you previously used $(location). See the docstrings of nodejs_binary or params_file for examples of how to use $(rootpath) in templated_args and args respectively.

Example usage (generated)

load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations")

    # context
    ctx = None,
    # String to be expanded
    input = None,




String to be expanded


List of targets for additional lookup information.


Expand make variables and substitute like genrule does.

This function is the same as ctx.expand_make_variables with the additional genrule-like substitutions of:

  • $@: The output file if it is a single file. Else triggers a build error.
  • $(@D): The output directory. If there is only one file name in outs,
       this expands to the directory containing that file. If there are multiple files,
       this instead expands to the package's root directory in the bin tree,
       even if all generated files belong to the same subdirectory!
  • $(RULEDIR): The output directory of the rule, that is, the directory corresponding to the name of the package containing the rule under the bin tree.

See and for more information of how these special variables are expanded.

Example usage (generated)

load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")

    # starlark rule context
    ctx = None,
    # expression to expand
    s = None,


starlark rule context


expression to expand


declared outputs of the rule, for expanding references to outputs


whether the rule is expected to output a directory (TreeArtifact)


name of the attribute containing the expression