Skip to content

cargo test --doc does not respect CARGO_BUILD_JOBS env variable #97551

Open
@tmpolaczyk

Description

@tmpolaczyk

I originally reported this issue in the cargo repo: rust-lang/cargo#10702 but I was told that "this behaviour is controlled by rustdoc".

Problem

I use a global variable CARGO_BUILD_JOBS=4 because the default configuration results in my machine running out of memory when compiling some rust projects. However recently I noticed that this fails when there are many doc tests in the project, because it looks like all the doc tests are compiled in parallel.

Steps

  1. Find some project with many doc tests
  2. export CARGO_BUILD_JOBS=1
  3. cargo test --doc
  4. In a separate terminal, run pidof rustc and notice how there are as multiple processes, instead of the expected 1.
  5. This results in the tests failing, in my case because the linker is killed because the system has run out of memory.

Possible Solution(s)

In my opinion the correct behavior is to respect CARGO_BUILD_JOBS when compiling the tests.

As a workaround, you can use

cargo test -- --test-threads 1

to limit the number of build jobs to 1, however this also affects normal tests, so running tests will be much slower.

Version

cargo 1.61.0 (a028ae4 2022-04-29)
release: 1.61.0
commit-hash: a028ae42fc1376571de836be702e840ca8e060c2
commit-date: 2022-04-29
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1m)
os: Ubuntu 22.04 (jammy) [64-bit]

Comments

@weihanglo provided helpful comments:

To my understanding, rustdoc collects each doctest and compiles them by invoking rustc on-the-fly1, and that is included as a part of test fn2, which will be executed by libtest.

That tells us why build.jobs (controls cargo-rustc jobserver) doesn't affect parallelism of doctest, but --test-threads does (controls parallelism of libtest).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions