bazel-contrib / bazel-contrib/buildtools

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

Open
#999 5 comments 1 reaction 0 assignees View on GitHub
P2
Dominant language
Go
Stars
1.2k
Forks
471
Avg merge
2d 22h
Merged PRs (30d)
13

Description

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.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing how buildozer currently parses and edits target declarations, then compare that flow with the top-level assignments shown in the BUILD and .bzl examples. Done means buildozer can directly edit top-level list or dict variables without the set_my_copts macro workaround, while preserving existing target-editing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.