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

Allow dependency groups to include the containing package #9385

Merged
merged 1 commit into from
Nov 26, 2024
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
10 changes: 10 additions & 0 deletions crates/uv-distribution-types/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ pub enum Node {
},
}

impl Node {
/// Returns `true` if the node should be installed.
pub fn install(&self) -> bool {
match self {
Self::Root => false,
Self::Dist { install, .. } => *install,
}
}
}

/// An edge in the resolution graph, along with the marker that must be satisfied to traverse it.
#[derive(Debug, Clone)]
pub enum Edge {
Expand Down
19 changes: 19 additions & 0 deletions crates/uv-resolver/src/lock/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,19 @@ impl<'env> InstallTarget<'env> {
index
}
Entry::Occupied(entry) => {
// Critically, if the package is already in the graph, then it's a workspace
// member. If it was omitted due to, e.g., `--only-dev`, but is itself
// referenced as a development dependency, then we need to re-enable it.
let index = *entry.get();
let node = &mut petgraph[index];
if !dev.prod() {
*node = self.package_to_node(
dep_dist,
tags,
build_options,
install_options,
)?;
}
index
}
};
Expand Down Expand Up @@ -314,7 +326,14 @@ impl<'env> InstallTarget<'env> {
index
}
Entry::Occupied(entry) => {
// Critically, if the package is already in the graph, then it's a workspace
// member. If it was omitted due to, e.g., `--only-dev`, but is itself
// referenced as a development dependency, then we need to re-enable it.
let index = *entry.get();
let node = &mut petgraph[index];
if !dev.prod() {
*node = self.package_to_node(dist, tags, build_options, install_options)?;
}
index
}
};
Expand Down
22 changes: 14 additions & 8 deletions crates/uv-resolver/src/pubgrub/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter;
use pubgrub::Ranges;
use tracing::warn;

use uv_normalize::{ExtraName, PackageName};
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pep440::{Version, VersionSpecifiers};
use uv_pypi_types::{
ParsedArchiveUrl, ParsedDirectoryUrl, ParsedGitUrl, ParsedPathUrl, ParsedUrl, Requirement,
Expand All @@ -29,6 +29,7 @@ pub(crate) struct PubGrubDependency {
impl PubGrubDependency {
pub(crate) fn from_requirement<'a>(
requirement: &'a Requirement,
dev: Option<&'a GroupName>,
source_name: Option<&'a PackageName>,
) -> impl Iterator<Item = Self> + 'a {
// Add the package, plus any extra variants.
Expand All @@ -45,9 +46,11 @@ impl PubGrubDependency {
match &*package {
PubGrubPackageInner::Package { name, .. } => {
// Detect self-dependencies.
if source_name.is_some_and(|source_name| source_name == name) {
warn!("{name} has a dependency on itself");
return None;
if dev.is_none() {
if source_name.is_some_and(|source_name| source_name == name) {
warn!("{name} has a dependency on itself");
return None;
}
}

Some(PubGrubDependency {
Expand All @@ -64,10 +67,13 @@ impl PubGrubDependency {
url,
}),
PubGrubPackageInner::Extra { name, .. } => {
debug_assert!(
!source_name.is_some_and(|source_name| source_name == name),
"extras not flattened for {name}"
);
// Detect self-dependencies.
if dev.is_none() {
debug_assert!(
!source_name.is_some_and(|source_name| source_name == name),
"extras not flattened for {name}"
);
}
Some(PubGrubDependency {
package: package.clone(),
version: version.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-resolver/src/resolution/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Display for ResolutionGraphNode {
}
}

#[derive(Eq, PartialEq, Hash)]
#[derive(Debug, Eq, PartialEq, Hash)]
struct PackageRef<'a> {
package_name: &'a PackageName,
version: &'a Version,
Expand Down
28 changes: 22 additions & 6 deletions crates/uv-resolver/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,9 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag

requirements
.iter()
.flat_map(|requirement| PubGrubDependency::from_requirement(requirement, None))
.flat_map(|requirement| {
PubGrubDependency::from_requirement(requirement, None, None)
})
.collect()
}
PubGrubPackageInner::Package {
Expand Down Expand Up @@ -1445,7 +1447,7 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag
let mut dependencies: Vec<_> = requirements
.iter()
.flat_map(|requirement| {
PubGrubDependency::from_requirement(requirement, Some(name))
PubGrubDependency::from_requirement(requirement, dev.as_ref(), Some(name))
})
.collect();

Expand Down Expand Up @@ -1577,6 +1579,12 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag
.requirements_for_extra(regular_and_dev_dependencies, extra, env, python_requirement)
.collect::<Vec<_>>();

// Dependency groups can include the project itself, so no need to flatten recursive
// dependencies.
if dev.is_some() {
return requirements;
}

// Check if there are recursive self inclusions and we need to go into the expensive branch.
if !requirements
.iter()
Expand Down Expand Up @@ -2416,7 +2424,9 @@ impl ForkState {
dev: ref dependency_dev,
..
} => {
if self_name.is_some_and(|self_name| self_name == dependency_name) {
if self_dev.is_none()
&& self_name.is_some_and(|self_name| self_name == dependency_name)
{
continue;
}
let to_url = self.fork_urls.get(dependency_name);
Expand Down Expand Up @@ -2444,7 +2454,9 @@ impl ForkState {
marker: ref dependency_marker,
..
} => {
if self_name.is_some_and(|self_name| self_name == dependency_name) {
if self_dev.is_none()
&& self_name.is_some_and(|self_name| self_name == dependency_name)
{
continue;
}
let to_url = self.fork_urls.get(dependency_name);
Expand Down Expand Up @@ -2473,7 +2485,9 @@ impl ForkState {
marker: ref dependency_marker,
..
} => {
if self_name.is_some_and(|self_name| self_name == dependency_name) {
if self_dev.is_none()
&& self_name.is_some_and(|self_name| self_name == dependency_name)
{
continue;
}
let to_url = self.fork_urls.get(dependency_name);
Expand Down Expand Up @@ -2502,7 +2516,9 @@ impl ForkState {
marker: ref dependency_marker,
..
} => {
if self_name.is_some_and(|self_name| self_name == dependency_name) {
if self_dev.is_none()
&& self_name.is_some_and(|self_name| self_name == dependency_name)
{
continue;
}
let to_url = self.fork_urls.get(dependency_name);
Expand Down
Loading
Loading