Skip to content

Commit 1ef954e

Browse files
committed
Fix --document-private-items for multiple targets.
Closes #5958.
1 parent c514b94 commit 1ef954e

File tree

7 files changed

+48
-19
lines changed

7 files changed

+48
-19
lines changed

src/bin/cargo/command_prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub trait ArgMatchesExt {
315315
),
316316
target_rustdoc_args: None,
317317
target_rustc_args: None,
318+
local_rustdoc_args: None,
318319
export_dir: None,
319320
};
320321
Ok(opts)

src/bin/cargo/commands/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5151
deps: !args.is_present("no-deps"),
5252
};
5353
let mut compile_opts = args.compile_options(config, mode)?;
54-
compile_opts.target_rustdoc_args = if args.is_present("document-private-items") {
54+
compile_opts.local_rustdoc_args = if args.is_present("document-private-items") {
5555
Some(vec!["--document-private-items".to_string()])
5656
} else {
5757
None

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ pub struct BuildContext<'a, 'cfg: 'a> {
2424
pub resolve: &'a Resolve,
2525
pub profiles: &'a Profiles,
2626
pub build_config: &'a BuildConfig,
27-
/// This is a workaround to carry the extra compiler args for either
28-
/// `rustc` or `rustdoc` given on the command-line for the commands `cargo
29-
/// rustc` and `cargo rustdoc`. These commands only support one target,
30-
/// but we don't want the args passed to any dependencies, so we include
31-
/// the `Unit` corresponding to the top-level target.
32-
pub extra_compiler_args: Option<(Unit<'a>, Vec<String>)>,
27+
/// Extra compiler args for either `rustc` or `rustdoc`.
28+
pub extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
3329
pub packages: &'a PackageSet<'cfg>,
3430

3531
/// Information about the compiler
@@ -51,7 +47,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
5147
config: &'cfg Config,
5248
build_config: &'a BuildConfig,
5349
profiles: &'a Profiles,
54-
extra_compiler_args: Option<(Unit<'a>, Vec<String>)>,
50+
extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
5551
) -> CargoResult<BuildContext<'a, 'cfg>> {
5652
let incremental_env = match env::var("CARGO_INCREMENTAL") {
5753
Ok(v) => Some(v == "1"),
@@ -200,12 +196,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
200196
}
201197

202198
pub fn extra_args_for(&self, unit: &Unit<'a>) -> Option<&Vec<String>> {
203-
if let Some((ref args_unit, ref args)) = self.extra_compiler_args {
204-
if args_unit == unit {
205-
return Some(args);
206-
}
207-
}
208-
None
199+
self.extra_compiler_args.get(unit)
209200
}
210201

211202
/// Return the list of filenames read by cargo to generate the BuildContext

src/cargo/ops/cargo_clean.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fs;
23
use std::path::Path;
34

@@ -97,7 +98,7 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
9798
opts.config,
9899
&build_config,
99100
profiles,
100-
None,
101+
HashMap::new(),
101102
)?;
102103
let mut cx = Context::new(config, &bcx)?;
103104
cx.prepare_units(None, &units)?;

src/cargo/ops/cargo_compile.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ pub struct CompileOptions<'a> {
5353
/// Filter to apply to the root package to select which targets will be
5454
/// built.
5555
pub filter: CompileFilter,
56-
/// Extra arguments to be passed to rustdoc (for main crate and dependencies)
56+
/// Extra arguments to be passed to rustdoc (single target only)
5757
pub target_rustdoc_args: Option<Vec<String>>,
5858
/// The specified target will be compiled with all the available arguments,
5959
/// note that this only accounts for the *final* invocation of rustc
6060
pub target_rustc_args: Option<Vec<String>>,
61+
/// Extra arguments passed to all selected targets for rustdoc.
62+
pub local_rustdoc_args: Option<Vec<String>>,
6163
/// The directory to copy final artifacts to. Note that even if `out_dir` is
6264
/// set, a copy of artifacts still could be found a `target/(debug\release)`
6365
/// as usual.
@@ -80,6 +82,7 @@ impl<'a> CompileOptions<'a> {
8082
},
8183
target_rustdoc_args: None,
8284
target_rustc_args: None,
85+
local_rustdoc_args: None,
8386
export_dir: None,
8487
})
8588
}
@@ -219,6 +222,7 @@ pub fn compile_ws<'a>(
219222
ref filter,
220223
ref target_rustdoc_args,
221224
ref target_rustc_args,
225+
ref local_rustdoc_args,
222226
ref export_dir,
223227
} = *options;
224228

@@ -265,8 +269,6 @@ pub fn compile_ws<'a>(
265269
let profiles = ws.profiles();
266270
profiles.validate_packages(&mut config.shell(), &packages)?;
267271

268-
let mut extra_compiler_args = None;
269-
270272
let units = generate_targets(
271273
ws,
272274
profiles,
@@ -277,6 +279,7 @@ pub fn compile_ws<'a>(
277279
build_config,
278280
)?;
279281

282+
let mut extra_compiler_args = HashMap::new();
280283
if let Some(args) = extra_args {
281284
if units.len() != 1 {
282285
bail!(
@@ -286,7 +289,14 @@ pub fn compile_ws<'a>(
286289
extra_args_name
287290
);
288291
}
289-
extra_compiler_args = Some((units[0], args));
292+
extra_compiler_args.insert(units[0], args);
293+
}
294+
if let Some(args) = local_rustdoc_args {
295+
for unit in &units {
296+
if unit.mode.is_doc() {
297+
extra_compiler_args.insert(*unit, args.clone());
298+
}
299+
}
290300
}
291301

292302
let ret = {

src/cargo/ops/cargo_package.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ fn run_verify(ws: &Workspace, tar: &FileLock, opts: &PackageOpts) -> CargoResult
444444
},
445445
target_rustdoc_args: None,
446446
target_rustc_args: None,
447+
local_rustdoc_args: None,
447448
export_dir: None,
448449
},
449450
&exec,

tests/testsuite/doc.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,31 @@ fn doc_private_items() {
12061206
);
12071207
}
12081208

1209+
#[test]
1210+
fn doc_private_ws() {
1211+
let p = project()
1212+
.file(
1213+
"Cargo.toml",
1214+
r#"
1215+
[workspace]
1216+
members = ["a", "b"]
1217+
"#,
1218+
).file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
1219+
.file("a/src/lib.rs", "fn p() {}")
1220+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
1221+
.file("b/src/lib.rs", "fn p2() {}")
1222+
.file("b/src/main.rs", "fn main() {}")
1223+
.build();
1224+
p.cargo("doc --all --bins --lib --document-private-items -v")
1225+
.with_stderr_contains(
1226+
"[RUNNING] `rustdoc [..] a/src/lib.rs [..]--document-private-items[..]",
1227+
).with_stderr_contains(
1228+
"[RUNNING] `rustdoc [..] b/src/lib.rs [..]--document-private-items[..]",
1229+
).with_stderr_contains(
1230+
"[RUNNING] `rustdoc [..] b/src/main.rs [..]--document-private-items[..]",
1231+
).run();
1232+
}
1233+
12091234
const BAD_INTRA_LINK_LIB: &str = r#"
12101235
#![deny(intra_doc_link_resolution_failure)]
12111236

0 commit comments

Comments
 (0)