Prhub

#48472 [CI] Add SPDX license header to Rust/Protobuf sources

原始 PR 作者 BugenZhao 合并时间 2026-07-13 17:22 文件变更 253 提交数 2 评论 1 代码增减 +813 / -30

执行摘要

为 Rust/Protobuf 添加 SPDX 头部检查

确保项目所有源码文件(包括 Rust 和 Protobuf)都包含 SPDX License 标识,满足开源合规要求。PR body 明确说明目的为 Extend the SPDX license header pre-commit hook to cover Rust and Protobuf sources as well。

PR 变更直接,不涉及复杂逻辑,但值得学习的是其扩展现有检查器的设计模式(通过 HeaderStyle 统一注释语法)。对于负责 CI/合规的开发者可精读 check_spdx_header.py 的变化。

讨论亮点

PR 无实质性 review 讨论。第一版提交后 pre-commit 检查失败(mergify bot 提醒),作者在第二 commit (backfill headers) 中修复了遗漏,最终通过 njhill 审核合并。

实现拆解

  1. 重构检查工具:在 tools/pre_commit/check_spdx_header.py 中引入 HeaderStyle 数据类,用于定义不同文件类型的注释前缀(#//)及是否保留 shebang。将原有的硬编码常量替换为 LICENSE_TEXTCOPYRIGHT_TEXTFILE_STYLES 映射字典,支持 .py.rs.proto 三种后缀。
  2. 新增辅助函数:添加 file_style()(根据后缀获取 HeaderStyle)、spdx_header()(生成带注释前缀的许可证行和版权行)、header_insertion_index()(计算头部插入位置,处理 shebang 保留)。
  3. 通用化检查与修复逻辑:重写 check_spdx_header_status()add_header(),使其不再依赖硬编码的固定字符串,而是通过 file_style()spdx_header() 动态获取当前文件对应的头部格式,从而支持多文件类型。
  4. 批量添加头部:为仓库中所有 250+ 个 Rust (.rs) 和 Protobuf (.proto) 源文件逐文件添加 SPDX 头部注释(// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright contributors to the vLLM project)。第一个提交执行主要添加,第二个提交(backfill headers)补充了遗漏的文件。
文件 模块 状态 重要度
tools/pre_commit/check_spdx_header.py 工具脚本 modified 8.06
rust/src/chat/examples/external_engine_chat_qwen.rs Rust 聊天模块 modified 4.2
rust/src/engine-core-client/examples/external_engine_logprobs.rs Rust 引擎核心客户端 modified 4.2

关键符号

HeaderStyle file_style spdx_header header_insertion_index check_spdx_header_status add_header

分析完成后,这里会展示 LLM 生成的相对完整源码片段和详细注释。

评论区精华

Pre-commit 检查失败 other

mergify bot 提醒 pre-commit 检查失败,提示运行 `pre-commit run --all-files` 修复。

结论:作者在第二个 commit (backfill headers) 中补充了遗漏的文件,通过检查。 · 已解决

风险与影响

主要风险在于新增的 pre-commit 检查可能会在开发过程中对尚未添加头部的 Rust/Protobuf 文件报错,但因一次性完成了所有文件的头部添加,后续新文件需要开发者注意头部合规。此外,HeaderStyle 的扩展性良好,未来添加新文件类型只需在 FILE_STYLES 字典注册。

影响范围:所有 Rust/Protobuf 源文件的头部合规性。开发者需确保新 Rust/Protobuf 文件包含 SPDX 头部,否则 pre-commit 将报错。影响程度低,因为这是一次性全局添加,且 pre-commit 自动修复。

合规性变更 pre-commit 要求

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论