Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use -Z rustdoc-map instead of rewriting it #1188

Merged
merged 4 commits into from
Nov 22, 2020
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
15 changes: 13 additions & 2 deletions crates/metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let mut cargo_args: Vec<String> = vec!["rustdoc".into(), "--lib".into()];
let mut cargo_args: Vec<String> =
vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()];

if let Some(features) = &self.features {
cargo_args.push("--features".into());
Expand Down Expand Up @@ -521,7 +522,12 @@ mod test_calculations {
use super::*;

fn default_cargo_args() -> Vec<String> {
vec!["rustdoc".into(), "--lib".into(), "--".into()]
vec![
"rustdoc".into(),
"--lib".into(),
"-Zrustdoc-map".into(),
"--".into(),
]
}

#[test]
Expand Down Expand Up @@ -574,6 +580,7 @@ mod test_calculations {
let expected_args = vec![
"rustdoc".into(),
"--lib".into(),
"-Zrustdoc-map".into(),
"--features".into(),
String::new(),
"--".into(),
Expand All @@ -588,6 +595,7 @@ mod test_calculations {
let expected_args = vec![
String::from("rustdoc"),
"--lib".into(),
"-Zrustdoc-map".into(),
"--features".into(),
"some_feature".into(),
"--".into(),
Expand All @@ -602,6 +610,7 @@ mod test_calculations {
let expected_args = vec![
String::from("rustdoc"),
"--lib".into(),
"-Zrustdoc-map".into(),
"--features".into(),
"feature1 feature2".into(),
"--".into(),
Expand All @@ -623,6 +632,7 @@ mod test_calculations {
let expected_args = vec![
String::from("rustdoc"),
"--lib".into(),
"-Zrustdoc-map".into(),
"--".into(),
"-Z".into(),
"unstable-options".into(),
Expand All @@ -641,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(),
Expand Down
16 changes: 6 additions & 10 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?),
Expand Down Expand Up @@ -589,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.
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));
}
Expand Down
32 changes: 7 additions & 25 deletions src/utils/cargo_metadata.rs
Original file line number Diff line number Diff line change
@@ -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<String, Package>,
deps_graph: HashMap<String, HashSet<String>>,
root_id: String,
root: Package,
}

impl CargoMetadata {
Expand All @@ -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
}
}

Expand Down