构建器
构建器是将bazel BUILD 和.bzl 文件与标准常规格式化的工具.
设置
构建工具 :
- 检查回购,然后通过
go install或者说bazel build //buildifier - 如果您已经安装了“ go” , 然后通过 :
go install github.com/bazelbuild/buildtools/buildifier@latest
使用量
使用构建器为 BUILD 和 .bzl 文件创建标准化格式 与用于源文件的clang-format相同.
$ buildifier path/to/file
您也可以同时处理多个文件 :
$ buildifier path/to/file1 path/to/file2
您可以让构建器自动查找所有 Starlark 文件( 即 BUILD, WorkSPACE, . bzl, 或 递归目录中:
$ buildifier -r path/to/dir
构建者支持以下文件类型: BUILD, (中文(简体) ), WORKSPACE, (中文(简体) ), .bzl,以及
默认情况下, 后者被保留给 Stararak 文件构建器不知道
(例如,用于使用Starlark的第三方项目的配置文件). 这个
workSPACE文件的格式规则与BUILD文件的格式规则相同(两者都是:
声明性并有更严格的格式化规则),默认文件格式化
类似.bzl文件,允许更大的灵活性. 不同的间隙警告
可能仅限于这些文件类型的任何子集,例如:
只与 Bazel 文件有关(即:. BUILD, (中文(简体) ), WORKSPACE,以及 .bzl(或改为)
非WORKSPACE文件.
构建器会自动通过文件名检测文件类型,其中包含
账户可选前缀和后缀,例如:. BUILD, (中文(简体) ), BUILD.oss,或
BUILD.bazel 将被检测为 BUILD 文件,并且 build_defs.bzl.oss 是一个
. bzl 文件 (中文(简体) ). 有未知名称的文件(例如. foo.bar)或通过stdin通过的文件
将被视为默认文件类型。 覆盖自动文件类型
检测使用 --type 明确标记 :
$ cat foo.bar | buildifier --type=build
$ cat foo.bar | buildifier --type=bzl
$ cat foo.bar | buildifier --type=workspace
$ cat foo.bar | buildifier --type=default
$ cat foo.bar | buildifier --type=module
线条
建构器有一个集成电路,可以指出,在某些情况下 自动解决各种问题。 用它来发射以下命令之一 (注意有些问题不能是 自动固定 :
buildifier --lint=warn path/to/file
buildifier --lint=fix path/to/file
默认情况下,对与给定相关的所有已知问题的线性搜索 文件类型,但有标记的文件除外 " (中文(简体) )默认禁用" 在文件中.
您可以使用 --warnings 通过提供
明确类别:
buildifier --lint=warn --warnings=positional-args,duplicated-name
或通过使用 + 或者说 - 修改前
每个警告类别 :
buildifier --lint=warn --warnings=-positional-args,+unsorted-dict-items
也可以提供 --warnings=all 以使用所有支持的警告
类别( 它们仍将限于给定文件的相关警告)
类型).
另见 完整列表 或支持的警告.
通过 Bazel 设置和使用
您也可以通过 Bazel 规则引用构建者 .
WORKSPACE 文件 :
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")
go_rules_dependencies()
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains")
go_register_toolchains(version = "1.20.3")
http_archive(
name = "bazel_gazelle",
sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
],
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
# If you use WORKSPACE.bazel, use the following line instead of the bare gazelle_dependencies():
# gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")
gazelle_dependencies()
http_archive(
name = "com_google_protobuf",
sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568",
strip_prefix = "protobuf-3.19.4",
urls = [
"https://github.com/protocolbuffers/protobuf/archive/v3.19.4.tar.gz",
],
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3",
strip_prefix = "buildtools-4.2.2",
urls = [
"https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz",
],
)
BUILD.bazel 通常在工作空间根中:
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
buildifier(
name = "buildifier",
)
调用
bazel run //:buildifier
Json中的文件诊断
构建器支持以机器可读格式(json)输出的诊断,由
--format=json (仅与 --mode=check) (中文(简体) ). 如果结合使用 -v, (中文(简体) ),
输出 json 将缩进,以便更好的可读性.
输出格式如下:
{
"success": false, // true if all files are formatted and generate no warnings, false otherwise
"files": [ // list of all files processed by buildifier
{
"filename": "file_1.bzl",
"formatted": true, // whether the file is correctly formatted
"valid": true, // whether the file is a valid Starlark file. Can only be false if formatted = false
"warnings": [ // a list of warnings
{
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 10
},
"category": "integer-division",
"actionable": true,
"autoFixable": true,
"message": "The \"/\" operator for integer division is deprecated in favor of \"//\".",
"url": "https://github.com/bazelbuild/buildtools/blob/main/WARNINGS.md#integer-division"
}
]
},
{
"filename": "file_2.bzl",
"formatted": false,
"valid": true,
"warnings": [],
"rewrites": { // technical information, a list of rewrites buildifier applies during reformatting
"editoctal": 1
}
},
{
"filename": "file_3.bzl",
"formatted": true,
"valid": true,
"warnings": []
},
{
"filename": "file_4.not_bzl",
"formatted": false,
"valid": false,
"warnings": []
}
]
}
当 --format 提供了标记, 构建器总是返回 0 除非有内部
失败或输入参数错误,这意味着输出可以解析为JSON,以及它的 success
字段应用于确定诊断结果是否为正数.