Keep `include` calls together in `MODULE.bazel` files (#1345)
diff --git a/build/print.go b/build/print.go index 8e6cc96..0492dd4 100644 --- a/build/print.go +++ b/build/print.go
@@ -273,6 +273,10 @@ // foo_deps.module(path = "github.com/foo/bar") // use_repo(foo_deps, "com_github_foo_bar") return true + } else if p.fileType == TypeModule && isInclude(s1) && isInclude(s2) { + // include takes a single argument and there are typically multiple in + // a row, so we want to keep them together. + return true } else if isCommentBlock(s1) || isCommentBlock(s2) { // Standalone comment blocks shouldn't be attached to other statements return false @@ -448,6 +452,15 @@ } } +func isInclude(x Expr) bool { + if call, ok := x.(*CallExpr); ok { + if ident, ok := call.X.(*Ident); ok && ident.Name == "include" { + return true + } + } + return false +} + // isCommentBlock reports whether x is a comment block node. func isCommentBlock(x Expr) bool { _, ok := x.(*CommentBlock)
diff --git a/buildifier/integration_test.sh b/buildifier/integration_test.sh index 293e8bb..1a1511e 100755 --- a/buildifier/integration_test.sh +++ b/buildifier/integration_test.sh
@@ -51,6 +51,10 @@ echo -e "a+b" > test_dir/.git/git.bzl cat > test_dir/MODULE.bazel <<'EOF' module(name='my-module',version='1.0',compatibility_level=1) +include("cpp.MODULE.bazel") + +include("go.MODULE.bazel") +include("web.MODULE.bazel") bazel_dep(name='rules_cc',version='0.0.1') bazel_dep(name='protobuf',repo_name='com_google_protobuf',version='3.19.0') bazel_dep( @@ -161,6 +165,10 @@ compatibility_level = 1, ) +include("cpp.MODULE.bazel") +include("go.MODULE.bazel") +include("web.MODULE.bazel") + bazel_dep(name = "rules_cc", version = "0.0.1") bazel_dep(name = "protobuf", version = "3.19.0", repo_name = "com_google_protobuf") bazel_dep(