'expand_template with make variables

I have a seemingly simple task that is giving me a hard time: expand a template with a variable defined by the user.

I have a file (template) that has one variable I need to substitute:

$key_selection_color: @SELECTION_COLOR@;

I decided to use expand_template like this:

expand_template(
    name = "key_colors",
    out = "_key_colors.scss",
    substitutions = {
        "@SELECTION_COLOR@": "$(FOO)",
    },
    template = "_key_colors.scss.in",
)

However, when I run bazel build --define FOO=000000 //:key_colors, $(FOO) doesn't get substituted and my _key_colors.scss file becomes:

$key_selection_color: $(FOO);

What have I done wrong and what should I do to achieve the desired behavior? Thank you!



Solution 1:[1]

The bazel-skylib implementation of this rule does not support replacing vars in the substitutions, in the same way it doesn't support expanding locations etc (at the time of writing this). It's a very light wrapper around ctx.actions.expand_template.

However, the aspect-build/bazel-lib implementation of expand_template does support these substitutions. See the docs for further info.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Mackay