From f13f4d0607c849f0330a65581f527e47df627aae Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 21 Nov 2020 21:19:21 -0500 Subject: [PATCH 1/4] Use `-Z rustdoc-map` instead of rewriting it --- crates/metadata/lib.rs | 3 ++- src/docbuilder/rustwide_builder.rs | 10 ---------- src/utils/cargo_metadata.rs | 32 +++++++----------------------- 3 files changed, 9 insertions(+), 36 deletions(-) diff --git a/crates/metadata/lib.rs b/crates/metadata/lib.rs index 8ceeee651..3f39d9806 100644 --- a/crates/metadata/lib.rs +++ b/crates/metadata/lib.rs @@ -229,7 +229,8 @@ impl Metadata { /// For example, the links may point somewhere different than they would on docs.rs. /// However, rustdoc will see exactly the same code as it would on docs.rs, even counting `cfg`s. pub fn cargo_args(&self, additional_args: &[String], rustdoc_args: &[String]) -> Vec { - let mut cargo_args: Vec = vec!["rustdoc".into(), "--lib".into()]; + let mut cargo_args: Vec = + vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()]; if let Some(features) = &self.features { cargo_args.push("--features".into()); diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index ce53f771e..83592dc6b 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -518,16 +518,6 @@ impl RustwideBuilder { let mut rustdoc_flags = Vec::new(); - for dep in &cargo_metadata.root_dependencies() { - rustdoc_flags.push("--extern-html-root-url".to_string()); - rustdoc_flags.push(format!( - "{}=https://docs.rs/{}/{}", - dep.name.replace("-", "_"), - dep.name, - dep.version - )); - } - rustdoc_flags.extend(vec![ "--resource-suffix".to_string(), format!("-{}", parse_rustc_version(&self.rustc_version)?), diff --git a/src/utils/cargo_metadata.rs b/src/utils/cargo_metadata.rs index c35337ac3..c3573666b 100644 --- a/src/utils/cargo_metadata.rs +++ b/src/utils/cargo_metadata.rs @@ -1,13 +1,11 @@ use crate::error::Result; use rustwide::{cmd::Command, Toolchain, Workspace}; use serde::{Deserialize, Serialize}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::path::Path; pub(crate) struct CargoMetadata { - packages: HashMap, - deps_graph: HashMap>, - root_id: String, + root: Package, } impl CargoMetadata { @@ -31,34 +29,18 @@ impl CargoMetadata { )); }; - // Convert from Vecs to HashMaps and HashSets to get more efficient lookups + let root = metadata.resolve.root; Ok(CargoMetadata { - packages: metadata + root: metadata .packages .into_iter() - .map(|pkg| (pkg.id.clone(), pkg)) - .collect(), - deps_graph: metadata - .resolve - .nodes - .into_iter() - .map(|node| (node.id, node.deps.into_iter().map(|d| d.pkg).collect())) - .collect(), - root_id: metadata.resolve.root, + .find(|pkg| pkg.id == root) + .unwrap(), }) } - pub(crate) fn root_dependencies(&self) -> Vec<&Package> { - let ids = &self.deps_graph[&self.root_id]; - self.packages - .iter() - .filter(|(id, _pkg)| ids.contains(id.as_str())) - .map(|(_id, pkg)| pkg) - .collect() - } - pub(crate) fn root(&self) -> &Package { - &self.packages[&self.root_id] + &self.root } } From 0ac66d113b708a6910f4d86f37044cb090c5109b Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 08:23:10 -0500 Subject: [PATCH 2/4] Use rustdoc-map even without the upstream cargo patch --- src/docbuilder/rustwide_builder.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index 83592dc6b..af17db9ef 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -579,6 +579,12 @@ impl RustwideBuilder { // Add docs.rs specific arguments let mut cargo_args = Vec::new(); + // Show the command line Rustdoc is invoked with + cargo_args.push("--verbose".into()); + // We know that `metadata` unconditionally passes `-Z rustdoc-map`. + // Don't copy paste this, since that fact is not stable and may change in the future. + // Normally we would need -Z unstable-options, but we currently pass that too. + cargo_args.push(r#"--config=doc.extern-map.registries.crates-io="https://docs.rs""#.into()); if let Some(cpu_limit) = self.config.build_cpu_limit { cargo_args.push(format!("-j{}", cpu_limit)); } From 742a10476b1f235ea6c8d7930fbab4603f4e6672 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 08:25:20 -0500 Subject: [PATCH 3/4] Fix metadata tests --- crates/metadata/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/metadata/lib.rs b/crates/metadata/lib.rs index 3f39d9806..656c9975f 100644 --- a/crates/metadata/lib.rs +++ b/crates/metadata/lib.rs @@ -522,7 +522,12 @@ mod test_calculations { use super::*; fn default_cargo_args() -> Vec { - vec!["rustdoc".into(), "--lib".into(), "--".into()] + vec![ + "rustdoc".into(), + "--lib".into(), + "-Zrustdoc-map".into(), + "--".into(), + ] } #[test] @@ -575,6 +580,7 @@ mod test_calculations { let expected_args = vec![ "rustdoc".into(), "--lib".into(), + "-Zrustdoc-map".into(), "--features".into(), String::new(), "--".into(), @@ -589,6 +595,7 @@ mod test_calculations { let expected_args = vec![ String::from("rustdoc"), "--lib".into(), + "-Zrustdoc-map".into(), "--features".into(), "some_feature".into(), "--".into(), @@ -603,6 +610,7 @@ mod test_calculations { let expected_args = vec![ String::from("rustdoc"), "--lib".into(), + "-Zrustdoc-map".into(), "--features".into(), "feature1 feature2".into(), "--".into(), @@ -624,6 +632,7 @@ mod test_calculations { let expected_args = vec![ String::from("rustdoc"), "--lib".into(), + "-Zrustdoc-map".into(), "--".into(), "-Z".into(), "unstable-options".into(), @@ -642,6 +651,7 @@ mod test_calculations { let expected_args = vec![ String::from("rustdoc"), "--lib".into(), + "-Zrustdoc-map".into(), "-Z".into(), "unstable-options".into(), "--config".into(), From fc27cd80fe7a324287339b1c4963bfb09cbb091f Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 14:13:23 -0500 Subject: [PATCH 4/4] Pass -Zunstable-args in case `metadata` doesn't --- src/docbuilder/rustwide_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index af17db9ef..98850ece6 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -583,7 +583,7 @@ impl RustwideBuilder { cargo_args.push("--verbose".into()); // We know that `metadata` unconditionally passes `-Z rustdoc-map`. // Don't copy paste this, since that fact is not stable and may change in the future. - // Normally we would need -Z unstable-options, but we currently pass that too. + cargo_args.push("-Zunstable-options".into()); cargo_args.push(r#"--config=doc.extern-map.registries.crates-io="https://docs.rs""#.into()); if let Some(cpu_limit) = self.config.build_cpu_limit { cargo_args.push(format!("-j{}", cpu_limit));