buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

Allow buildozer to edit top-level variable assignments, not just targets

Open tetromino opened this issue 4 years ago • 2 comments

Suppose we want to programmatically edit a top-level variable in a BUILD or bzl file.

For example, maybe we want to add a new entry to a list constant in a BUILD file which is used by multiple targets:

MY_COPTS = ["--foo", "--bar"]

...

cc_library(name = "a_lib", copts = MY_COPTS, ...)
cc_library(name = "b_lib", copts = MY_COPTS + OTHER_COPTS, ...)

Or perhaps we want to edit an exported top-level dict constant defined in a .bzl file.

Currently, buildozer provides no way to do so.

Currently, we can hack around this by introducing a macro in a .bzl file which mutates the top-level constant, invoke that macro in a way which looks like a target declaration, and edit the invocation with buildozer, e.g.:

MY_COPTS = []

def set_my_copts(my_copts):
    for val in my_copts:
        MY_COPTS.append(val)

set_my_copts(my_copts = ["--foo", "--bar"])

cat my_copts.bzl | buildozer add 'my_copts "--baz"' -:%set_my_copts

But really there ought to be a better way.

tetromino avatar Sep 07 '21 20:09 tetromino

Buildozer CLI was originally designed for dealing with .bzl files. If the variable is used in target of the BUILD file and you want to edit the target, --edit-variables can help.

I agree it could be nice to expand the scope of Buildozer.

laurentlb avatar Sep 07 '21 21:09 laurentlb

Does --edit-variables support variables loaded from another bzl file?

guw avatar Apr 15 '24 14:04 guw