Skip to content

Add unstable documentation dependencies under [doc-dependencies] #10435

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

Closed
wants to merge 7 commits into from
Closed
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
6 changes: 3 additions & 3 deletions benches/benchsuite/benches/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo::core::compiler::{CompileKind, RustcTargetData};
use cargo::core::resolver::features::{CliFeatures, FeatureOpts, FeatureResolver, ForceAllTargets};
use cargo::core::resolver::{HasDevUnits, ResolveBehavior};
use cargo::core::resolver::{HasTransitiveUnits, ResolveBehavior};
use cargo::core::{PackageIdSpec, Workspace};
use cargo::ops::WorkspaceResolve;
use cargo::Config;
Expand Down Expand Up @@ -147,7 +147,7 @@ struct ResolveInfo<'cfg> {
target_data: RustcTargetData<'cfg>,
cli_features: CliFeatures,
specs: Vec<PackageIdSpec>,
has_dev_units: HasDevUnits,
has_dev_units: HasTransitiveUnits,
force_all_targets: ForceAllTargets,
ws_resolve: WorkspaceResolve<'cfg>,
}
Expand Down Expand Up @@ -186,7 +186,7 @@ fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
let cli_features = CliFeatures::from_command_line(&[], false, true).unwrap();
let pkgs = cargo::ops::Packages::Default;
let specs = pkgs.to_package_id_specs(&ws).unwrap();
let has_dev_units = HasDevUnits::Yes;
let has_dev_units = HasTransitiveUnits::Yes;
let force_all_targets = ForceAllTargets::No;
// Do an initial run to download anything necessary so that it does
// not confuse criterion's warmup.
Expand Down
1 change: 1 addition & 0 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ impl fmt::Debug for PrettyPrintRegistry {
d.version_req(),
match d.kind() {
DepKind::Development => "DepKind::Development",
DepKind::Documentation => "DepKind::Documentation",
DepKind::Build => "DepKind::Build",
DepKind::Normal => "DepKind::Normal",
},
Expand Down
11 changes: 9 additions & 2 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<
}

if kinds.is_empty() {
kinds.extend(&["normal", "build", "dev"]);
kinds.extend(&["normal", "build", "dev", "doc"]);
}

(kinds, no_proc_macro)
Expand All @@ -249,8 +249,10 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<
result.insert(EdgeKind::Dep(DepKind::Normal));
result.insert(EdgeKind::Dep(DepKind::Build));
result.insert(EdgeKind::Dep(DepKind::Development));
result.insert(EdgeKind::Dep(DepKind::Documentation));
};
let unknown = |k| {
// FIXME: Should include no-doc when stable
bail!(
"unknown edge kind `{}`, valid values are \
\"normal\", \"build\", \"dev\", \
Expand All @@ -266,8 +268,10 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<
"no-normal" => result.remove(&EdgeKind::Dep(DepKind::Normal)),
"no-build" => result.remove(&EdgeKind::Dep(DepKind::Build)),
"no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)),
"no-doc" => result.remove(&EdgeKind::Dep(DepKind::Documentation)),
"features" => result.insert(EdgeKind::Feature),
"normal" | "build" | "dev" | "all" => {
"normal" | "build" | "dev" | "doc" | "all" => {
// FIXME: Should include no-doc when stable
bail!(
"`{}` dependency kind cannot be mixed with \
\"no-normal\", \"no-build\", or \"no-dev\" \
Expand Down Expand Up @@ -298,6 +302,9 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<
"dev" => {
result.insert(EdgeKind::Dep(DepKind::Development));
}
"doc" => {
result.insert(EdgeKind::Dep(DepKind::Documentation));
}
k => return unknown(k),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::compiler::UnitInterner;
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::profiles::{Profiles, UnitFor};
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::core::resolver::HasDevUnits;
use crate::core::resolver::HasTransitiveUnits;
use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace};
use crate::ops::{self, Packages};
use crate::util::errors::CargoResult;
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn resolve_std<'cfg>(
requested_targets,
&cli_features,
&specs,
HasDevUnits::No,
HasTransitiveUnits::No,
crate::core::resolver::features::ForceAllTargets::No,
)?;
Ok((
Expand Down
33 changes: 21 additions & 12 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct State<'a, 'cfg> {
/// A set of edges in `unit_dependencies` where (a, b) means that the
/// dependency from a to b was added purely because it was a dev-dependency.
/// This is used during `connect_run_custom_build_deps`.
dev_dependency_edges: HashSet<(Unit, Unit)>,
transitive_dependency_edges: HashSet<(Unit, Unit)>,
}

/// A boolean-like to indicate if a `Unit` is an artifact or not.
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn build_unit_dependencies<'a, 'cfg>(
profiles,
interner,
scrape_units,
dev_dependency_edges: HashSet::new(),
transitive_dependency_edges: HashSet::new(),
};

let std_unit_deps = calc_deps_of_std(&mut state, std_roots)?;
Expand Down Expand Up @@ -325,7 +325,7 @@ fn compute_deps(
}
}
}
state.dev_dependency_edges.extend(dev_deps);
state.transitive_dependency_edges.extend(dev_deps);

// If this target is a build script, then what we've collected so far is
// all we need. If this isn't a build script, then it depends on the
Expand Down Expand Up @@ -992,7 +992,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_>) {
// be cyclic we could have cyclic build-script executions.
.filter_map(move |(parent, other)| {
if state
.dev_dependency_edges
.transitive_dependency_edges
.contains(&((*parent).clone(), other.unit.clone()))
{
None
Expand Down Expand Up @@ -1087,14 +1087,23 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
// only applies to test/example or doc/doctest targets.
match dep.kind() {
DepKind::Development => {
if !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
}
}
DepKind::Documentation => {
if !unit.mode.is_doc() && !unit.mode.is_doc_test() {
return false;
}
}
DepKind::Normal | DepKind::Build => {}
}

// If this dependency is only available for certain platforms,
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl ser::Serialize for Dependency {
pub enum DepKind {
Normal,
Development,
Documentation,
Build,
}

Expand All @@ -114,6 +115,7 @@ impl ser::Serialize for DepKind {
match *self {
DepKind::Normal => None,
DepKind::Development => Some("dev"),
DepKind::Documentation => Some("doc"),
DepKind::Build => Some("build"),
}
.serialize(s)
Expand Down Expand Up @@ -369,7 +371,7 @@ impl Dependency {
pub fn is_transitive(&self) -> bool {
match self.inner.kind {
DepKind::Normal | DepKind::Build => true,
DepKind::Development => false,
DepKind::Development | DepKind::Documentation => false,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ features! {

// Allow specifying rustflags directly in a profile
(unstable, profile_rustflags, "", "reference/unstable.html#profile-rustflags-option"),

// Allow specifying dependencies under [doc-dependencies]
(unstable, doc_dependencies, "", "reference/unstable.html#documentation-dependencies"),
}

pub struct Feature {
Expand Down
22 changes: 12 additions & 10 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use toml_edit::easy as toml;
use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::resolver::{HasTransitiveUnits, Resolve};
use crate::core::source::MaybePackage;
use crate::core::{Dependency, Manifest, PackageId, SourceId, Target};
use crate::core::{SourceMap, Summary, Workspace};
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'cfg> PackageSet<'cfg> {
&self,
resolve: &Resolve,
root_ids: &[PackageId],
has_dev_units: HasDevUnits,
has_transitive_units: HasTransitiveUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'cfg>,
force_all_targets: ForceAllTargets,
Expand All @@ -503,7 +503,7 @@ impl<'cfg> PackageSet<'cfg> {
used: &mut BTreeSet<PackageId>,
resolve: &Resolve,
pkg_id: PackageId,
has_dev_units: HasDevUnits,
has_transitive_units: HasTransitiveUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
Expand All @@ -514,7 +514,7 @@ impl<'cfg> PackageSet<'cfg> {
let filtered_deps = PackageSet::filter_deps(
pkg_id,
resolve,
has_dev_units,
has_transitive_units,
requested_kinds,
target_data,
force_all_targets,
Expand All @@ -524,7 +524,7 @@ impl<'cfg> PackageSet<'cfg> {
used,
resolve,
pkg_id,
has_dev_units,
has_transitive_units,
requested_kinds,
target_data,
force_all_targets,
Expand All @@ -543,7 +543,7 @@ impl<'cfg> PackageSet<'cfg> {
&mut to_download,
resolve,
*id,
has_dev_units,
has_transitive_units,
requested_kinds,
target_data,
force_all_targets,
Expand All @@ -560,7 +560,7 @@ impl<'cfg> PackageSet<'cfg> {
ws: &Workspace<'cfg>,
resolve: &Resolve,
root_ids: &[PackageId],
has_dev_units: HasDevUnits,
has_transitive_units: HasTransitiveUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
Expand All @@ -571,7 +571,7 @@ impl<'cfg> PackageSet<'cfg> {
let dep_pkgs_to_deps: Vec<_> = PackageSet::filter_deps(
root_id,
resolve,
has_dev_units,
has_transitive_units,
requested_kinds,
target_data,
force_all_targets,
Expand Down Expand Up @@ -612,7 +612,7 @@ impl<'cfg> PackageSet<'cfg> {
fn filter_deps<'a>(
pkg_id: PackageId,
resolve: &'a Resolve,
has_dev_units: HasDevUnits,
has_transitive_units: HasTransitiveUnits,
requested_kinds: &'a [CompileKind],
target_data: &'a RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
Expand All @@ -621,7 +621,9 @@ impl<'cfg> PackageSet<'cfg> {
.deps(pkg_id)
.filter(move |&(_id, deps)| {
deps.iter().any(|dep| {
if dep.kind() == DepKind::Development && has_dev_units == HasDevUnits::No {
if (dep.kind() == DepKind::Development || dep.kind() == DepKind::Documentation)
&& has_transitive_units == HasTransitiveUnits::No
{
return false;
}
if force_all_targets == ForceAllTargets::No {
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,11 @@ pub fn resolve_features<'b>(
s: &'b Summary,
opts: &'b ResolveOpts,
) -> ActivateResult<(HashSet<InternedString>, Vec<(Dependency, FeaturesSet)>)> {
// First, filter by dev-dependencies.
// First, filter by dev-dependencies or doc-dependencies.
let deps = s.dependencies();
let deps = deps.iter().filter(|d| d.is_transitive() || opts.dev_deps);
let deps = deps
.iter()
.filter(|d| d.is_transitive() || opts.transitive_deps);

let reqs = build_requirements(parent, s, opts)?;
let mut ret = Vec::new();
Expand Down
13 changes: 8 additions & 5 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct FeatureOpts {
/// `cargo test` because the lib may need to be built 3 times instead of
/// twice.
#[derive(Copy, Clone, PartialEq)]
pub enum HasDevUnits {
pub enum HasTransitiveUnits {
Yes,
No,
}
Expand Down Expand Up @@ -163,7 +163,7 @@ impl FeaturesFor {
impl FeatureOpts {
pub fn new(
ws: &Workspace<'_>,
has_dev_units: HasDevUnits,
has_dev_units: HasTransitiveUnits,
force_all_targets: ForceAllTargets,
) -> CargoResult<FeatureOpts> {
let mut opts = FeatureOpts::default();
Expand Down Expand Up @@ -195,7 +195,7 @@ impl FeatureOpts {
enable(&vec!["all".to_string()]).unwrap();
}
}
if let HasDevUnits::Yes = has_dev_units {
if let HasTransitiveUnits::Yes = has_dev_units {
// Dev deps cannot be decoupled when they are in use.
opts.decouple_dev_deps = false;
}
Expand All @@ -206,12 +206,15 @@ impl FeatureOpts {
}

/// Creates a new FeatureOpts for the given behavior.
pub fn new_behavior(behavior: ResolveBehavior, has_dev_units: HasDevUnits) -> FeatureOpts {
pub fn new_behavior(
behavior: ResolveBehavior,
has_dev_units: HasTransitiveUnits,
) -> FeatureOpts {
match behavior {
ResolveBehavior::V1 => FeatureOpts::default(),
ResolveBehavior::V2 => FeatureOpts {
decouple_host_deps: true,
decouple_dev_deps: has_dev_units == HasDevUnits::No,
decouple_dev_deps: has_dev_units == HasTransitiveUnits::No,
ignore_inactive_targets: true,
compare: false,
},
Expand Down
14 changes: 8 additions & 6 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use std::time::{Duration, Instant};

use log::{debug, trace};

use crate::core::dependency::DepKind;
use crate::core::PackageIdSpec;
use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::util::config::Config;
Expand All @@ -69,7 +70,7 @@ use self::types::{FeaturesSet, RcVecIter, RemainingDeps, ResolverProgress};
pub use self::encode::Metadata;
pub use self::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
pub use self::errors::{ActivateError, ActivateResult, ResolveError};
pub use self::features::{CliFeatures, ForceAllTargets, HasDevUnits};
pub use self::features::{CliFeatures, ForceAllTargets, HasTransitiveUnits};
pub use self::resolve::{Resolve, ResolveVersion};
pub use self::types::{ResolveBehavior, ResolveOpts};
pub use self::version_prefs::{VersionOrdering, VersionPreferences};
Expand Down Expand Up @@ -378,7 +379,7 @@ fn activate_deps_loop(

let pid = candidate.package_id();
let opts = ResolveOpts {
dev_deps: false,
transitive_deps: false,
features: RequestedFeatures::DepFeatures {
features: Rc::clone(&features),
uses_default_features: dep.uses_default_features(),
Expand Down Expand Up @@ -1009,13 +1010,14 @@ fn check_cycles(resolve: &Resolve) -> CargoResult<()> {
for id in resolve.iter() {
let map = graph.entry(id).or_insert_with(BTreeMap::new);
for (dep_id, listings) in resolve.deps_not_replaced(id) {
let transitive_dep = listings.iter().find(|d| d.is_transitive());
// We explitly don't use is_transitive() as doc-deps can create cycles.
let not_dev_dep = listings.iter().find(|d| d.kind() != DepKind::Development);

if let Some(transitive_dep) = transitive_dep.cloned() {
map.insert(dep_id, transitive_dep.clone());
if let Some(not_dev_dep) = not_dev_dep.cloned() {
map.insert(dep_id, not_dev_dep.clone());
resolve
.replacement(dep_id)
.map(|p| map.insert(p, transitive_dep));
.map(|p| map.insert(p, not_dev_dep));
}
}
}
Expand Down
Loading