diff --git a/crates/uv-distribution-types/src/resolution.rs b/crates/uv-distribution-types/src/resolution.rs index 793e0db74809..7e300b454109 100644 --- a/crates/uv-distribution-types/src/resolution.rs +++ b/crates/uv-distribution-types/src/resolution.rs @@ -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 { diff --git a/crates/uv-resolver/src/lock/target.rs b/crates/uv-resolver/src/lock/target.rs index 1871893d5110..fea518e45dc7 100644 --- a/crates/uv-resolver/src/lock/target.rs +++ b/crates/uv-resolver/src/lock/target.rs @@ -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 } }; @@ -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 } }; diff --git a/crates/uv-resolver/src/pubgrub/dependencies.rs b/crates/uv-resolver/src/pubgrub/dependencies.rs index ee9ac491ebc8..69be28de1ed7 100644 --- a/crates/uv-resolver/src/pubgrub/dependencies.rs +++ b/crates/uv-resolver/src/pubgrub/dependencies.rs @@ -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, @@ -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 + 'a { // Add the package, plus any extra variants. @@ -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 { @@ -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(), diff --git a/crates/uv-resolver/src/resolution/output.rs b/crates/uv-resolver/src/resolution/output.rs index e9d01e171458..448cf7870979 100644 --- a/crates/uv-resolver/src/resolution/output.rs +++ b/crates/uv-resolver/src/resolution/output.rs @@ -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, diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index 09b08debe0d1..bf66c9449cd6 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -1259,7 +1259,9 @@ impl ResolverState ResolverState = requirements .iter() .flat_map(|requirement| { - PubGrubDependency::from_requirement(requirement, Some(name)) + PubGrubDependency::from_requirement(requirement, dev.as_ref(), Some(name)) }) .collect(); @@ -1577,6 +1579,12 @@ impl ResolverState>(); + // 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() @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/crates/uv/tests/it/sync.rs b/crates/uv/tests/it/sync.rs index 770042d9fb08..aabcdf8691d4 100644 --- a/crates/uv/tests/it/sync.rs +++ b/crates/uv/tests/it/sync.rs @@ -1863,6 +1863,341 @@ fn sync_default_groups() -> Result<()> { Ok(()) } +/// Sync with `--only-group`, where the group includes a workspace member. +#[test] +fn sync_group_member() -> Result<()> { + let context = TestContext::new("3.12"); + + // Create a workspace. + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig>=2"] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + + [dependency-groups] + foo = ["child", "typing-extensions>=4"] + + [tool.uv.workspace] + members = ["child"] + + [tool.uv.sources] + child = { workspace = true } + "#, + )?; + + // Add a workspace member. + context + .temp_dir + .child("child") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "child" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig>=1"] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + "#, + )?; + + // Generate a lockfile. + context.lock().assert().success(); + + uv_snapshot!(context.filters(), context.sync().arg("--only-group").arg("foo"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 4 packages in [TIME] + Prepared 3 packages in [TIME] + Installed 3 packages in [TIME] + + child==0.1.0 (from file://[TEMP_DIR]/child) + + iniconfig==2.0.0 + + typing-extensions==4.10.0 + "###); + + Ok(()) +} + +/// Sync with `--only-group`, where the group includes a legacy non-`[project]` workspace member. +#[test] +fn sync_group_legacy_non_project_member() -> Result<()> { + let context = TestContext::new("3.12"); + + // Create a workspace. + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [dependency-groups] + foo = ["child", "typing-extensions>=4"] + + [tool.uv.workspace] + members = ["child"] + + [tool.uv.sources] + child = { workspace = true } + "#, + )?; + + // Add a workspace member. + context + .temp_dir + .child("child") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "child" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig>=1"] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + "#, + )?; + + // Generate a lockfile. + uv_snapshot!(context.filters(), context.lock(), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + "###); + + let lock = context.read("uv.lock"); + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + lock, @r###" + version = 1 + requires-python = ">=3.12" + + [options] + exclude-newer = "2024-03-25T00:00:00Z" + + [manifest] + members = [ + "child", + ] + requirements = [ + { name = "child" }, + { name = "typing-extensions", specifier = ">=4" }, + ] + + [[package]] + name = "child" + version = "0.1.0" + source = { editable = "child" } + dependencies = [ + { name = "iniconfig" }, + ] + + [package.metadata] + requires-dist = [{ name = "iniconfig", specifier = ">=1" }] + + [[package]] + name = "iniconfig" + version = "2.0.0" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/d7/4b/cbd8e699e64a6f16ca3a8220661b5f83792b3017d0f79807cb8708d33913/iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3", size = 4646 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 }, + ] + + [[package]] + name = "typing-extensions" + version = "4.10.0" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/16/3a/0d26ce356c7465a19c9ea8814b960f8a36c3b0d07c323176620b7b483e44/typing_extensions-4.10.0.tar.gz", hash = "sha256:b0abd7c89e8fb96f98db18d86106ff1d90ab692004eb746cf6eda2682f91b3cb", size = 77558 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/f9/de/dc04a3ea60b22624b51c703a84bbe0184abcd1d0b9bc8074b5d6b7ab90bb/typing_extensions-4.10.0-py3-none-any.whl", hash = "sha256:69b1a937c3a517342112fb4c6df7e72fc39a38e7891a5730ed4985b5214b5475", size = 33926 }, + ] + "### + ); + }); + + uv_snapshot!(context.filters(), context.sync().arg("--only-group").arg("foo"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + Prepared 3 packages in [TIME] + Installed 3 packages in [TIME] + + child==0.1.0 (from file://[TEMP_DIR]/child) + + iniconfig==2.0.0 + + typing-extensions==4.10.0 + "###); + + Ok(()) +} + +/// Sync with `--only-group`, where the group includes the project itself. +#[test] +fn sync_group_self() -> Result<()> { + let context = TestContext::new("3.12"); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig>=2"] + + [project.optional-dependencies] + test = ["idna>=3"] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + + [dependency-groups] + foo = ["project", "typing-extensions>=4"] + bar = ["project[test]"] + "#, + )?; + + // Generate a lockfile. + uv_snapshot!(context.filters(), context.lock(), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 4 packages in [TIME] + "###); + + let lock = context.read("uv.lock"); + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + lock, @r###" + version = 1 + requires-python = ">=3.12" + + [options] + exclude-newer = "2024-03-25T00:00:00Z" + + [[package]] + name = "idna" + version = "3.6" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/bf/3f/ea4b9117521a1e9c50344b909be7886dd00a519552724809bb1f486986c2/idna-3.6.tar.gz", hash = "sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca", size = 175426 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/c2/e7/a82b05cf63a603df6e68d59ae6a68bf5064484a0718ea5033660af4b54a9/idna-3.6-py3-none-any.whl", hash = "sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f", size = 61567 }, + ] + + [[package]] + name = "iniconfig" + version = "2.0.0" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/d7/4b/cbd8e699e64a6f16ca3a8220661b5f83792b3017d0f79807cb8708d33913/iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3", size = 4646 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 }, + ] + + [[package]] + name = "project" + version = "0.1.0" + source = { editable = "." } + dependencies = [ + { name = "iniconfig" }, + ] + + [package.optional-dependencies] + test = [ + { name = "idna" }, + ] + + [package.dev-dependencies] + bar = [ + { name = "project", extra = ["test"] }, + ] + foo = [ + { name = "project" }, + { name = "typing-extensions" }, + ] + + [package.metadata] + requires-dist = [ + { name = "idna", marker = "extra == 'test'", specifier = ">=3" }, + { name = "iniconfig", specifier = ">=2" }, + ] + + [package.metadata.requires-dev] + bar = [{ name = "project", extras = ["test"] }] + foo = [ + { name = "project" }, + { name = "typing-extensions", specifier = ">=4" }, + ] + + [[package]] + name = "typing-extensions" + version = "4.10.0" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/16/3a/0d26ce356c7465a19c9ea8814b960f8a36c3b0d07c323176620b7b483e44/typing_extensions-4.10.0.tar.gz", hash = "sha256:b0abd7c89e8fb96f98db18d86106ff1d90ab692004eb746cf6eda2682f91b3cb", size = 77558 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/f9/de/dc04a3ea60b22624b51c703a84bbe0184abcd1d0b9bc8074b5d6b7ab90bb/typing_extensions-4.10.0-py3-none-any.whl", hash = "sha256:69b1a937c3a517342112fb4c6df7e72fc39a38e7891a5730ed4985b5214b5475", size = 33926 }, + ] + "### + ); + }); + + uv_snapshot!(context.filters(), context.sync().arg("--only-group").arg("foo"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 4 packages in [TIME] + Prepared 3 packages in [TIME] + Installed 3 packages in [TIME] + + iniconfig==2.0.0 + + project==0.1.0 (from file://[TEMP_DIR]/) + + typing-extensions==4.10.0 + "###); + + uv_snapshot!(context.filters(), context.sync().arg("--only-group").arg("bar"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 4 packages in [TIME] + Prepared 1 package in [TIME] + Uninstalled 1 package in [TIME] + Installed 1 package in [TIME] + + idna==3.6 + - typing-extensions==4.10.0 + "###); + + Ok(()) +} + /// Regression test for . /// /// Previously, we would read metadata statically from pyproject.toml and write that to `uv.lock`. In