Skip to content

Adding support for .bzl file operations#1464

Open
mydatascience wants to merge 2 commits into
bazelbuild:mainfrom
mydatascience:bzl_support
Open

Adding support for .bzl file operations#1464
mydatascience wants to merge 2 commits into
bazelbuild:mainfrom
mydatascience:bzl_support

Conversation

@mydatascience
Copy link
Copy Markdown

@mydatascience mydatascience commented May 8, 2026

Buildtools PR checklist

  • The code in this PR is covered by unit/integration tests.
  • I have tested these changes and provide testing instructions below.
  • I have either responded to, or resolved all Gemini comments on the PR.
  • I have read Google Eng Practices on Small Changes, this PR either follows these guidelines or the description provides reasoning for why they can not be followed.

Description

This PR extends edit label resolution to allow targeting .bzl files directly (for buildozer-style edits), e.g. //pkg:defs.bzl and //pkg:defs.bzl:rule. This makes it possible to apply edit transformations (like adding/removing entries from list attributes such as patches = [...]) to .bzl files, while still relying on buildifier-compatible formatting.

It also adds unit tests covering:

resolving .bzl targets via InterpretLabelForWorkspaceLocation
editing a patches list in a parsed .bzl file (add + remove) and validating formatted output

(optional) These changes were tested using the following steps

Run unit tests for the edited package:
go test ./edit
(Optional sanity) Run the specific new tests:
go test ./edit -run 'TestInterpretLabelForWorkspaceLocation_BzlFile|TestAddAndRemovePatchesInBzlFile'

@mydatascience mydatascience requested a review from a team as a code owner May 8, 2026 17:34
@mydatascience mydatascience requested review from oreflow and removed request for a team May 8, 2026 17:34
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request extends the build tools to support editing .bzl files, enabling buildozer-style transformations on global variables. Key changes include updated label parsing for .bzl targets, improved global variable detection that excludes function-local assignments, and modifications to cmdAdd and cmdRemove. Feedback highlights critical logic errors in the new command handlers where variable-based rule checks were inverted and cmdRemove incorrectly used an addition function. Refactoring was also recommended to eliminate duplicated label parsing logic.

Comment thread edit/buildozer.go Outdated
// The cmdXXX functions implement the various commands.

func cmdAdd(opts *Options, env CmdEnvironment) (*build.File, error) {
if env.Rule != nil && env.Rule.Call != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The condition env.Rule.Call != nil is incorrect for variable editing. According to the changes in expandTargets (lines 995-996), variable targets are represented by a dummy Rule where the Call field is nil. This check makes the logic for adding values to variables unreachable.

Suggested change
if env.Rule != nil && env.Rule.Call != nil {
if env.Rule != nil && env.Rule.Call == nil {
References
  1. When modifying code, ensure that changes are consistent with previous behavior, especially if the previous behavior was intentional.

Comment thread edit/buildozer.go Outdated
Comment thread edit/buildozer.go
Comment thread edit/edit.go Outdated
Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Adding support for .bzl file operations

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Adding support for .bzl file operations

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Adding support for .bzl file operations

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Fixing comments

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Fixing comments

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>

Fix tests

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>
@mydatascience mydatascience changed the title [Draft]Adding support for .bzl file operations Adding support for .bzl file operations May 8, 2026
@mydatascience
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for editing global variables in .bzl files via buildozer. It includes updates to label resolution to handle .bzl files and refactors the Kind() method for more robust path extraction. Review feedback points out a risk of panics in existing commands due to the introduction of rules with nil call expressions and identifies limitations in the variable detection logic regarding tuple unpacking and loop variables.

Comment thread edit/buildozer.go
Comment thread edit/buildozer.go
Comment on lines +1174 to +1178
if as, ok := x.(*build.AssignExpr); ok {
if lhs, ok := as.LHS.(*build.Ident); ok {
vars[lhs.Name] = as
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This implementation only detects global variables assigned via simple identifiers (e.g., VAR = ...). It will miss variables assigned via tuple unpacking (e.g., (A, B) = ...) or those defined as loop variables in top-level for statements. While this may be acceptable for common use cases, it is a limitation of the current variable detection logic.

Signed-off-by: Vladimir Suvorov <suvorovv@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant