Skip to content
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

Allow keep to be explained #1447

Merged
merged 5 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,9 @@ In addition to directives, Gazelle supports ``# keep`` comments that protect
parts of build files from being modified. ``# keep`` may be written before
a rule, before an attribute, or after a string within a list.

``# keep`` comments might take one of 2 forms; the ``# keep`` literal or a
description prefixed by ``# keep: ``.

Example
^^^^^^^

Expand All @@ -1003,6 +1006,7 @@ know what imports to resolve, so you may need to add dependencies manually with
visibility = ["//visibility:public"],
deps = [
"@com_github_example_gen//:go_default_library", # keep
"@com_github_example_gen//a/b/c:go_default_library", # keep: this is also important
],
)

Expand Down
2 changes: 1 addition & 1 deletion rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ func (r *Rule) sync() {
func ShouldKeep(e bzl.Expr) bool {
for _, c := range append(e.Comment().Before, e.Comment().Suffix...) {
text := strings.TrimSpace(strings.TrimPrefix(c.Token, "#"))
if text == "keep" {
if text == "keep" || strings.HasPrefix(text, "keep: ") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please also document this new capability under https://github.com/bazelbuild/bazel-gazelle#keep-comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 708dd8b

return true
}
}
Expand Down
28 changes: 28 additions & 0 deletions rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ func TestKeepRule(t *testing.T) {
src: `
# keep
x_library(name = "x")
`,
want: true,
}, {
desc: "prefix with description",
src: `
# keep: hack, see more in ticket #42
x_library(name = "x")
`,
want: true,
}, {
Expand Down Expand Up @@ -360,6 +367,27 @@ func TestShouldKeepExpr(t *testing.T) {
"s"
`,
want: true,
}, {
desc: "before with description",
src: `
# keep: we need it for the ninja feature
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you also add negative tests for the undesirable cases I mentioned in my comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in dc7aa9c.

"s"
`,
want: true,
}, {
desc: "before but not the correct prefix (keeping)",
src: `
# keeping this for now
"s"
`,
want: false,
}, {
desc: "before but not the correct prefix (no colon)",
src: `
# keep this around for the time being
"s"
`,
want: false,
}, {
desc: "suffix",
src: `
Expand Down