Skip to content

Commit

Permalink
cargo doc --open always respect request_kind
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Mar 8, 2023
1 parent 234d9f6 commit 83863ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Compilation<'cfg> {
pub cdylibs: Vec<UnitOutput>,

/// The crate names of the root units specified on the command-line.
pub root_crate_names: Vec<String>,
pub root_crate_names: Vec<(String, CompileKind)>,

/// All directories for the output of native build commands.
///
Expand Down
9 changes: 6 additions & 3 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
}
self.primary_packages
.extend(self.bcx.roots.iter().map(|u| u.pkg.package_id()));
self.compilation
.root_crate_names
.extend(self.bcx.roots.iter().map(|u| u.target.crate_name()));
self.compilation.root_crate_names.extend(
self.bcx
.roots
.iter()
.map(|u| (u.target.crate_name(), u.kind.clone())),
);

self.record_units_requiring_metadata();

Expand Down
16 changes: 13 additions & 3 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
let compilation = ops::compile(ws, &options.compile_opts)?;

if options.open_result {
let name = &compilation
// The open behavior is as follows:
// cargo doc --open:
// - Pick the first root unit that was built for host.
// - If none found, pick the first one(whatever it's target is).
// cargo doc --target TARGET --open:
// - Pick the first root unit for the given target.
// - If none found, pick the first one(whatever it's target is).
let request_kind = options.compile_opts.build_config.single_requested_kind()?;
let (name, kind) = &compilation
.root_crate_names
.get(0)
.iter()
.find(|(_, kind)| *kind == request_kind)
.or_else(|| compilation.root_crate_names.get(0))
.ok_or_else(|| anyhow::anyhow!("no crates with documentation"))?;
let kind = options.compile_opts.build_config.single_requested_kind()?;

let path = compilation.root_output[&kind]
.with_file_name("doc")
.join(&name)
Expand Down

0 comments on commit 83863ea

Please sign in to comment.