Skip to content

Commit

Permalink
Let buildozer preserve type-specific formatting (#1148)
Browse files Browse the repository at this point in the history
Before this commit, when applying buildozer to e.g. MODULE.bazel file,
it would still apply BUILD-specific formatting rules.
  • Loading branch information
fmeum authored Mar 17, 2023
1 parent 4da6558 commit 9c3c1fc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
31 changes: 31 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2029,4 +2029,35 @@ EOF
)' pkg2
}

function test_module_bazel() {
cat > MODULE.bazel <<EOF
module(
name = "foo",
version = "0.27.0",
)
bazel_dep(name = "gazelle", version = "0.30.0")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps, "com_example_foo")
EOF

cat > MODULE.bazel.expected <<EOF
module(
name = "foo",
version = "0.27.0",
)
bazel_dep(name = "gazelle", version = "0.30.0")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
EOF

$buildozer 'delete' //MODULE.bazel:%10
diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
}


run_suite "buildozer tests"
2 changes: 1 addition & 1 deletion edit/buildozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ func rewrite(opts *Options, commandsForFile commandsForFile) *rewriteResult {
}
}

f, err := build.ParseBuild(name, data)
f, err := build.Parse(name, data)
if err != nil {
return &rewriteResult{file: name, errs: []error{err}}
}
Expand Down
2 changes: 1 addition & 1 deletion edit/default_buildifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (b *defaultBuildifier) Buildify(opts *Options, f *build.File) ([]byte, erro
// value is a chunk of code, like "f(x)". The AST should be printed and
// re-read to parse such expressions correctly.
contents := build.Format(f)
newF, err := build.ParseBuild(f.Path, []byte(contents))
newF, err := build.Parse(f.Path, contents)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func PackageDeclaration(f *build.File) *build.Rule {
// This might appear because of a buildozer transformation (e.g. when removing a package
// attribute). Removing it is required for the file to be valid.
func RemoveEmptyPackage(f *build.File) *build.File {
if f.Type != build.TypeBuild {
return f
}
var all []build.Expr
for _, stmt := range f.Stmt {
if isEmptyPackage(stmt) {
Expand Down Expand Up @@ -315,7 +318,7 @@ func DeleteRule(f *build.File, rule *build.Rule) *build.File {
}
all = append(all, stmt)
}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: build.TypeBuild}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: f.Type}
}

// DeleteRuleByName returns the AST without the rules that have the
Expand All @@ -333,7 +336,7 @@ func DeleteRuleByName(f *build.File, name string) *build.File {
all = append(all, stmt)
}
}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: build.TypeBuild}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: f.Type}
}

// DeleteRuleByKind removes the rules of the specified kind from the AST.
Expand All @@ -351,7 +354,7 @@ func DeleteRuleByKind(f *build.File, kind string) *build.File {
all = append(all, stmt)
}
}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: build.TypeBuild}
return &build.File{Path: f.Path, Comments: f.Comments, Stmt: all, Type: f.Type}
}

// AllLists returns all the lists concatenated in an expression.
Expand Down
2 changes: 1 addition & 1 deletion edit/safe/buildifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (b *buildifier) Buildify(_ *edit.Options, f *build.File) ([]byte, error) {
// value is a chunk of code, like "f(x)". The AST should be printed and
// re-read to parse such expressions correctly.
contents := build.Format(f)
newF, err := build.ParseBuild(f.Path, []byte(contents))
newF, err := build.Parse(f.Path, contents)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9c3c1fc

Please sign in to comment.