Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

rustdoc: Tracking issue for command-line option --doctest-build-arg #134172

Open

Description

@GuillaumeGomez

A command line option to pass program arguments to the underlying compiler building doctests.

Original issue motivating this feature: #67533.

Note

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.

A tracking issue is however not meant for large scale discussion, questions, or bug reports.
Instead, open a dedicated issue for the specific matter or post in the #t-rustdoc Zulip channel.

Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.

Steps

Unresolved Questions

  • Should argument parsing be closer to what already exists for RUSTFLAGS (whitespace splitting and nothing else) or should it be closer to how shell parses input (take into account escaped characters and quote characters)? Or should we offer two options, one that works like RUSTFLAGS for human users and one that works like CARGO_ENCODED_RUSTLFAGS for tool usage? Update: #137096 (comment) changed the requirements and #139863 the design!
  • From #139863: Even though we don't process the argument passed to --doctest-build-arg, we end up passing it via an argument file (rustc @argfile) which delimits arguments by line break (LF or CRLF, via) meaning ultimately the arguments still get split which is unfortunate. Is there are good way to resolve this (obvious but intrusive solution: smh signal to rustc not to split inside the argsfile)?

Implementation History

Activity

  1. added
    C-tracking-issue
    T-rustdoc
    on Dec 11, 2024
  2. added a commit that references this issue on Dec 21, 2024
  3. added a commit that references this issue on Dec 21, 2024
  4. weihanglo commented on Dec 21, 2024

    @weihanglo
    Member

    Considering all linked issues (#67533, #66623, rust-lang/cargo#13697, rust-lang/miri#3869) are actually Cargo-related, I wonder how to integrate this flag to help Cargo users. Cargo doesn't parse RUSTFLAGS so it may not know if --cap-lints=warn is there and pass to --doctest-compilation-args accordingly.

    Also, the feature seems to open a door way bigger than what #67533 was looking for. It accepts arbitrary rustc flags. Mind sharing why propagating -cap-lints isn't under consideration? Maybe there are other use cases for arbitrary flags I am missing. We all know that once we provide more stuff to users, we might have no chance to take it back 😓.

    That said, I don't think this feature on its own is bad. Thank you for working on a fix!!

  5. added
    A-doctests
    A-CLI
    on Apr 15, 2025
  6. added a commit that references this issue on May 9, 2025
  7. added a commit that references this issue on May 9, 2025
  8. added a commit that references this issue on May 9, 2025
  9. added a commit that references this issue on May 10, 2025
  10. changed the title [-]Tracking Issue for rustdoc `--doctest-compilation-args` command line option[/-] [+]Tracking Issue for rustdoc `--doctest-build-arg` command line option[/+] on May 10, 2025
  11. added a commit that references this issue on Sep 25, 2025
  12. added a commit that references this issue on Sep 25, 2025
  13. josephlr commented on Sep 29, 2025

    @josephlr
    Contributor

    I can offer some context on why this feature might be useful if properly integrated into cargo. In getrandom's tests, we want to run our existing tests with the unstable -Zsanitizer=memory flag.

    If we replace instances of:

    cargo test --target=x86_64-unknown-linux-gnu

    with

    RUSTFLAGS="-Zsanitizer=memory" cargo test -Zbuild-std --target=x86_64-unknown-linux-gnu

    our building, linking, and testing work exactly as expected, except for doctests.

    For doctests, as far as I can tell, it's not currently possible to pass -Zsanitizer=memory in all the necessary locations to make doctests work. Specifically, even the following does not work (on the currently nightly):

    RUSTDOCFLAGS="-Zsanitizer=memory --doctest-build-arg=-Zsanitizer=memory" RUSTFLAGS="-Zsanitizer=memory" cargo test --doc -Zbuild-std --target=x86_64-unknown-linux-gn

    giving the following error:

        Finished `test` profile [unoptimized + debuginfo] target(s) in 0.03s
       Doc-tests getrandom
    error: mixing `-Zsanitizer` will cause an ABI mismatch in crate `getrandom`
      |
      = help: the `-Zsanitizer` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely
      = note: unset `-Zsanitizer` in this crate is incompatible with `-Zsanitizer=memory` in dependency `core`
      = help: set `-Zsanitizer=memory` in this crate or unset `-Zsanitizer` in `core`
      = help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=sanitizer` to silence this error
    

    While we can just disable doctests when testing with the sanitizer, I think this provides a good example for making sure the correct build flags are passed everywhere.

    It often seems like when doing anything complicated with RUSTFLAGS or testing in general, the answer is "just don't use doctests". However, I really like doctests, and would like to see them well integrated with the rest of the Rust testing story.

    In an ideal world, setting RUSTFLAGS should work for all build invocations, including those for doctests, and RUSTDOCFLAGS/--doctest-build-arg wouldn't even be necessary. However, I understand that doing this in a backwards-compatible way might be challenging.

  14. added
    B-unstable
    on Oct 10, 2025
  15. changed the title [-]Tracking Issue for rustdoc `--doctest-build-arg` command line option[/-] [+]rustdoc: Tracking issue for command-line option `--doctest-build-arg`[/+] on Oct 10, 2025
  16. added
    S-tracking-needs-to-bake
    on Oct 10, 2025
  17. fmease commented on Nov 14, 2025

    @fmease
    Member

    Thanks for your report. However, I'd like to draw attention to the prominent banner in the issue description, most notably this part:

    A tracking issue is however not meant for large scale discussion, questions, or bug reports.
    Instead, open a dedicated issue for the specific matter or post in the #t-rustdoc Zulip channel.

    Moreover, it's unclear how your issue relates to the experimental command-line option --doctest-build-arg which this issue is tracking, I see no mention of it in your report.

    Regarding your following statement:

    My expectation would be that even if rustdoc ignores RUSTFLAGS, it would do so consistently. Right now, you can apparently end up in a situation where a rustdoc snippet is compiled using RUSTDOCFLAGS, but the modules that it imports are compiled using RUSTFLAGS.

    You are mixing up two different concepts, namely the build system Cargo and the programs rustdoc / rustc. rustdoc doesn't know anything about RUSTDOCFLAGS or RUSTFLAGS and rustc doesn't know anything about RUSTFLAGS, these environment variables are only read & understood by Cargo.

    It's not hard to imagine how this could lead to unsoundness.

    It might indeed lead to library unsoundness (not language unsoundness) if the assumptions made by the library author turn out to be false (either due to a Cargo or rustdoc bug or due to misconceptions of the author).


    It would be great if you could create a separate issue with a minimal reproducer so it's easier for us to see what might be going on. https://github.com/rust-lang/rust/issues is fine, I'll transfer to https://github.com/rust-lang/cargo/issues if needed, once I can visualize the problem since right now I can only speculate that some flags might not get forwarded from rustdoc to the rustc used for compiling doctests or that inlined cross-crate re-exports could play a role here (where rustdoc inlines items defined in a crate that was compiled with rustc instead of rustdoc).

  18. valadaptive commented on Nov 14, 2025

    @valadaptive
    Contributor

    I've removed the original comment. I don't think it needs much discussion and is probably intended behavior, albeit confusing. I think opening an issue would get it closed as a duplicate of this one, so I'll just wait for the broader "passing RUSTFLAGS into rustdoc" problem (is there a tracking issue for that?) to be solved.

    The closest thing I found on Google relating to my issue was https://internals.rust-lang.org/t/can-we-do-something-about-rustflags-rustdocflags/21513, which referenced #67533. That issue was closed by #128780, which is being tracked in this issue. That led me to assume #67533 is "fixed" and you won't accept any more discussion on that issue, so I couldn't really find an ideal place to bring it up.

    After thinking things over, I think I was "reporting" a known issue, albeit in a roundabout way, and we're just waiting on someone to resolve the open questions and stabilize things. Apologies for wasting your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-CLIA-doctestsB-unstableC-tracking-issueS-tracking-needs-to-bakeT-rustdoc

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions