-
Notifications
You must be signed in to change notification settings - Fork 461
Allowing to disable buildozer on specific targets via #buildozer: leave-alone #1453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
oreflow
wants to merge
2
commits into
main
Choose a base branch
from
buildozer-leave-alone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package build | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
| ) | ||
|
|
||
| // checkSelfOrInheritedComment checks comments relate to the expression, and returns true if | ||
| // the provided predicate matches any of the comments. | ||
| func checkSelfOrInheritedComment(expr Expr, predicate func(Comment) bool) bool { | ||
| for _, comment := range slices.Concat( | ||
| expr.Comment().Before, | ||
| expr.Comment().After, | ||
| expr.Comment().Suffix, | ||
| expr.Comment().Inherited) { | ||
| if predicate(comment) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // HasCommentContaining does a case insensitive matching to see if an expression, | ||
| // or its parent expressions have a comment containing the provided prefix. | ||
| func HasCommentContaining(expr Expr, prefix string) bool { | ||
| return checkSelfOrInheritedComment(expr, func(comment Comment) bool { | ||
| trimmedComment := strings.Trim(comment.Token, " \t\n#") | ||
| return strings.Contains(strings.ToLower(trimmedComment), strings.ToLower(prefix)) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package build | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func callByName(name string) func(f *File) Expr { | ||
| return func(f *File) Expr { | ||
| var expr Expr | ||
| WalkInterruptable(f, func(x Expr, stk []Expr) error { | ||
| if call, ok := x.(*CallExpr); ok { | ||
| if ident, ok := call.X.(*Ident); ok && ident.Name == name { | ||
| expr = call | ||
| return &StopTraversalError{} | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| return expr | ||
| } | ||
| } | ||
|
|
||
| func assignExprByLHSName(name string) func(f *File) Expr { | ||
| return func(f *File) Expr { | ||
| var expr Expr | ||
| WalkInterruptable(f, func(x Expr, stk []Expr) error { | ||
| if assign, ok := x.(*AssignExpr); ok { | ||
| if ident, ok := assign.LHS.(*Ident); ok && ident.Name == name { | ||
| expr = assign | ||
| return &StopTraversalError{} | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| return expr | ||
| } | ||
| } | ||
|
|
||
| func TestHasCommentContaining(t *testing.T) { | ||
| var tests = []struct { | ||
| name string | ||
| buildFile string | ||
| selector func(*File) Expr | ||
| comment string | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "rule_call_with_comment", | ||
| buildFile: ` | ||
| # has-comment | ||
| my_rule( | ||
| name = "my_target", | ||
| )`, | ||
| comment: "has-comment", | ||
| selector: callByName("my_rule"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "rule_call_with_trailing_comment", | ||
| buildFile: ` | ||
| my_rule( | ||
| name = "my_target", | ||
| ) # has-comment | ||
| `, | ||
| comment: "has-comment", | ||
| selector: callByName("my_rule"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "rule_call_with_inherited_comment", | ||
| buildFile: ` | ||
| # has-comment | ||
| my_rule( | ||
| name = "my_target", | ||
| attr = my_func() | ||
| ) | ||
| `, | ||
| comment: "has-comment", | ||
| selector: callByName("my_func"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "function_call_with_comment", | ||
| buildFile: ` | ||
| my_rule( | ||
| name = "my_target", | ||
| # has-comment | ||
| attr = my_func() | ||
| ) | ||
| `, | ||
| comment: "has-comment", | ||
| selector: callByName("my_func"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "function_call_with_trailing_comment", | ||
| buildFile: ` | ||
| my_rule( | ||
| name = "my_target", | ||
| attr = my_func() # has-comment | ||
| ) | ||
| `, | ||
| comment: "has-comment", | ||
| selector: callByName("my_func"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "rule_call_without_comment", | ||
| buildFile: ` | ||
| my_rule( | ||
| name = "my_target", | ||
| )`, | ||
| comment: "has-comment", | ||
| selector: callByName("my_rule"), | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "sibling_call_has_comment", | ||
| buildFile: ` | ||
| my_rule( | ||
| name = "my_target", | ||
| attr = my_func(), | ||
| attr2 = my_other_func() # has-comment | ||
| )`, | ||
| comment: "has-comment", | ||
| selector: callByName("my_func"), | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "assign_expr_with_trailing_comment", | ||
| buildFile: ` | ||
| my_var = 1 # has-comment | ||
| `, | ||
| comment: "has-comment", | ||
| selector: assignExprByLHSName("my_var"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "assign_expr_with_inherited_comment", | ||
| buildFile: ` | ||
| # has-comment | ||
| my_rule( | ||
| name = "my_target", | ||
| attr = my_func(func_arg = 1) | ||
| ) | ||
| `, | ||
| comment: "has-comment", | ||
| selector: assignExprByLHSName("func_arg"), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "assign_expr_without_comment", | ||
| buildFile: ` | ||
| my_var = 1 | ||
| `, | ||
| comment: "has-comment", | ||
| selector: assignExprByLHSName("my_var"), | ||
| want: false, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| bld, err := Parse("BUILD", []byte(tc.buildFile)) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
| expr := tc.selector(bld) | ||
| if expr == nil { | ||
| t.Error("selector returned nil") | ||
| } | ||
| got := HasCommentContaining(expr, tc.comment) | ||
| if got != tc.want { | ||
| t.Errorf("HasCommentContaining(%q) = %v, want %v", tc.comment, got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
suffixComment.Start.Line <= eStart.Line || suffixComment.Start.Line <= eEnd.Lineis redundant and can be simplified tosuffixComment.Start.Line <= eEnd.Line.However, the existing comment on line 919 states that suffix comments are inherited for expressions that overlap with the comment line number. An overlap check would be
eStart.Line <= suffixComment.Start.Line && suffixComment.Start.Line <= eEnd.Line.This creates a discrepancy between the code's logic and its documentation. Please clarify the intended behavior. If the 'overlap' behavior (as described in the existing comment) was the intentional previous behavior, the code should be adjusted to implement that. If the
suffixComment.Start.Line <= eEnd.Linebehavior is the intended one, then the code can be simplified as noted, and the comment on line 919 should be updated to reflect this logic.Please ensure the chosen behavior is consistent with previous intentional behavior.
References