Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 },
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
bcx: &BuildContext<'a, 'gctx>,
) -> CargoResult<Arc<Vec<OutputFile>>> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/cargo/core/compiler/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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;
Expand All @@ -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");
}
Expand Down
28 changes: 13 additions & 15 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 2 additions & 7 deletions src/cargo/ops/cargo_compile/unit_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
}
}
7 changes: 6 additions & 1 deletion tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();

Expand Down