diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 4fc51bba416..bec2c914bd4 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -176,10 +176,7 @@ pub enum CompileMode { /// a test. Check { test: bool }, /// Document with `rustdoc`. - /// - /// If `deps` is true, then it will also document all dependencies. - /// if `json` is true, the documentation output is in json format. - Doc { deps: bool, json: bool }, + Doc, /// Test with `rustdoc`. Doctest, /// Scrape for function calls by `rustdoc`. @@ -271,7 +268,7 @@ impl CompileMode { pub enum UserIntent { /// Build benchmark binaries, e.g., `cargo bench` Bench, - /// Build binaries and libraray, e.g., `cargo run`, `cargo install`, `cargo build`. + /// Build binaries and libraries, e.g., `cargo run`, `cargo install`, `cargo build`. Build, /// Perform type-check, e.g., `cargo check`. Check { test: bool }, @@ -292,6 +289,16 @@ impl UserIntent { matches!(self, UserIntent::Doc { .. }) } + /// User wants rustdoc output in JSON format. + pub fn wants_doc_json_output(self) -> bool { + matches!(self, UserIntent::Doc { json: true, .. }) + } + + /// User wants to document also for dependencies. + pub fn wants_deps_docs(self) -> bool { + matches!(self, UserIntent::Doc { deps: true, .. }) + } + /// Returns `true` if this is any type of test (test, benchmark, doc test, or /// check test). pub fn is_any_test(self) -> bool { diff --git a/src/cargo/core/compiler/build_runner/compilation_files.rs b/src/cargo/core/compiler/build_runner/compilation_files.rs index 865d150ff9a..1892e8775bb 100644 --- a/src/cargo/core/compiler/build_runner/compilation_files.rs +++ b/src/cargo/core/compiler/build_runner/compilation_files.rs @@ -448,8 +448,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { bcx: &BuildContext<'a, 'gctx>, ) -> CargoResult>> { let ret = match unit.mode { - CompileMode::Doc { json, .. } => { - let path = if json { + CompileMode::Doc => { + let path = if bcx.build_config.intent.wants_doc_json_output() { self.out_dir(unit) .join(format!("{}.json", unit.target.crate_name())) } else { diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index be11255dd2b..baa7ad7d625 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -869,7 +869,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu build_deps_args(&mut rustdoc, build_runner, unit)?; rustdoc::add_root_urls(build_runner, unit, &mut rustdoc)?; - rustdoc::add_output_format(build_runner, unit, &mut rustdoc)?; + rustdoc::add_output_format(build_runner, &mut rustdoc)?; if let Some(args) = build_runner.bcx.extra_args_for(unit) { rustdoc.args(args); diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index 7a856fe1bc5..ad56137c0e3 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -12,8 +12,6 @@ use std::fmt; use std::hash; use url::Url; -use super::CompileMode; - const DOCS_RS_URL: &'static str = "https://docs.rs/"; /// Mode used for `std`. This is for unstable feature [`-Zrustdoc-map`][1]. @@ -250,7 +248,6 @@ pub fn add_root_urls( /// [1]: https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html?highlight=output-format#-w--output-format-output-format pub fn add_output_format( build_runner: &BuildRunner<'_, '_>, - unit: &Unit, rustdoc: &mut ProcessBuilder, ) -> CargoResult<()> { let gctx = build_runner.bcx.gctx; @@ -259,7 +256,7 @@ pub fn add_output_format( return Ok(()); } - if let CompileMode::Doc { json: true, .. } = unit.mode { + if build_runner.bcx.build_config.intent.wants_doc_json_output() { rustdoc.arg("-Zunstable-options"); rustdoc.arg("--output-format=json"); } diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index dd916282348..3255c820861 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -628,21 +628,19 @@ fn compute_deps_doc( IS_NO_ARTIFACT_DEP, )?; ret.push(lib_unit_dep); - if dep_lib.documented() { - if let CompileMode::Doc { deps: true, .. } = unit.mode { - // Document this lib as well. - let doc_unit_dep = new_unit_dep( - state, - unit, - dep_pkg, - dep_lib, - dep_unit_for, - unit.kind.for_target(dep_lib), - unit.mode, - IS_NO_ARTIFACT_DEP, - )?; - ret.push(doc_unit_dep); - } + if dep_lib.documented() && state.intent.wants_deps_docs() { + // Document this lib as well. + let doc_unit_dep = new_unit_dep( + state, + unit, + dep_pkg, + dep_lib, + dep_unit_for, + unit.kind.for_target(dep_lib), + unit.mode, + IS_NO_ARTIFACT_DEP, + )?; + ret.push(doc_unit_dep); } } diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 12fcabb7cad..6a406cc9227 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -432,7 +432,7 @@ pub fn create_bcx<'a, 'gctx>( // TODO: In theory, Cargo should also dedupe the roots, but I'm uncertain // what heuristics to use in that case. - if matches!(build_config.intent, UserIntent::Doc { deps: true, .. }) { + if build_config.intent.wants_deps_docs() { remove_duplicate_doc(build_config, &units, &mut unit_graph); } diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index aadd3618283..29762c8f8a4 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -11,8 +11,8 @@ use crate::core::dependency::DepKind; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{self, FeaturesFor}; use crate::core::resolver::{HasDevUnits, Resolve}; +use crate::core::Workspace; use crate::core::{FeatureValue, Package, PackageSet, Summary, Target}; -use crate::core::{TargetKind, Workspace}; use crate::util::restricted_names::is_glob_pattern; use crate::util::{closest_msg, CargoResult}; @@ -84,11 +84,6 @@ impl<'a> UnitGenerator<'a, '_> { CompileMode::Test } } - CompileMode::Build => match *target.kind() { - TargetKind::Test => CompileMode::Test, - TargetKind::Bench => CompileMode::Test, - _ => CompileMode::Build, - }, _ => initial_target_mode, }; @@ -781,7 +776,7 @@ fn to_compile_mode(intent: UserIntent) -> CompileMode { UserIntent::Test | UserIntent::Bench => CompileMode::Test, UserIntent::Build => CompileMode::Build, UserIntent::Check { test } => CompileMode::Check { test }, - UserIntent::Doc { deps, json } => CompileMode::Doc { deps, json }, + UserIntent::Doc { .. } => CompileMode::Doc, UserIntent::Doctest => CompileMode::Doctest, } } diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index de1c41561e7..d28a0d1999c 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -1831,6 +1831,9 @@ fn test_run_implicit_example_target() { [[example]] name = "myexm2" test = true + + [profile.test] + panic = "abort" # this should be ignored by default Cargo targets set. "#, ) .file( @@ -1852,11 +1855,13 @@ fn test_run_implicit_example_target() { ) .build(); - // Compiles myexm1 as normal, but does not run it. + // Compiles myexm1 as normal binary (without --test), but does not run it. prj.cargo("test -v") .with_stderr_contains("[RUNNING] `rustc [..]myexm1.rs [..]--crate-type bin[..]") .with_stderr_contains("[RUNNING] `rustc [..]myexm2.rs [..]--test[..]") .with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]") + // profile.test panic settings shouldn't be applied even to myexm1 + .with_stderr_line_without(&["[RUNNING] `rustc --crate-name myexm1"], &["panic=abort"]) .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]") .run();