# PR #23700 完整报告

- 仓库：`sgl-project/sglang`
- 标题：ci: consolidate rust + protoc install across workflows
- 合并时间：2026-04-30 04:39
- 原文链接：http://prhub.com.cn/sgl-project/sglang/pull/23700

---

# 执行摘要

- 一句话：统一 CI 中 Rust 与 protoc 安装流程
- 推荐动作：建议合并，该 PR 通过集中化脚本和版本锁定显著提升 CI 可维护性，值得作为 CI 标准化参考。

# 功能与动机

消除 CI 中重复的安装步骤，确保 Rust toolchain 版本一致，并利用缓存减少 CPU job 的构建时间。具体引用 PR body："Eliminates the 6-line Install protoc + Install Rust toolchain stanza copy-pasted across CPU jobs and the duplicate rustup-install line in the gateway script."

# 实现拆解

1. **创建统一安装入口脚本**：`scripts/ci/utils/install_rust_protoc.sh` 封装了 sudo 判断，依次调用 `install_protoc.sh` 和 `install_rustup.sh`，保证幂等性。
2. **锁定 Rust 工具链版本**：在 `rust/sglang-grpc/` 和 `sgl-model-gateway/` 下分别添加 `rust-toolchain.toml`，channel 固定为 `1.90`。
3. **改造 CI 调用点**：
 - `scripts/ci/cuda/ci_install_gateway_dependencies.sh`：移除手写 rustup 安装，改为调用新脚本。
 - `scripts/ci/cuda/ci_install_dependency.sh`：同样替换。
 - `.github/workflows/pr-test.yml`、`rerun-test.yml`、`pr-test-rust.yml`：将原先两步（Install protoc + Install Rust toolchain）合并为一步，并添加 `timeout-minutes: 10`。
4. **添加 Rust 缓存**：在 `pr-test.yml` 的 stage-a-test-cpu 中引入 `Swatinem/rust-cache@v2`，缓存 `rust/sglang-grpc` 构建产物，并通过 `save-if` 限制只有第一个分区保存避免竞态。
5. **精简 protoc 安装依赖**：`install_protoc.sh` 移除了 `wget`、`unzip` 之外的不必要 apt 包（`gcc`、`g++`、`perl`、`make`），因为 protoc 官方包已是静态二进制。

关键文件：
- `scripts/ci/utils/install_rust_protoc.sh`（模块 安装脚本；类别 infra；类型 infrastructure）: 核心统一安装脚本，被所有 workflow 调用，替代原先分散的步骤。
- `scripts/ci/cuda/ci_install_gateway_dependencies.sh`（模块 依赖安装；类别 infra；类型 infrastructure）: 网关依赖脚本，通过调用新统一脚本简化了 Rust 安装部分
- `sgl-model-gateway/rust-toolchain.toml`（模块 Rust 配置；类别 config；类型 configuration）: 为网关 Rust 项目固定工具链版本，确保与 sglang-grpc 一致
- `.github/workflows/pr-test.yml`（模块 CI 配置；类别 infra；类型 infrastructure）: 主 CI 工作流，使用新安装脚本并添加 rust-cache 和超时
- `rust/sglang-grpc/rust-toolchain.toml`（模块 Rust 配置；类别 config；类型 configuration）: 为 sglang-grpc 固定 Rust 版本，与网关项目同步
- `.github/workflows/rerun-test.yml`（模块 CI 配置；类别 infra；类型 infrastructure）: 重跑工作流，同步使用新安装脚本和超时
- `scripts/ci/utils/install_protoc.sh`（模块 安装工具；类别 infra；类型 infrastructure）: 精简了 apt 依赖，移除不必要的编译器
- `scripts/ci/cuda/ci_install_dependency.sh`（模块 依赖安装；类别 infra；类型 infrastructure）: CUDA 依赖安装脚本替换为调用统一脚本

关键符号：未识别

## 关键源码片段

### `scripts/ci/utils/install_rust_protoc.sh`

核心统一安装脚本，被所有 workflow 调用，替代原先分散的步骤。

```bash
#!/bin/bash
# 安装 protoc（全局）和 Rust 工具链（用户），幂等地调用子脚本。
#
# protoc 需要 root 权限（写入 /usr/local），rustup 必须以普通用户运行。
set -euxo pipefail

# 获取脚本所在目录，用于定位子脚本
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 检测是否需要 sudo（若已为 root 则无需）
if [ "$(id -u)" = "0" ]; then
  SUDO=""
elif command -v sudo >/dev/null 2>&1; then
  SUDO="sudo"
else
  SUDO=""
fi

# 先安装 protoc（需要 root），再安装 rustup（普通用户）
${SUDO} bash "${SCRIPT_DIR}/install_protoc.sh"
bash "${SCRIPT_DIR}/install_rustup.sh"

```

# 评论区精华

无公开讨论，PR 合并前无 review 争议。

- 暂无高价值评论线程

# 风险与影响

- 风险：低风险。主要风险点：安装脚本需要 root 权限安装 protoc 但 rustup 需要用户权限，脚本通过 SUDO 判断正确处理；rust-cache 在多分区并行时可能竞态，但已通过 save-if: ${{ matrix.partition == 0 }} 限制。
- 影响：仅影响 CI 构建流程，对用户功能无影响。统一后降低 CI 维护成本，toolchain 版本一致减少因版本差异导致的构建失败。
- 风险标记：暂无

# 关联脉络

- 暂无明显关联 PR