From e4c71e6c6c7612f8e0d40ba5b79322f0883fa500 Mon Sep 17 00:00:00 2001 From: zowoq <59103226+zowoq@users.noreply.github.com> Date: Wed, 9 Sep 2020 21:39:23 +1000 Subject: [PATCH] buildRustPackage: support setting test-threads --- doc/languages-frameworks/rust.section.md | 12 ++++++++++++ pkgs/build-support/rust/default.nix | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 7f9d93216ed..0e1d59e1a95 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -119,6 +119,18 @@ The above are just guidelines, and exceptions may be granted on a case-by-case b However, please check if it's possible to disable a problematic subset of the test suite and leave a comment explaining your reasoning. +#### Setting `test-threads` + +`buildRustPackage` will use parallel test threads by default, +sometimes it may be necessary to disable this so the tests run consecutively. + +```nix +rustPlatform.buildRustPackage { + /* ... */ + cargoParallelTestThreads = false; +} +``` + ### Building a package in `debug` mode By default, `buildRustPackage` will use `release` mode for builds. If a package diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index 0103e064828..f6177ce198d 100644 --- a/pkgs/build-support/rust/default.nix +++ b/pkgs/build-support/rust/default.nix @@ -30,6 +30,8 @@ , cargoVendorDir ? null , checkType ? buildType , depsExtraArgs ? {} +, cargoParallelTestThreads ? true + # Needed to `pushd`/`popd` into a subdir of a tarball if this subdir # contains a Cargo.toml, but isn't part of a workspace (which is e.g. the # case for `rustfmt`/etc from the `rust-sources). @@ -204,11 +206,12 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // { checkPhase = args.checkPhase or (let argstr = "${stdenv.lib.optionalString (checkType == "release") "--release"} --target ${rustTarget} --frozen"; + threads = if cargoParallelTestThreads then "$NIX_BUILD_CORES" else "1"; in '' ${stdenv.lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"} runHook preCheck echo "Running cargo test ${argstr} -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}" - cargo test -j $NIX_BUILD_CORES ${argstr} -- --test-threads=$NIX_BUILD_CORES ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"} + cargo test -j $NIX_BUILD_CORES ${argstr} -- --test-threads=${threads} ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"} runHook postCheck ${stdenv.lib.optionalString (buildAndTestSubdir != null) "popd"} '');