Buf
$ buf 官方出品 低风险Buf 官方命令行工具,以确定性的 Protobuf 规范检查、向后兼容性破坏检测、格式化与托管代码生成替代繁琐易错的 protoc 调用。原生 --error-format=json 与 buf format --diff --exit-code 极大提升了 Agent 处理 Protobuf 的可靠性。
brew install buf 直接在工作区本地运行 Protobuf 检查、兼容性比对与代码生成,优先使用 buf CLI。
Agent 友好度体检单
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
在 lint、breaking 与 build 中全面支持 --error-format=json,并提供 buf format --diff --exit-code 与 buf config ls-lint-rules --format json。
--error-format=jsonsupported acrosslint,breaking, andbuildbuf format --diff --exit-codeprovides clean dry-run verification
本地 lint、breaking 与 format --diff 严格只读;远程 buf push 及本地写入 format -w/generate 均由独立子命令或标志隔离。
- Clear separation between read-only checks and
-w/generate/push
静态 Go 单二进制,可通过 Homebrew、npm、go install、Docker 与 GitHub Releases 安装,无需外部 protoc。
- Zero external
protocdependency required for linting and breaking checks
以可复现的 buf.yaml / buf.gen.yaml 工作流替代脆弱的 protoc 长参数链。
- Fast Protobuf parsing, breaking-change detection, and remote/local codegen
Buf 官方开源项目(bufbuild/buf),采用 Apache-2.0 协议高频维护。
- Official
bufbuild/bufrepository
buf.build/docs 拥有极佳的 CLI 参考手册与规则文档。
- Official CLI reference at buf.build/docs/reference/cli/buf/
安全边界、鉴权与放权指南
本地 lint/format/generate 无需凭证;若访问 Buf Schema Registry (BSR),凭证保存在 ~/.netrc 或 BUF_TOKEN 环境变量中。
Apache-2.0 开源 CLI,本地使用完全免费(BSR 私有仓库套餐为可选项)。
# Buf (buf) Agent Rules - Always pass `--error-format=json` to `buf lint` and `buf breaking`. - Run `buf format --diff --exit-code` before `buf format -w`, and never push to BSR (`buf push`) without explicit user consent.
Agent 高频安全命令配方
只读查询(安全推荐)
buf lint --error-format=json
依据 buf.yaml 规则校验 .proto 文件,并逐行输出 JSON 诊断。
buf breaking --against '.git#branch=main' --error-format=json
以 JSON 检测相对 main 分支的 .proto 协议或源码破坏性变更,不修改文件。
受控写入与预演
buf format --diff --exit-code && buf format -w
先通过 --diff --exit-code 预检格式差异,再按需执行 -w 原地格式化。
核心优势
- +
lint、breaking与build统一支持--error-format=json,且内置独立 Protobuf 编译器。 - + 原生支持 Git 感知的
buf breaking --against '.git#branch=main',精准拦截协议兼容回退。
注意事项与踩坑点
- !
buf.gen.yaml中若引用 BSR 远程依赖或远程插件,需要联网权限(私有模块需BUF_TOKEN)。 - ! 不同代际的仓库可能混用
buf.yamlv1 与 v2 配置格式。