rust_doc_test: no way to set environment variables for the test runner (generated script does `exec env -`)

未关闭
#4,209 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
rust
领域
build-system

调研方向

从 rust/private/rustdoc_test.bzl 和 rustdoc_test_writer 路径开始,然后跟踪 _construct_writer_arguments 以及现有的 rust_test env 处理逻辑。确认生成的脚本如何组装其 --action_env 变量。完成的标准是 rust_doc_test 接受所请求的环境属性,在运行时环境中保留这些变量,并支持 NIX_LD 示例,同时不削弱现有的环境清除行为。

由索引模型根据 Issue 内容生成。

描述

Problem

rust_doc_test is the only test rule in rules_rust that execs a toolchain tool
(rustdoc) at test runtime rather than build time. The script generated by
rustdoc_test_writer begins with:

exec env - \
CARGO_CRATE_NAME='...' \
... \
'.../process_wrapper' ... '.../bin/rustdoc' ...

env - clears the entire environment and re-sets only the writer's explicit
list (CARGO_*, plus the hardcoded --action_env passthroughs: DEVELOPER_DIR,
PATHEXT, SDKROOT, SYSROOT). There is no user-facing way to add to that list:

  • rust_doc_test has no env attribute (checked on 0.70.0 and current main).
  • Bazel's --test_env doesn't help: the variables reach the test action, but
    the generated script's own env - discards them before rustdoc runs.

Why this matters: remote execution on NixOS-style images

On RBE worker images where the prebuilt Rust toolchain runs through a loader
shim such as nix-ld (the FHS ELF
interpreter path is a shim that resolves the real dynamic loader via the
NIX_LD env var), every rustdoc invocation from the generated script dies
instantly:

[nix-ld] FATAL: panicked at src/main.rs:187:55:
called `Result::unwrap()` on an `Err` value: Posix(2)

Build-time rustc/rustdoc actions on the same workers are fine — build actions
can carry the needed variables via --action_env / the image environment. The
doc-test runner is the one place where the environment is force-cleared with
no escape hatch, so rust_doc_test targets pass locally and hard-fail on RBE.

Feature request

Support the conventional env (and ideally env_inherit) attributes on
rust_doc_test, merged into the variable list the test writer bakes into the
generated script (i.e. flowing into _construct_writer_arguments /
--action_env), with env subject to the usual location/make-variable
expansion like rust_test.env.

That would make this work:

rust_doc_test(
    name = "mycrate_doc_test",
    crate = ":mycrate",
    env = {"NIX_LD": "/lib/my-real-ld.so"},
)

Workaround we settled on

Not really a workaround for doc tests in general: we removed the
rust_doc_test target and restated the invariant our compile_fail doctest
proved (a type must not implement Clone) as an in-crate impl-ambiguity
assertion, keeping the doctest as documentation only (run via
cargo test --doc outside Bazel). Crates that rely on doc tests for real
example coverage have no equivalent escape.

Versions

  • rules_rust 0.70.0 (bzlmod), also inspected rust/private/rustdoc_test.bzl
    on main — attrs are still crate, crate_features, deps,
    proc_macro_deps, rustdoc_flags + privates; no env.
  • Bazel 8, remote execution via REAPI, Linux x86_64 workers on a
    Nix-built container image using nix-ld for FHS binaries.
主要语言
Starlark
星标
857
派生
701
平均合并
4 天 7 小时
30 天内合并 PR
46

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bazelbuild/rules_rust 的其他 Issue

查看 bazelbuild/rules_rust 的全部 Issue

相似的 Issue

更多 Build System Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。