diff --git a/crates/uv-lock/src/lock/export/cyclonedx_json.rs b/crates/uv-lock/src/lock/export/cyclonedx_json.rs index edf5c6e072e..fe03f5d41ea 100644 --- a/crates/uv-lock/src/lock/export/cyclonedx_json.rs +++ b/crates/uv-lock/src/lock/export/cyclonedx_json.rs @@ -20,7 +20,7 @@ use uv_preview::{Preview, PreviewFeature}; use uv_pypi_types::HashAlgorithm; use uv_warnings::warn_user; -use crate::lock::export::{ExportableRequirement, ExportableRequirements}; +use crate::lock::export::{ExportableRequirement, ExportableRequirements, VirtualPackages}; use crate::lock::{LockErrorKind, Package, PackageId, RegistrySource, Source, WheelWireSource}; use crate::{Installable, LockError}; @@ -360,6 +360,7 @@ pub fn from_lock<'lock>( groups, annotate, install_options, + VirtualPackages::Include, )?; nodes.sort_unstable_by_key(|node| &node.package.id); diff --git a/crates/uv-lock/src/lock/export/mod.rs b/crates/uv-lock/src/lock/export/mod.rs index b11c5f4570f..71b4dcacf11 100644 --- a/crates/uv-lock/src/lock/export/mod.rs +++ b/crates/uv-lock/src/lock/export/mod.rs @@ -1,8 +1,9 @@ use std::collections::VecDeque; -use std::collections::hash_map::Entry; +use std::mem; use either::Either; -use petgraph::graph::NodeIndex; +use petgraph::algo::kosaraju_scc; +use petgraph::graph::{EdgeIndex, NodeIndex}; use petgraph::prelude::EdgeRef; use petgraph::visit::IntoNodeReferences; use petgraph::{Direction, Graph}; @@ -11,12 +12,11 @@ use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet}; use uv_configuration::{ DependencyGroupsWithDefaults, ExtrasSpecificationWithDefaults, InstallOptions, }; -use uv_normalize::{ExtraName, GroupName, PackageName}; -use uv_pep508::MarkerTree; -use uv_pypi_types::ConflictItem; +use uv_normalize::{ExtraName, PackageName}; +use uv_pep508::{ExtraOperator, MarkerExpression, MarkerTree, MarkerValueExtra}; +use uv_pypi_types::{ConflictItem, ConflictSet, Conflicts}; -use uv_resolver_types::graph_ops::Reachable; -use uv_resolver_types::universal_marker::resolve_activated_extras; +use uv_resolver_types::universal_marker::encode_conflict_item; pub use crate::lock::export::metadata::{Metadata, PythonReport}; pub(crate) use crate::lock::export::metadata::{ @@ -25,7 +25,7 @@ pub(crate) use crate::lock::export::metadata::{ }; pub use crate::lock::export::pylock_toml::{PylockToml, PylockTomlError, PylockTomlErrorKind}; pub use crate::lock::export::requirements_txt::RequirementsTxtExport; -use crate::lock::{LockErrorKind, PackageIndex}; +use crate::lock::{LockErrorKind, PackageIndex, Source}; use crate::{Installable, InstallableRootKind, LockError, Package}; pub mod cyclonedx_json; @@ -33,6 +33,13 @@ mod metadata; mod pylock_toml; mod requirements_txt; +/// Whether to include virtual packages in the export. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum VirtualPackages { + Include, + Exclude, +} + /// A flat requirement, with its associated marker. #[derive(Debug, Clone, PartialEq, Eq)] struct ExportableRequirement<'lock> { @@ -50,6 +57,8 @@ struct ExportableRequirements<'lock>(Vec>); impl<'lock> ExportableRequirements<'lock> { /// Generate the set of exportable [`ExportableRequirement`] entries from the given lockfile. + /// + /// Excluded virtual packages still contribute dependencies. fn from_lock( target: &impl Installable<'lock>, prune: &[PackageName], @@ -57,14 +66,15 @@ impl<'lock> ExportableRequirements<'lock> { groups: &DependencyGroupsWithDefaults, annotate: bool, install_options: &'lock InstallOptions, + virtual_packages: VirtualPackages, ) -> Result { let size_guess = target.lock().packages.len(); let mut graph = Graph::, Edge<'lock>>::with_capacity(size_guess, size_guess); - let mut inverse = vec![None; size_guess]; + let mut node_by_package = vec![None; size_guess]; let mut queue: VecDeque<(PackageIndex, Option<&ExtraName>)> = VecDeque::new(); let mut seen = FxHashSet::default(); - let mut activated_items = FxHashMap::default(); + let mut activated_items = FxHashSet::default(); let root = graph.add_node(Node::Root); @@ -92,16 +102,14 @@ impl<'lock> ExportableRequirements<'lock> { name: root_name.clone(), })?; - if root_kind == InstallableRootKind::Production { + if root_kind == InstallableRootKind::Production && groups.prod() { // Track the activated package in the list of known conflicts. - activated_items.insert(ConflictItem::from(dist.id.name.clone()), MarkerTree::TRUE); - } + activated_items.insert(ConflictItem::from(dist.id.name.clone())); - if root_kind == InstallableRootKind::Production && groups.prod() { let package_index = target.lock().by_id[&dist.id]; // Add the workspace package to the graph. - let index = *inverse[package_index.0] + let index = *node_by_package[package_index.0] .get_or_insert_with(|| graph.add_node(Node::Package(dist))); graph.add_edge( root, @@ -116,62 +124,51 @@ impl<'lock> ExportableRequirements<'lock> { queue.push_back((package_index, None)); for extra in extras.extra_names(dist.optional_dependencies.keys()) { queue.push_back((package_index, Some(extra))); - activated_items.insert( - ConflictItem::from((dist.id.name.clone(), extra.clone())), - MarkerTree::TRUE, - ); + activated_items + .insert(ConflictItem::from((dist.id.name.clone(), extra.clone()))); } } // Add any development dependencies. - for (group, dep) in dist + for (group, dependencies) in dist .dependency_groups .iter() - .filter_map(|(group, deps)| { - if target.includes_group(Some(&dist.id.name), group, groups) { - Some(deps.iter().map(move |dep| (group, dep))) - } else { - None - } - }) - .flatten() + .filter(|(group, _)| target.includes_group(Some(&dist.id.name), group, groups)) { - // Track the activated group in the list of known conflicts. - activated_items.insert( - ConflictItem::from((dist.id.name.clone(), group.clone())), - MarkerTree::TRUE, - ); + // Empty selected groups still participate in conflicts. + activated_items.insert(ConflictItem::from((dist.id.name.clone(), group.clone()))); - if prune.contains(&dep.package_id.name) { - continue; - } + for dep in dependencies { + if prune.contains(&dep.package_id.name) { + continue; + } - let dep_dist = target.lock().package(dep.index); + let dep_dist = target.lock().package(dep.index); - // Add the dependency to the graph. - let dep_index = *inverse[dep.index.0] - .get_or_insert_with(|| graph.add_node(Node::Package(dep_dist))); + // Add the dependency to the graph. + let dep_index = *node_by_package[dep.index.0] + .get_or_insert_with(|| graph.add_node(Node::Package(dep_dist))); - // Add an edge from the root. Development dependencies may be installed without - // installing the workspace package itself (which can never have markers on it - // anyway), so they're directly connected to the root. - graph.add_edge( - root, - dep_index, - Edge::Dev { - group, - marker: dep.simplified_marker.as_simplified_marker_tree(), - dep_extras: dep.extra.iter().collect(), - }, - ); + // Add an edge from the root. Development dependencies may be installed without + // installing the workspace package itself (which can never have markers on it + // anyway), so they're directly connected to the root. + graph.add_edge( + root, + dep_index, + Edge::Prod { + marker: dep.simplified_marker.as_simplified_marker_tree(), + dep_extras: dep.extra.iter().collect(), + }, + ); - // Push its dependencies on the queue. - if seen.insert((dep.index, None)) { - queue.push_back((dep.index, None)); - } - for extra in &dep.extra { - if seen.insert((dep.index, Some(extra))) { - queue.push_back((dep.index, Some(extra))); + // Push its dependencies on the queue. + if seen.insert((dep.index, None)) { + queue.push_back((dep.index, None)); + } + for extra in &dep.extra { + if seen.insert((dep.index, Some(extra))) { + queue.push_back((dep.index, Some(extra))); + } } } } @@ -229,7 +226,7 @@ impl<'lock> ExportableRequirements<'lock> { let package_index = target.lock().by_id[&dist.id]; // Add the dependency to the graph and get its index. - let dep_index = *inverse[package_index.0] + let dep_index = *node_by_package[package_index.0] .get_or_insert_with(|| graph.add_node(Node::Package(dist))); // Add an edge from the root. @@ -255,9 +252,9 @@ impl<'lock> ExportableRequirements<'lock> { } } - // Create all the relevant nodes. + // Traverse each package's regular and requested optional dependencies separately. while let Some((package_index, extra)) = queue.pop_front() { - let index = inverse[package_index.0].expect("queued package has a graph node"); + let index = node_by_package[package_index.0].expect("queued package has a graph node"); let package = target.lock().package(package_index); let deps = if let Some(extra) = extra { @@ -277,11 +274,10 @@ impl<'lock> ExportableRequirements<'lock> { continue; } - // Evaluate the conflict marker. let dep_dist = target.lock().package(dep.index); // Add the dependency to the graph. - let dep_index = *inverse[dep.index.0] + let dep_index = *node_by_package[dep.index.0] .get_or_insert_with(|| graph.add_node(Node::Package(dep_dist))); let dep_extras = dep.extra.iter().collect::>(); @@ -314,8 +310,33 @@ impl<'lock> ExportableRequirements<'lock> { } } + let include_package = |package: &Package| { + let include_source = match &package.id.source { + Source::Virtual(_) => virtual_packages == VirtualPackages::Include, + Source::Registry(_) + | Source::Git(..) + | Source::Direct(..) + | Source::Path(_) + | Source::Directory(_) + | Source::Editable(_) => true, + }; + + include_source + && install_options.include_package( + package.as_install_target(), + target.project_name(), + target.lock().members(), + ) + }; + // Determine the reachability of each node in the graph. - let mut reachability = conflict_marker_reachability(&graph, &[], &activated_items); + let mut reachability = conflict_marker_reachability( + &graph, + root, + &activated_items, + target.lock().conflicts(), + include_package, + )?; // Collect all packages. let nodes = graph @@ -324,13 +345,7 @@ impl<'lock> ExportableRequirements<'lock> { Node::Root => None, Node::Package(package) => Some((index, package)), }) - .filter(|(_index, package)| { - install_options.include_package( - package.as_install_target(), - target.project_name(), - target.lock().members(), - ) - }) + .filter(|(_index, package)| include_package(package)) .map(|(index, package)| ExportableRequirement { package, marker: reachability.remove(&index).unwrap_or_default(), @@ -376,11 +391,6 @@ enum Edge<'lock> { marker: MarkerTree, dep_extras: Vec<&'lock ExtraName>, }, - Dev { - group: &'lock GroupName, - marker: MarkerTree, - dep_extras: Vec<&'lock ExtraName>, - }, } impl Edge<'_> { @@ -389,7 +399,6 @@ impl Edge<'_> { match self { Self::Prod { marker, .. } => marker, Self::Optional { marker, .. } => marker, - Self::Dev { marker, .. } => marker, } } @@ -398,189 +407,593 @@ impl Edge<'_> { match self { Self::Prod { dep_extras, .. } => dep_extras, Self::Optional { dep_extras, .. } => dep_extras, - Self::Dev { dep_extras, .. } => dep_extras, } } } -impl Reachable for Edge<'_> { - fn true_marker() -> MarkerTree { - MarkerTree::TRUE +/// Compute package reachability before resolving global conflict activations. +/// +/// Package and optional-extra reachability form a monotone graph problem while conflict +/// variables remain symbolic. Combining complete paths first preserves correlations between +/// alternative forks. Each referenced conflict item then has one activation equation, shared +/// by every branch of the dependency graph. +/// +/// For example, if the only path requesting `foo[ssl]` has marker `sys_platform == 'linux'`, +/// that condition replaces references to `foo[ssl]` throughout the graph, including on siblings. +fn conflict_marker_reachability<'lock>( + graph: &Graph, Edge<'lock>>, + root: NodeIndex, + known_conflicts: &FxHashSet, + conflicts: &Conflicts, + include_package: impl Fn(&Package) -> bool, +) -> Result, LockError> { + let variables = ConflictVariables::from_graph(graph, known_conflicts, conflicts); + + // Give each requested package extra its own reachability node. For `a -> foo[ssl]`, the + // edge reaches both `foo` and `foo[ssl]`; the optional dependencies of `foo[ssl]` leave + // from the extra node, so reaching `foo` alone cannot activate them. + let mut solver = SymbolicExportReachability { + edges: Vec::with_capacity(graph.edge_count()), + nodes: vec![ReachabilityNode::new(); graph.node_count()], + activations: vec![MarkerTree::FALSE; variables.len()], + queue: VecDeque::new(), + }; + let mut extra_nodes = FxHashMap::default(); + for edge in graph.edge_references() { + let source = match edge.weight() { + Edge::Optional { extra, .. } => *extra_nodes + .entry((edge.source(), *extra)) + .or_insert_with(|| solver.add_node()), + Edge::Prod { .. } => edge.source().index(), + }; + let mut targets = vec![edge.target().index()]; + for extra in edge.weight().dep_extras() { + targets.push( + *extra_nodes + .entry((edge.target(), *extra)) + .or_insert_with(|| solver.add_node()), + ); + } + solver.edges.push(ExportEdge { + targets, + marker: *edge.weight().marker(), + }); + solver.nodes[source].outgoing.push(edge.id()); } - fn false_marker() -> MarkerTree { - MarkerTree::FALSE + // Link package-local extra nodes to their global facts, and seed the selected root items. + for ((package_index, extra), extra_index) in extra_nodes { + if let Node::Package(package) = graph[package_index] { + let item = ConflictItem::from((package.name().clone(), extra.clone())); + solver.nodes[extra_index].extra_fact = variables.by_item.get(&item).copied(); + if known_conflicts.contains(&item) { + // Selected extras belong to unique, unconditionally reached production roots. + solver.add_reachability(extra_index, MarkerTree::TRUE); + } + } } + for item in known_conflicts { + if let Some(&fact) = variables.by_item.get(item) { + solver.activations[fact] = MarkerTree::TRUE; + } + } + solver.add_reachability(root.index(), MarkerTree::TRUE); + + resolve_marker_bindings(graph, root, &mut solver, &variables); + solver.simplify_unconditional_activations(&variables); + solver.solve(); + + // Recursive activations may remain uncertain, but only emitted packages need + // determined reachability. Omitted packages still participate in the analysis. + let bounds = ExportActivationBounds::new(&solver.activations, &variables).solve(); + let mut resolved = FxHashMap::default(); + let mut reachability = FxHashMap::default(); + for index in graph.node_indices() { + let marker = solver.nodes[index.index()].marker; + let (lower, upper) = *resolved.entry(marker).or_insert_with(|| { + marker.resolve_extras_with_bounds( + variables + .inputs(marker) + .into_iter() + .map(|fact| (variables.names[fact].clone(), bounds[fact])), + ) + }); - fn marker(&self) -> MarkerTree { - *self.marker() + // A difference matters only if it describes a possible environment. + if let Node::Package(package) = graph[index] + && include_package(package) + && !upper.and(lower.negate()).is_false() + { + return Err(LockErrorKind::UnresolvedExportConflict { + package: package.id.clone(), + } + .into()); + } + reachability.insert(index, lower); } + + Ok(reachability) } -/// Determine the markers under which a package is reachable in the dependency tree, taking into -/// account conflicts. -/// -/// This method is structurally similar to [`marker_reachability`], but it _also_ attempts to resolve -/// conflict markers. Specifically, in addition to tracking the reachability marker for each node, -/// we also track (for each node) the conditions under which each conflict item is `true`. Then, -/// when evaluating the marker for the node, we inline the conflict marker conditions, thus removing -/// all conflict items from the marker expression. -fn conflict_marker_reachability<'lock>( - graph: &Graph, Edge<'lock>>, - fork_markers: &[Edge<'lock>], - known_conflicts: &FxHashMap, -) -> FxHashMap { - // For each node, track the conditions under which each conflict item is enabled. - let mut conflict_maps = - FxHashMap::>::with_capacity_and_hasher( - graph.node_count(), - FxBuildHasher, - ); - - // Note that we build including the virtual packages due to how we propagate markers through - // the graph, even though we then only read the markers for base packages. - let mut reachability = FxHashMap::with_capacity_and_hasher(graph.node_count(), FxBuildHasher); - - // Collect the root nodes. - // - // Besides the actual virtual root node, virtual dev dependencies packages are also root - // nodes since the edges don't cover dev dependencies. - let mut queue: Vec<_> = graph - .node_indices() - .filter(|node_index| { - graph - .edges_directed(*node_index, Direction::Incoming) - .next() - .is_none() - }) - .collect(); - - // The root nodes are always applicable, unless the user has restricted resolver - // environments with `tool.uv.environments`. - let root_markers = if fork_markers.is_empty() { - MarkerTree::TRUE - } else { - fork_markers +/// Global conflict variables, indexed by both their conflict item and encoded marker name. +#[derive(Default)] +struct ConflictVariables { + by_item: FxHashMap, + by_encoded_name: FxHashMap, + names: Vec, +} + +impl ConflictVariables { + fn from_graph( + graph: &Graph, Edge<'_>>, + known_conflicts: &FxHashSet, + conflicts: &Conflicts, + ) -> Self { + let mut referenced_names = FxHashSet::default(); + let mut referenced_scoped = FxHashSet::default(); + for edge in graph.edge_references() { + edge.weight().marker().visit_extras(|_, extra| { + referenced_names.insert(extra.clone()); + if let Node::Package(package) = graph[edge.source()] { + referenced_scoped + .insert(ConflictItem::from((package.name().clone(), extra.clone()))); + } + }); + } + + // Intern the global facts used by marker inputs. Include declared conflicts as well: their + // encoded names can shadow literal extra names even when no path activates them. + let conflict_items = conflicts .iter() - .fold(MarkerTree::FALSE, |mut acc, edge| { - acc = acc.or(*edge.marker()); - acc - }) - }; - for root_index in &queue { - reachability.insert(*root_index, root_markers); + .flat_map(ConflictSet::iter) + .collect::>(); + let mut variables = Self::default(); + let mut add_fact = |item: ConflictItem| { + let name = encode_conflict_item(&item); + if referenced_names.contains(&name) + || referenced_scoped.contains(&item) + || conflict_items.contains(&item) + { + variables.intern(item, name); + } + }; + + for item in &conflict_items { + add_fact((*item).clone()); + } + for item in known_conflicts { + add_fact(item.clone()); + } + + for edge in graph.edge_references() { + if let Node::Package(package) = graph[edge.target()] { + for extra in edge.weight().dep_extras() { + add_fact(ConflictItem::from(( + package.name().clone(), + (*extra).clone(), + ))); + } + } + } + + variables } - // Propagate all markers through the graph, so that the eventual marker for each node is the - // union of the markers of each path we can reach the node by. - while let Some(parent_index) = queue.pop() { - // Resolve any conflicts in the parent marker. - reachability.entry(parent_index).and_modify(|marker| { - let conflict_map = conflict_maps.get(&parent_index).unwrap_or(known_conflicts); - let scope_package = match &graph[parent_index] { - Node::Package(package) => Some(package.name()), - Node::Root => None, - }; - *marker = resolve_activated_extras(*marker, scope_package, conflict_map); + fn intern(&mut self, item: ConflictItem, name: ExtraName) { + let index = *self.by_item.entry(item).or_insert_with(|| { + let index = self.names.len(); + self.names.push(name.clone()); + index }); + self.by_encoded_name.insert(name, index); + } - // When we see an edge like `parent [dotenv]> flask`, we should take the reachability - // on `parent`, combine it with the marker on the edge, then add `flask[dotenv]` to - // the inference map on the `flask` node. - for child_edge in graph.edges_directed(parent_index, Direction::Outgoing) { - let mut parent_marker = reachability[&parent_index]; - - // The marker for all paths to the child through the parent. - let mut parent_map = conflict_maps - .get(&parent_index) - .cloned() - .unwrap_or_else(|| known_conflicts.clone()); - - if let Node::Package(child) = graph[child_edge.target()] { - for extra in child_edge.weight().dep_extras() { - let item = ConflictItem::from((child.name().clone(), (*extra).clone())); - parent_map.insert(item, parent_marker); - } + fn len(&self) -> usize { + self.names.len() + } + + /// Return the distinct canonical conflict variables read by a marker. + fn inputs(&self, marker: MarkerTree) -> Vec { + let mut inputs = Vec::new(); + marker.visit_extras(|_, name| inputs.push(self.by_encoded_name[name])); + inputs.sort_unstable(); + inputs.dedup(); + inputs + } +} + +/// A dependency's package/extra targets and its canonical marker. +#[derive(Debug)] +struct ExportEdge { + targets: Vec, + marker: MarkerTree, +} + +/// Bind raw and encoded extra names before evaluating their activation conditions. +/// +/// For example, on an edge out of `bar`, `extra == 'extra-3-foo-ssl'` could refer to the +/// encoded conflict `foo[ssl]` or the literal extra `bar[extra-3-foo-ssl]`. The encoded fact +/// wins when it is structurally present at the edge, even through a path with a false marker. +/// Its activation value is resolved later. +fn resolve_marker_bindings( + graph: &Graph, Edge<'_>>, + root: NodeIndex, + solver: &mut SymbolicExportReachability, + variables: &ConflictVariables, +) { + // Request lists follow edge order, so membership checks can use binary search. + let mut requests = vec![Vec::new(); variables.len()]; + for (index, edge) in solver.edges.iter().enumerate() { + for &target in &edge.targets { + if let Some(fact) = solver.nodes[target].extra_fact { + requests[fact].push(EdgeIndex::new(index)); } + } + } - let scope_package = match &graph[parent_index] { - Node::Package(package) => Some(package.name()), - Node::Root => None, - }; + let scoped_fact = |source: NodeIndex, name: &ExtraName| match graph[source] { + Node::Package(package) => variables + .by_item + .get(&ConflictItem::from((package.name().clone(), name.clone()))) + .copied(), + Node::Root => None, + }; - let marker = match child_edge.weight() { - Edge::Prod { marker, .. } => { - // Resolve any active extras on the edge. - resolve_activated_extras(*marker, scope_package, &parent_map) - } - Edge::Optional { extra, marker, .. } => { - // The optional edge is only active when its extra is active. Preserve the - // extra's reachability marker, since matching constraints can be omitted from - // the dependency marker as redundant when the lockfile is written. - let active_marker = if let Node::Package(parent) = graph[parent_index] { - let item = ConflictItem::from((parent.name().clone(), (*extra).clone())); - *parent_map.entry(item).or_insert(parent_marker) - } else { - parent_marker - }; + // Find names that could refer to either a local extra or an encoded conflict. + let mut aliases: FxHashMap> = FxHashMap::default(); + for edge in graph.edge_references() { + edge.weight().marker().visit_extras(|_, name| { + if let Some(scoped) = scoped_fact(edge.source(), name) + && let Some(&encoded) = variables.by_encoded_name.get(name) + { + aliases + .entry(encoded) + .or_default() + .push((edge.id(), scoped)); + } + }); + } - // Resolve any active extras on the edge. - let marker = resolve_activated_extras(*marker, scope_package, &parent_map); - marker.and(active_marker) - } - Edge::Dev { group, marker, .. } => { - // The dependency group is active for this edge itself, so add it before - // resolving any active extras on the edge. - if let Node::Package(parent) = graph[parent_index] { - let item = ConflictItem::from((parent.name().clone(), (*group).clone())); - parent_map.insert(item, parent_marker); - } + let mut overrides = FxHashMap::default(); + for (fact, bindings) in aliases { + // Selected facts are present from the root; requested facts become present at the target. + let mut pending = if solver.activations[fact].is_true() { + vec![root] + } else { + Vec::new() + }; + pending.extend(requests[fact].iter().map(|&edge| { + graph + .edge_endpoints(edge) + .expect("request belongs to an export edge") + .1 + })); + + let mut present = vec![false; graph.node_count()]; + while let Some(index) = pending.pop() { + if present[index.index()] { + continue; + } + present[index.index()] = true; + pending.extend(graph.neighbors_directed(index, Direction::Outgoing)); + } + + for (index, scoped) in bindings { + let (parent, _) = graph + .edge_endpoints(index) + .expect("binding belongs to an export edge"); + if !present[parent.index()] && requests[fact].binary_search(&index).is_err() { + overrides.insert((index, fact), scoped); + } + } + } - // Resolve any active extras on the edge. - resolve_activated_extras(*marker, scope_package, &parent_map) + // Replace each extra name with its bound fact. An edge's own request is already active + // when evaluating that edge's marker. + for edge in graph.edge_references() { + let mut replacements = Vec::new(); + edge.weight().marker().visit_extras(|_, name| { + let fact = variables + .by_encoded_name + .get(name) + .map(|&fact| overrides.get(&(edge.id(), fact)).copied().unwrap_or(fact)) + .or_else(|| scoped_fact(edge.source(), name)); + let marker = fact.map_or(MarkerTree::FALSE, |fact| { + if requests[fact].binary_search(&edge.id()).is_ok() { + MarkerTree::TRUE + } else { + conflict_variable(&variables.names[fact]) } - }; + }); + replacements.push((name.clone(), marker)); + }); + solver.edges[edge.id().index()].marker = + edge.weight().marker().resolve_extras(replacements); + } +} + +/// Least package and optional-extra reachability with conflict variables held symbolic. +/// +/// Original graph nodes keep their indices, and optional-extra nodes are appended. Edge indices +/// also match the original graph. `activations` is indexed by global fact rather than node. +struct SymbolicExportReachability { + edges: Vec, + nodes: Vec, + activations: Vec, + queue: VecDeque, +} + +/// Reachability and worklist state for one package or optional-extra node. +#[derive(Clone)] +struct ReachabilityNode { + marker: MarkerTree, + outgoing: Vec, + extra_fact: Option, + queued: bool, +} + +impl ReachabilityNode { + /// Create an unreachable node with no outgoing edges or global activation. + fn new() -> Self { + Self { + marker: MarkerTree::FALSE, + outgoing: Vec::new(), + extra_fact: None, + queued: false, + } + } +} + +impl SymbolicExportReachability { + /// Add an optional-extra node that can be reached only by an actual request. + fn add_node(&mut self) -> usize { + let index = self.nodes.len(); + self.nodes.push(ReachabilityNode::new()); + index + } - // Propagate the edge to the known conflicts. - for value in parent_map.values_mut() { - *value = value.and(marker); + /// Merge complete symbolic paths and schedule only changed sources. + fn add_reachability(&mut self, index: usize, marker: MarkerTree) { + let node = &mut self.nodes[index]; + let combined = node.marker.or(marker); + if combined != node.marker { + node.marker = combined; + if !mem::replace(&mut node.queued, true) { + self.queue.push_back(index); } + } + } + + /// Fold activations proved by unconditional request paths before building symbolic paths. + /// + /// Only constant-true guards and seeds establish these facts, independently of any + /// assumed activation. All unproved activations remain symbolic. + fn simplify_unconditional_activations(&mut self, variables: &ConflictVariables) { + if self.activations.is_empty() { + return; + } - // Propagate the edge to the node itself. - parent_marker = parent_marker.and(marker); + let mut active = self + .activations + .iter() + .map(|marker| marker.is_true()) + .collect::>(); - // Combine the inferred conflicts with the existing conflicts on the node. - match conflict_maps.entry(child_edge.target()) { - Entry::Occupied(mut existing) => { - let child_map = existing.get_mut(); - for (key, value) in parent_map { - let child_marker = child_map.entry(key).or_insert(MarkerTree::FALSE); - *child_marker = child_marker.or(value); - } - } - Entry::Vacant(vacant) => { - vacant.insert(parent_map); + let mut reached = vec![false; self.nodes.len()]; + let mut pending = self.queue.iter().copied().collect::>(); + while let Some(source) = pending.pop() { + if mem::replace(&mut reached[source], true) { + continue; + } + if let Some(fact) = self.nodes[source].extra_fact { + active[fact] = true; + } + for &edge in &self.nodes[source].outgoing { + let edge = &self.edges[edge.index()]; + if !edge.marker.is_true() { + continue; } + pending.extend(edge.targets.iter().copied()); } + } - // Combine the inferred marker with the existing marker on the node. - match reachability.entry(child_edge.target()) { - Entry::Occupied(mut existing) => { - // If the marker is a subset of the existing marker (A ⊆ B exactly if - // A ∪ B = A), updating the child wouldn't change child's marker. - parent_marker = parent_marker.or(*existing.get()); - if parent_marker != *existing.get() { - existing.insert(parent_marker); - queue.push(child_edge.target()); - } - } - Entry::Vacant(vacant) => { - vacant.insert(parent_marker); - queue.push(child_edge.target()); + if active.iter().any(|&active| active) { + for edge in &mut self.edges { + edge.marker = edge + .marker + .simplify_extras_with(|name| active[variables.by_encoded_name[name]]); + } + } + } + + /// Close package/extra reachability, then derive each global activation from its local nodes. + fn solve(&mut self) { + while let Some(source) = self.queue.pop_front() { + self.nodes[source].queued = false; + let marker = self.nodes[source].marker; + for outgoing in 0..self.nodes[source].outgoing.len() { + let edge = self.nodes[source].outgoing[outgoing].index(); + let condition = marker.and(self.edges[edge].marker); + for target in 0..self.edges[edge].targets.len() { + self.add_reachability(self.edges[edge].targets[target], condition); } } } + + for node in &self.nodes { + if let Some(fact) = node.extra_fact { + self.activations[fact] = self.activations[fact].or(node.marker); + } + } } +} - reachability +/// The canonical Boolean variable for one global conflict item. +fn conflict_variable(name: &ExtraName) -> MarkerTree { + MarkerTree::expression(MarkerExpression::Extra { + operator: ExtraOperator::Equal, + name: MarkerValueExtra::Extra(name.clone()), + }) +} + +/// Which side of an activation interval is being computed with the other side fixed. +#[derive(Clone, Copy)] +enum ExportBound { + Lower, + Upper, +} + +/// Solve global activation equations in dependency order, iterating only recursive components. +/// +/// Under founded evaluation, a positive cycle cannot activate itself. Each `(lower, upper)` pair +/// describes environments where an item is definitely or possibly active under that rule. For +/// example, `a = a` has no external support, so both bounds are false. Mutually negative +/// equations such as `a = !b; b = !a` can remain uncertain. +struct ExportActivationBounds<'a> { + /// Activation condition for each global fact; it may refer to itself or other facts. + conditions: &'a [MarkerTree], + variables: &'a ConflictVariables, + inputs: Vec>, + dependents: Vec>, + component_by_fact: Vec, + queued: Vec, + bounds: Vec<(MarkerTree, MarkerTree)>, +} + +impl<'a> ExportActivationBounds<'a> { + /// Index each equation by the conflict facts whose values can change it. + fn new(conditions: &'a [MarkerTree], variables: &'a ConflictVariables) -> Self { + let inputs = conditions + .iter() + .map(|&marker| variables.inputs(marker)) + .collect::>(); + let mut dependents = vec![Vec::new(); conditions.len()]; + for (fact, inputs) in inputs.iter().enumerate() { + for &input in inputs { + dependents[input].push(fact); + } + } + + Self { + conditions, + variables, + inputs, + dependents, + component_by_fact: vec![0; conditions.len()], + queued: vec![false; conditions.len()], + bounds: vec![(MarkerTree::FALSE, MarkerTree::FALSE); conditions.len()], + } + } + + /// Compute founded bounds while retaining uncertainty from predecessor components. + fn solve(mut self) -> Vec<(MarkerTree, MarkerTree)> { + let mut graph = Graph::<(), ()>::with_capacity(self.conditions.len(), 0); + for _ in self.conditions { + graph.add_node(()); + } + + for (fact, inputs) in self.inputs.iter().enumerate() { + for &input in inputs { + // SCC postorder then visits an equation's dependencies before its readers. + graph.add_edge(NodeIndex::new(fact), NodeIndex::new(input), ()); + } + } + + let components = kosaraju_scc(&graph); + for (component_index, component) in components.iter().enumerate() { + for fact in component { + self.component_by_fact[fact.index()] = component_index; + } + } + + for (component_index, component) in components.iter().enumerate() { + let component = component + .iter() + .map(|fact| fact.index()) + .collect::>(); + + self.solve_component(&component, component_index); + } + + self.bounds + } + + /// Solve one component using bounds already established for its dependencies. + fn solve_component(&mut self, component: &[usize], component_index: usize) { + // A nonrecursive equation can be evaluated once after its inputs have been solved. + if let [fact] = component + && self.inputs[*fact].binary_search(fact).is_err() + { + self.bounds[*fact] = self.evaluate(*fact); + return; + } + + // Negative inputs use the fixed opposite bound, so each inner worklist grows + // monotonically. Restart upper passes from the new lower bound to discard + // support from paths whose negative conditions have closed. + self.solve_bound(component, component_index, ExportBound::Upper); + loop { + let previous_upper = component + .iter() + .map(|&fact| self.bounds[fact].1) + .collect::>(); + for &fact in component { + self.bounds[fact].0 = MarkerTree::FALSE; + } + self.solve_bound(component, component_index, ExportBound::Lower); + + for &fact in component { + self.bounds[fact].1 = self.bounds[fact].0; + } + self.solve_bound(component, component_index, ExportBound::Upper); + + // With an unchanged upper bound, another lower pass would produce the same result. + if component + .iter() + .zip(previous_upper) + .all(|(&fact, previous)| self.bounds[fact].1 == previous) + { + break; + } + } + } + + /// Evaluate a complete activation equation over the current global intervals. + fn evaluate(&self, fact: usize) -> (MarkerTree, MarkerTree) { + self.conditions[fact].resolve_extras_with_bounds( + self.inputs[fact] + .iter() + .map(|&input| (self.variables.names[input].clone(), self.bounds[input])), + ) + } + + /// Compute one least fixed point without revisiting unrelated components. + fn solve_bound(&mut self, component: &[usize], component_index: usize, bound: ExportBound) { + let mut queue = component.iter().copied().collect::>(); + for &fact in component { + self.queued[fact] = true; + } + + while let Some(fact) = queue.pop_front() { + self.queued[fact] = false; + let (lower, upper) = self.evaluate(fact); + let (current, next) = match bound { + ExportBound::Lower => (&mut self.bounds[fact].0, lower), + ExportBound::Upper => (&mut self.bounds[fact].1, upper), + }; + debug_assert!(current.and(next.negate()).is_false()); + if *current == next { + continue; + } + *current = next; + + for &dependent in &self.dependents[fact] { + if self.component_by_fact[dependent] == component_index + && !mem::replace(&mut self.queued[dependent], true) + { + queue.push_back(dependent); + } + } + } + } } diff --git a/crates/uv-lock/src/lock/export/pylock_toml.rs b/crates/uv-lock/src/lock/export/pylock_toml.rs index 86624145a60..081d0768d79 100644 --- a/crates/uv-lock/src/lock/export/pylock_toml.rs +++ b/crates/uv-lock/src/lock/export/pylock_toml.rs @@ -44,7 +44,7 @@ use uv_warnings::warn_user_once; use uv_resolver_types::ResolverOutput; -use crate::lock::export::ExportableRequirements; +use crate::lock::export::{ExportableRequirements, VirtualPackages}; use crate::lock::{Source, WheelTagHint, is_wheel_unreachable}; use crate::{Installable, LockError}; @@ -793,6 +793,7 @@ impl<'lock> PylockToml { dev, annotate, install_options, + VirtualPackages::Exclude, )?; // Sort the nodes. diff --git a/crates/uv-lock/src/lock/export/requirements_txt.rs b/crates/uv-lock/src/lock/export/requirements_txt.rs index cfb39bb25b9..0e664f03668 100644 --- a/crates/uv-lock/src/lock/export/requirements_txt.rs +++ b/crates/uv-lock/src/lock/export/requirements_txt.rs @@ -14,7 +14,7 @@ use uv_normalize::PackageName; use uv_pypi_types::{ParsedArchiveUrl, ParsedGitDirectoryUrl, ParsedGitPathUrl}; use uv_redacted::SafeUrl; -use crate::lock::export::{ExportableRequirement, ExportableRequirements}; +use crate::lock::export::{ExportableRequirement, ExportableRequirements, VirtualPackages}; use crate::lock::{Package, PackageId, Source}; use crate::{Installable, LockError}; @@ -45,6 +45,7 @@ impl<'lock> RequirementsTxtExport<'lock> { dev, annotate, install_options, + VirtualPackages::Exclude, )?; // Sort the nodes, such that unnamed URLs (editables) appear at the top. diff --git a/crates/uv-lock/src/lock/mod.rs b/crates/uv-lock/src/lock/mod.rs index 9b75e142527..bdf44657a17 100644 --- a/crates/uv-lock/src/lock/mod.rs +++ b/crates/uv-lock/src/lock/mod.rs @@ -10153,6 +10153,15 @@ enum LockErrorKind { /// The ID of the dependency whose inclusion is ambiguous. dependency: PackageId, }, + /// Recursive extra conditions leave a package's export marker undetermined. + #[error( + "Cannot determine whether to export `{package}` because its dependency markers contain recursive extra conditions", + package = package.cyan() + )] + UnresolvedExportConflict { + /// The package whose inclusion remains undetermined. + package: PackageId, + }, /// An error that occurs when resolving metadata for a package. #[error("Failed to generate package metadata for `{id}`", id = id.cyan())] Resolution { diff --git a/crates/uv-pep508/src/marker/algebra.rs b/crates/uv-pep508/src/marker/algebra.rs index fb52199b106..b1a88f598b4 100644 --- a/crates/uv-pep508/src/marker/algebra.rs +++ b/crates/uv-pep508/src/marker/algebra.rs @@ -850,6 +850,100 @@ impl InternerGuard<'_> { result } + /// Substitute activation intervals without enumerating DNF paths. + pub(crate) fn resolve_extras_with_bounds( + &mut self, + marker: NodeId, + known_extras: &FxHashMap, + ) -> (NodeId, NodeId) { + self.resolve_extras_with_bounds_cached(marker, known_extras, &mut FxHashMap::default()) + } + + /// Transform each signed node once, computing both activation bounds together. + fn resolve_extras_with_bounds_cached( + &mut self, + marker: NodeId, + known_extras: &FxHashMap, + cache: &mut FxHashMap, + ) -> (NodeId, NodeId) { + if marker.is_true() || marker.is_false() { + return (marker, marker); + } + if let Some(&result) = cache.get(&marker) { + return result; + } + + let node = self.shared.node(marker); + let result = if let (Variable::Extra(name), Edges::Boolean { high, low }) = + (&node.var, &node.children) + { + let (lower, upper) = known_extras + .get(name.extra()) + .copied() + .unwrap_or((NodeId::FALSE, NodeId::FALSE)); + debug_assert!(self.and(lower, upper.not()).is_false()); + + if lower.is_true() { + self.resolve_extras_with_bounds_cached(high.negate(marker), known_extras, cache) + } else if upper.is_false() { + self.resolve_extras_with_bounds_cached(low.negate(marker), known_extras, cache) + } else { + let (high_lower, high_upper) = self.resolve_extras_with_bounds_cached( + high.negate(marker), + known_extras, + cache, + ); + let (low_lower, low_upper) = + self.resolve_extras_with_bounds_cached(low.negate(marker), known_extras, cache); + + // An extra is definitely absent only outside its upper bound; it is possibly + // absent outside its lower bound. Negation therefore swaps the bounds. + let positive_lower = self.and(lower, high_lower); + let negative_lower = self.and(upper.not(), low_lower); + let mut result_lower = self.or(positive_lower, negative_lower); + if lower != upper { + // Both branches can guarantee the marker even when this extra is unknown: + // `x or y` is true if `y` is definitely true, regardless of `x`. + let consensus = self.and(high_lower, low_lower); + result_lower = self.or(result_lower, consensus); + } + + let positive_upper = self.and(upper, high_upper); + let negative_upper = self.and(lower.not(), low_upper); + (result_lower, self.or(positive_upper, negative_upper)) + } + } else { + let mut lower_changed = false; + let lower_children = node.children.map(marker, |child| { + let (lower, _) = self.resolve_extras_with_bounds_cached(child, known_extras, cache); + lower_changed |= lower != child; + lower + }); + + let mut upper_changed = false; + let upper_children = node.children.map(marker, |child| { + let (_, upper) = self.resolve_extras_with_bounds_cached(child, known_extras, cache); + upper_changed |= upper != child; + upper + }); + + let lower = if lower_changed { + self.rebuild_node(&node.var, lower_children) + } else { + marker + }; + let upper = if upper_changed { + self.rebuild_node(&node.var, upper_children) + } else { + marker + }; + (lower, upper) + }; + + cache.insert(marker, result); + result + } + /// Rebuild a decision after substitution, allowing children to introduce earlier variables. /// Calling `create_node` directly would violate the diagram's variable ordering. fn rebuild_node(&mut self, variable: &Variable, children: EdgeBuilder) -> NodeId { diff --git a/crates/uv-pep508/src/marker/tree.rs b/crates/uv-pep508/src/marker/tree.rs index 26509841e19..95f8a35994e 100644 --- a/crates/uv-pep508/src/marker/tree.rs +++ b/crates/uv-pep508/src/marker/tree.rs @@ -1395,6 +1395,34 @@ impl MarkerTree { Self(INTERNER.lock().resolve_extras(self.0, &known_extras)) } + /// Bound the result of replacing extras with their activation conditions. + /// + /// Each extra has a lower and upper bound, and its lower bound must imply its upper bound. + /// The returned markers describe where this marker definitely and possibly holds, + /// respectively. Extras absent from `known_extras` are false. + /// + /// Substitution is simultaneous: extras in replacement bounds are not themselves replaced. + /// Shared subgraphs are transformed once, without expanding disjunctive normal form. + /// `known_extras` is iterated only when this marker contains an `extra` expression. + #[must_use] + pub fn resolve_extras_with_bounds( + self, + known_extras: impl IntoIterator, + ) -> (Self, Self) { + if self.is_true() || self.is_false() || !INTERNER.shared.contains_extras(self.0) { + return (self, self); + } + + let known_extras: FxHashMap<_, _> = known_extras + .into_iter() + .map(|(name, (lower, upper))| (name, (lower.0, upper.0))) + .collect(); + let (lower, upper) = INTERNER + .lock() + .resolve_extras_with_bounds(self.0, &known_extras); + (Self(lower), Self(upper)) + } + /// Returns a new `MarkerTree` where only `extra` expressions are removed. /// /// If the marker did not contain any `extra` expressions, then a marker @@ -2353,6 +2381,213 @@ mod test { assert_eq!(marker.resolve_extras(known), condition); } + /// An unknown decision cannot hide a condition guaranteed by both branches. + #[test] + fn resolve_extras_with_bounds_consensus() { + assert_eq!( + m("extra == 'x' or extra == 'y'").resolve_extras_with_bounds([ + ( + "x".parse().expect("valid extra name"), + (MarkerTree::FALSE, MarkerTree::TRUE), + ), + ( + "y".parse().expect("valid extra name"), + (MarkerTree::TRUE, MarkerTree::TRUE), + ), + ]), + (MarkerTree::TRUE, MarkerTree::TRUE) + ); + + let linux = m("sys_platform == 'linux'"); + + assert_eq!( + m("(extra == 'x' and extra == 'y') or (extra != 'x' and extra == 'z')") + .resolve_extras_with_bounds([ + ( + "x".parse().expect("valid extra name"), + (MarkerTree::FALSE, MarkerTree::TRUE), + ), + ( + "y".parse().expect("valid extra name"), + (linux, MarkerTree::TRUE), + ), + ( + "z".parse().expect("valid extra name"), + ( + linux, + m("sys_platform == 'linux' or sys_platform == 'darwin'") + ), + ), + ]), + (linux, MarkerTree::TRUE) + ); + } + + /// Negation exchanges the complemented upper and lower bounds. + #[test] + fn resolve_extras_with_bounds_negation() { + let known = [( + "a".parse().expect("valid extra name"), + ( + m("sys_platform == 'linux'"), + m("sys_platform == 'linux' or sys_platform == 'darwin'"), + ), + )]; + + assert_eq!( + m("extra != 'a'").resolve_extras_with_bounds(known.clone()), + ( + m("sys_platform != 'linux' and sys_platform != 'darwin'"), + m("sys_platform != 'linux'"), + ) + ); + + // Missing extras are inactive, including in negated expressions. + assert_eq!( + m("extra == 'missing'").resolve_extras_with_bounds(known.clone()), + (MarkerTree::FALSE, MarkerTree::FALSE) + ); + assert_eq!( + m("extra != 'missing'").resolve_extras_with_bounds(known), + (MarkerTree::TRUE, MarkerTree::TRUE) + ); + } + + /// Both bounds retain environment conditions and can introduce earlier decision variables. + #[test] + fn resolve_extras_with_bounds_environment_conditions() { + let marker = m("platform_release >= '24' and (extra == 'x' or extra == 'y')"); + + assert_eq!( + marker.resolve_extras_with_bounds([ + ( + "x".parse().expect("valid extra name"), + ( + m("sys_platform == 'linux'"), + m("sys_platform == 'linux' or sys_platform == 'darwin'"), + ), + ), + ( + "y".parse().expect("valid extra name"), + ( + m("sys_platform == 'darwin'"), + m("sys_platform == 'darwin' or sys_platform == 'win32'"), + ), + ), + ]), + ( + m( + "platform_release >= '24' and (sys_platform == 'linux' or sys_platform == 'darwin')" + ), + m( + "platform_release >= '24' and (sys_platform == 'linux' or sys_platform == 'darwin' or sys_platform == 'win32')" + ), + ) + ); + } + + /// Exact substitution retains each environment decision kind and leaves replacement extras alone. + #[test] + fn resolve_extras_with_bounds_substitutes_simultaneously() { + let condition = m("sys_platform == 'darwin' or extra == 'b'"); + let known = [ + ( + "a".parse().expect("valid extra name"), + (condition, condition), + ), + ( + "b".parse().expect("valid extra name"), + (MarkerTree::FALSE, MarkerTree::FALSE), + ), + ]; + let marker = m("python_version >= '3.10' + and implementation_name == 'cpython' + and platform_release >= '24' + and platform_machine in 'x86_64,aarch64' + and 'beta' in platform_version + and 'dev' in dependency_groups + and extra == 'a'"); + let expected = m("python_version >= '3.10' + and implementation_name == 'cpython' + and platform_release >= '24' + and platform_machine in 'x86_64,aarch64' + and 'beta' in platform_version + and 'dev' in dependency_groups + and (sys_platform == 'darwin' or extra == 'b')"); + + assert_eq!( + marker.resolve_extras_with_bounds(known), + (expected, expected) + ); + } + + /// Replacement iterators can acquire the interner lock, and are unnecessary without extras. + #[test] + fn resolve_extras_with_bounds_collects_replacements_before_locking() { + let replacements_built = Cell::new(0); + let replacements = iter::once_with(|| { + replacements_built.set(replacements_built.get() + 1); + ( + "a".parse().expect("valid extra name"), + (m("sys_platform == 'darwin'"), MarkerTree::TRUE), + ) + }); + let environment = m("python_version >= '3.10'"); + + assert_eq!( + environment.resolve_extras_with_bounds(replacements.clone()), + (environment, environment) + ); + assert_eq!(replacements_built.get(), 0); + + assert_eq!( + m("python_version >= '3.10' and extra == 'a'").resolve_extras_with_bounds(replacements), + ( + m("python_version >= '3.10' and sys_platform == 'darwin'"), + environment, + ) + ); + assert_eq!(replacements_built.get(), 1); + } + + /// Shared subgraphs and their complements must retain distinct activation bounds. + #[test] + fn resolve_extras_with_bounds_shared_subgraphs() { + let condition = m("sys_platform == 'darwin'"); + let known = [ + ( + "a".parse().expect("valid extra name"), + (MarkerTree::FALSE, MarkerTree::TRUE), + ), + ( + "b".parse().expect("valid extra name"), + (condition, condition), + ), + ( + "c".parse().expect("valid extra name"), + (MarkerTree::FALSE, MarkerTree::TRUE), + ), + ( + "d".parse().expect("valid extra name"), + (condition, condition), + ), + ]; + let marker = m("(implementation_name == 'cpython' + and (extra == 'a' or extra == 'b') + and (extra == 'c' or extra == 'd')) + or (implementation_name != 'cpython' + and ((extra != 'a' and extra != 'b') + or (extra != 'c' and extra != 'd')))"); + + assert_eq!( + marker.resolve_extras_with_bounds(known), + ( + m("implementation_name == 'cpython' and sys_platform == 'darwin'"), + m("implementation_name == 'cpython' or sys_platform != 'darwin'"), + ) + ); + } + #[test] fn restrict() { let environment = m( diff --git a/crates/uv-resolver-types/src/universal_marker.rs b/crates/uv-resolver-types/src/universal_marker.rs index e860e96942d..7c43a584097 100644 --- a/crates/uv-resolver-types/src/universal_marker.rs +++ b/crates/uv-resolver-types/src/universal_marker.rs @@ -668,7 +668,7 @@ impl std::fmt::Debug for ConflictMarker { } /// Encodes the given conflict into a valid `extra` value in a PEP 508 marker. -fn encode_conflict_item(conflict: &ConflictItem) -> ExtraName { +pub fn encode_conflict_item(conflict: &ConflictItem) -> ExtraName { match conflict.kind() { ConflictKind::Extra(extra) => encode_package_extra(conflict.package(), extra), ConflictKind::Group(group) => encode_package_group(conflict.package(), group), diff --git a/crates/uv/tests/project/export.rs b/crates/uv/tests/project/export.rs index 28013fae4e6..f5aec744008 100644 --- a/crates/uv/tests/project/export.rs +++ b/crates/uv/tests/project/export.rs @@ -358,6 +358,1333 @@ fn requirements_txt_conditional_transitive_extra() -> Result<()> { Ok(()) } +/// An extra requested through an incompatible path must not activate another package version. +/// A leaf also reached by an unconditional dependency remains unconditional. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_does_not_activate_extra_through_incompatible_path() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = [ + "shared==1.0.0 ; sys_platform == 'linux'", + "shared==2.0.0 ; sys_platform != 'linux'", + ] + + [project.optional-dependencies] + feature = ["bridge==1.0.0 ; sys_platform == 'linux'"] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "shared", version = "1.0.0", source = { registry = "https://example.com/simple" }, marker = "sys_platform == 'linux'" }, + { name = "shared", version = "2.0.0", source = { registry = "https://other.example/simple" }, marker = "sys_platform != 'linux'" }, + ] + + [package.optional-dependencies] + feature = [{ name = "bridge", version = "1.0.0", source = { registry = "https://example.com/simple" }, marker = "sys_platform == 'linux'" }] + + [package.metadata] + provides-extras = ["feature"] + + [[package]] + name = "bridge" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [ + { name = "shared", version = "1.0.0", source = { registry = "https://example.com/simple" }, marker = "sys_platform == 'linux'", extra = ["a"] }, + { name = "shared", version = "2.0.0", source = { registry = "https://other.example/simple" }, marker = "sys_platform != 'linux'", extra = ["a"] }, + ] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + resolution-markers = ["sys_platform == 'linux'"] + + [package.optional-dependencies] + a = [{ name = "leaf-b", version = "1.0.0", source = { registry = "https://example.com/simple" } }] + + [package.metadata] + provides-extras = ["a"] + + [[package]] + name = "shared" + version = "2.0.0" + source = { registry = "https://other.example/simple" } + resolution-markers = ["sys_platform != 'linux'"] + dependencies = [{ name = "leaf-b", version = "1.0.0", source = { registry = "https://example.com/simple" } }] + + [package.optional-dependencies] + a = [{ name = "leaf-c", version = "1.0.0", source = { registry = "https://example.com/simple" } }] + + [package.metadata] + provides-extras = ["a"] + + [[package]] + name = "leaf-b" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + + [[package]] + name = "leaf-c" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--extra").arg("feature") + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + bridge==1.0.0 ; sys_platform == 'linux' + leaf-b==1.0.0 + shared==1.0.0 ; sys_platform == 'linux' + shared==2.0.0 ; sys_platform != 'linux' + "); + + Ok(()) +} + +/// Extra activation must propagate through a dependency cycle even when the base package is +/// already unconditionally reachable. Compatible paths contribute distinct activation markers. +/// Scoped and encoded extras from a separate path must also constrain optional dependencies. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_transitive_extra_activation_cycle() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = [ + "shared==1.0.0", + "bridge-linux==1.0.0 ; sys_platform == 'linux'", + "bridge-macos==1.0.0 ; sys_platform == 'darwin'", + "context[enabled]==1.0.0 ; python_version >= '3.11'", + ] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "shared" }, + { name = "bridge-linux", marker = "sys_platform == 'linux'" }, + { name = "bridge-macos", marker = "sys_platform == 'darwin'" }, + { name = "context", extra = ["enabled"], marker = "python_full_version >= '3.11'" }, + ] + + [[package]] + name = "bridge-linux" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["a"] }] + + [[package]] + name = "bridge-macos" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["a"] }] + + [[package]] + name = "context" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["c"] }] + + [package.optional-dependencies] + enabled = [] + + [package.metadata] + provides-extras = ["enabled"] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + + [package.optional-dependencies] + a = [{ name = "relay" }] + # The self-cycle must not activate b outside the paths that requested it. + b = [ + { name = "leaf", marker = "extra == 'c' and extra == 'extra-7-context-enabled'" }, + { name = "shared", extra = ["b"] }, + ] + c = [] + + [package.metadata] + provides-extras = ["a", "b", "c"] + + [[package]] + name = "relay" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["b"] }] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + bridge-linux==1.0.0 ; sys_platform == 'linux' + bridge-macos==1.0.0 ; sys_platform == 'darwin' + context==1.0.0 ; python_full_version >= '3.11' + leaf==1.0.0 ; (python_full_version >= '3.11' and sys_platform == 'darwin') or (python_full_version >= '3.11' and sys_platform == 'linux') + relay==1.0.0 ; sys_platform == 'darwin' or sys_platform == 'linux' + shared==1.0.0 + "); + + Ok(()) +} + +/// A transitive request for shared[a] selects one of two conflicting versions only on Linux. +/// The other version's negative marker must account for that request before it contributes output. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_transitive_extra_negative_marker() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.11" + dependencies = ["shared", "aa ; sys_platform == 'linux'"] + + [tool.uv] + package = false + conflicts = [[{ package = "shared", extra = "a" }, { package = "shared", extra = "b" }]] + + [tool.uv.workspace] + members = ["aa", "ab", "shared"] + + [tool.uv.sources] + aa = { workspace = true } + ab = { workspace = true } + shared = { workspace = true } + "#, + )?; + + context.temp_dir.child("aa/pyproject.toml").write_str( + r#" + [project] + name = "aa" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["ab"] + + [tool.uv] + package = false + "#, + )?; + + context.temp_dir.child("ab/pyproject.toml").write_str( + r#" + [project] + name = "ab" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["shared[a]"] + + [tool.uv] + package = false + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["leaf>=1"] + + [project.optional-dependencies] + a = ["leaf==1.0.0"] + b = ["leaf==2.0.0"] + + [tool.uv] + package = false + "#, + )?; + + // The dependency markers match those generated for the workspace's conflicting extras. + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 5 + requires-python = ">=3.11" + conflicts = [[ + { package = "shared", extra = "a" }, + { package = "shared", extra = "b" }, + ]] + + [manifest] + members = ["aa", "ab", "root", "shared"] + + [[package]] + name = "aa" + version = "1.0.0" + source = { virtual = "aa" } + dependencies = [{ name = "ab" }] + + [[package]] + name = "ab" + version = "1.0.0" + source = { virtual = "ab" } + dependencies = [ + { name = "shared" }, + { name = "shared", extra = ["a"], marker = "extra == 'extra-6-shared-a'" }, + ] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + + [[package]] + name = "leaf" + version = "2.0.0" + source = { registry = "https://example.com/simple" } + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "aa", marker = "sys_platform == 'linux'" }, + { name = "shared" }, + ] + + [[package]] + name = "shared" + version = "1.0.0" + source = { virtual = "shared" } + dependencies = [ + { name = "leaf", version = "1.0.0", source = { registry = "https://example.com/simple" }, marker = "extra == 'extra-6-shared-a'" }, + { name = "leaf", version = "2.0.0", source = { registry = "https://example.com/simple" }, marker = "extra != 'extra-6-shared-a' or extra == 'extra-6-shared-b'" }, + ] + + [package.optional-dependencies] + a = [{ name = "leaf", version = "1.0.0", source = { registry = "https://example.com/simple" } }] + b = [{ name = "leaf", version = "2.0.0", source = { registry = "https://example.com/simple" } }] + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + leaf==1.0.0 ; sys_platform == 'linux' + leaf==2.0.0 ; sys_platform != 'linux' + "); + + Ok(()) +} + +/// A transitive extra selects the dependency version on a sibling branch as well as its own path. +/// The selector is reachable only on Linux, so each leaf version has a different platform marker. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_extra_activation_on_sibling_branch() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.11" + dependencies = ["shared", "other", "selector ; sys_platform == 'linux'"] + + [tool.uv] + package = false + conflicts = [[{ package = "shared", extra = "a" }, { package = "shared", extra = "b" }]] + + [tool.uv.workspace] + members = ["shared", "other", "selector"] + + [tool.uv.sources] + shared = { workspace = true } + other = { workspace = true } + selector = { workspace = true } + "#, + )?; + + context.temp_dir.child("other/pyproject.toml").write_str( + r#" + [project] + name = "other" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["leaf>=1"] + + [tool.uv] + package = false + "#, + )?; + + context + .temp_dir + .child("selector/pyproject.toml") + .write_str( + r#" + [project] + name = "selector" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["shared[a]"] + + [tool.uv] + package = false + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.11" + + [project.optional-dependencies] + a = ["leaf==1"] + b = ["leaf==2"] + + [tool.uv] + package = false + "#, + )?; + + // Include both leaf versions so the lock records both conflicting branches. + let wheels = context.temp_dir.child("wheels"); + + let (filename, wheel) = WheelBuilder::new(&"leaf".parse()?, &"1.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"leaf".parse()?, &"2.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + context + .lock() + .arg("--offline") + .arg("--no-index") + .arg("--find-links") + .arg("wheels") + .assert() + .success(); + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + leaf==1.0.0 ; sys_platform == 'linux' + leaf==2.0.0 ; sys_platform != 'linux' + "); + + Ok(()) +} + +/// With b disabled, the paths with and without a reach a bridge that requests a. Their union +/// establishes a even though following either path creates a dependency cycle. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_extra_activation_through_complementary_paths() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.11" + dependencies = ["shared"] + + [tool.uv] + package = false + conflicts = [[{ package = "shared", extra = "a" }, { package = "shared", extra = "b" }]] + + [tool.uv.workspace] + members = ["shared"] + + [tool.uv.sources] + shared = { workspace = true } + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["leaf>=1"] + + [project.optional-dependencies] + a = ["leaf==1", "bridge>=3"] + b = ["leaf==2", "bridge==2"] + + [tool.uv] + package = false + "#, + )?; + + // The newer bridge requests shared[a], closing the cycle through either leaf version. + let wheels = context.temp_dir.child("wheels"); + + let (filename, wheel) = WheelBuilder::new(&"leaf".parse()?, &"1.0.0".parse()?) + .requires(&["bridge>=1".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"leaf".parse()?, &"2.0.0".parse()?) + .requires(&["bridge>=1".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"bridge".parse()?, &"2.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"bridge".parse()?, &"3.0.0".parse()?) + .requires(&["shared[a]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + context + .lock() + .arg("--offline") + .arg("--no-index") + .arg("--find-links") + .arg("wheels") + .assert() + .success(); + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + bridge==3.0.0 + leaf==1.0.0 + "); + + Ok(()) +} + +/// Mutually recursive extra requests cannot be resolved from their activation bounds. +/// Export reports the unresolved condition even when the lock has one consistent selection. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_extra_activation_unresolved_recursive_selection() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.11" + dependencies = ["shared"] + + [tool.uv] + package = false + conflicts = [ + [{ package = "shared", extra = "a" }, { package = "shared", extra = "adummy" }], + [{ package = "shared", extra = "b" }, { package = "shared", extra = "bdummy" }], + ] + + [tool.uv.workspace] + members = ["shared"] + + [tool.uv.sources] + shared = { workspace = true } + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["choice-a>=1", "choice-b>=2"] + + [project.optional-dependencies] + a = ["choice-a!=2", "choice-b==2"] + adummy = [] + b = ["choice-a<=2"] + bdummy = [] + + [tool.uv] + package = false + "#, + )?; + + // The choices request the extras that constrain their own versions. + let wheels = context.temp_dir.child("wheels"); + + let (filename, wheel) = WheelBuilder::new(&"choice-a".parse()?, &"1.0.0".parse()?) + .requires(&["shared[a]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-a".parse()?, &"2.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-a".parse()?, &"3.0.0".parse()?) + .requires(&["shared[a]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-b".parse()?, &"2.0.0".parse()?) + .requires(&["shared[b]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-b".parse()?, &"3.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + context + .lock() + .arg("--offline") + .arg("--no-index") + .arg("--find-links") + .arg("wheels") + .assert() + .success(); + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 2 (failure) + ----- stderr ----- + error: Cannot determine whether to export `choice-a==1.0.0 @ registry+wheels` because its dependency markers contain recursive extra conditions + "); + + Ok(()) +} + +/// An unsupported positive cycle must not enable a when the default selection already determines +/// the export. Enabling a would select another consistent pair of versions and disable b. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_extra_activation_retains_default_selection() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.11" + dependencies = ["shared"] + + [tool.uv] + package = false + conflicts = [ + [{ package = "shared", extra = "a" }, { package = "shared", extra = "adummy" }], + [{ package = "shared", extra = "b" }, { package = "shared", extra = "bdummy" }], + ] + + [tool.uv.workspace] + members = ["shared"] + + [tool.uv.sources] + shared = { workspace = true } + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.11" + dependencies = ["choice-a>=1", "choice-b>=1"] + + [project.optional-dependencies] + a = ["choice-a==1", "choice-b==1"] + adummy = [] + b = ["leaf==1"] + bdummy = [] + + [tool.uv] + package = false + "#, + )?; + + // The older choice-a requests a, while the newer choice-b requests b. + let wheels = context.temp_dir.child("wheels"); + + let (filename, wheel) = WheelBuilder::new(&"choice-a".parse()?, &"1.0.0".parse()?) + .requires(&["shared[a]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-a".parse()?, &"2.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-b".parse()?, &"1.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"choice-b".parse()?, &"2.0.0".parse()?) + .requires(&["shared[b]".parse()?]) + .build(); + wheels.child(filename).write_binary(&wheel)?; + + let (filename, wheel) = WheelBuilder::new(&"leaf".parse()?, &"1.0.0".parse()?).build(); + wheels.child(filename).write_binary(&wheel)?; + + context + .lock() + .arg("--offline") + .arg("--no-index") + .arg("--find-links") + .arg("wheels") + .assert() + .success(); + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + choice-a==2.0.0 + choice-b==2.0.0 + leaf==1.0.0 + "); + + Ok(()) +} + +/// Selecting an empty group activates its encoded marker even without group dependency edges. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_empty_group_activation() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = ["shared==1.0.0"] + + [dependency-groups] + dev = [] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [{ name = "shared" }] + + [package.dependency-groups] + dev = [] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "leaf", marker = "extra == 'group-4-root-dev'" }] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--group") + .arg("dev") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + leaf==1.0.0 + shared==1.0.0 + "); + + Ok(()) +} + +/// A group-only export leaves the production project's encoded marker inactive. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_only_group_project_activation() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = [] + + [dependency-groups] + dev = ["shared==1.0.0"] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + + [package.dependency-groups] + dev = [{ name = "shared" }] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "leaf", marker = "extra != 'project-4-root'" }] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--only-group") + .arg("dev") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + leaf==1.0.0 + shared==1.0.0 + "); + + Ok(()) +} + +/// An encoded conflict takes precedence over a literal extra only when its request can reach +/// the marker's context, including through an incompatible path. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_encoded_extra_name_precedence() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = [ + "other[a]==1.0.0", + "shared[extra-6-shared-a]==1.0.0", + "shared[extra-5-other-a]==1.0.0 ; sys_platform == 'linux'", + ] + + [project.optional-dependencies] + feature = ["aaa-bridge==1.0.0 ; sys_platform == 'linux'"] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "other", extra = ["a"] }, + { name = "shared", extra = ["extra-6-shared-a"] }, + { name = "shared", extra = ["extra-5-other-a"], marker = "sys_platform == 'linux'" }, + ] + + [package.optional-dependencies] + feature = [{ name = "aaa-bridge", marker = "sys_platform == 'linux'" }] + + [package.metadata] + provides-extras = ["feature"] + + [[package]] + name = "aaa-bridge" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["a"], marker = "sys_platform != 'linux'" }] + + [[package]] + name = "other" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + + [package.optional-dependencies] + a = [] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "leaf-other", marker = "extra == 'extra-5-other-a'" }] + + [package.optional-dependencies] + a = [] + extra-5-other-a = [] + extra-6-shared-a = [{ name = "leaf", marker = "extra == 'extra-6-shared-a'" }] + + [package.metadata] + provides-extras = ["a", "extra-5-other-a", "extra-6-shared-a"] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + + [[package]] + name = "leaf-other" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + "#, + )?; + + // No dependency requests shared[a], so the marker refers to the literal extra. + // The unrelated other[a] request cannot widen the Linux-only literal extra. + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + leaf==1.0.0 + leaf-other==1.0.0 ; sys_platform == 'linux' + other==1.0.0 + shared==1.0.0 + "); + + // The encoded shared[a] fact is present but false, and takes precedence over the literal extra. + uv_snapshot!(context.filters(), context.export() + .arg("--extra").arg("feature") + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + aaa-bridge==1.0.0 ; sys_platform == 'linux' + leaf-other==1.0.0 ; sys_platform == 'linux' + other==1.0.0 + shared==1.0.0 + "); + + Ok(()) +} + +/// A negative encoded marker must wait for a transitive activation even when a literal extra has +/// the same name. Resolving the encoded fact's presence does not establish its final value. +/// The leaf's self-cycle cannot retain reachability after that guard closes. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_encoded_extra_negative_marker() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = ["shared[extra-6-shared-a]==1.0.0", "bridge-0==1.0.0"] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "shared", extra = ["extra-6-shared-a"] }, + { name = "bridge-0" }, + ] + + [[package]] + name = "bridge-0" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "bridge-1" }] + + [[package]] + name = "bridge-1" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "bridge-2" }] + + [[package]] + name = "bridge-2" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["a"] }] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "leaf", marker = "extra != 'extra-6-shared-a'" }] + + [package.optional-dependencies] + a = [] + extra-6-shared-a = [] + + [package.metadata] + provides-extras = ["a", "extra-6-shared-a"] + + [[package]] + name = "leaf" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "leaf" }] + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + bridge-0==1.0.0 + bridge-1==1.0.0 + bridge-2==1.0.0 + shared==1.0.0 + "); + + Ok(()) +} + +/// The only path that activates shared[a] depends on that extra being inactive. +/// Export must report the recursive condition instead of choosing a traversal-dependent result. +#[cfg(feature = "test-universal")] +#[test] +fn requirements_txt_recursive_negative_extra_marker() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = ["shared==1.0.0"] + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [{ name = "shared" }] + + [[package]] + name = "shared" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "bridge", marker = "extra != 'a'" }] + + [package.optional-dependencies] + a = [] + + [[package]] + name = "bridge" + version = "1.0.0" + source = { registry = "https://example.com/simple" } + dependencies = [{ name = "shared", extra = ["a"] }] + "#, + )?; + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 2 (failure) + ----- stderr ----- + error: Cannot determine whether to export `bridge==1.0.0 @ registry+https://example.com/simple` because its dependency markers contain recursive extra conditions + "); + + // The unresolved bridge does not affect the reachability of the emitted package. + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes") + .arg("--no-emit-package").arg("bridge"), @" + exit_code: 0 (success) + ----- stdout ----- + shared==1.0.0 + "); + + Ok(()) +} + +/// The two possible extra selections reach different virtual packages. Installable formats omit +/// both virtual packages, while CycloneDX must distinguish which virtual component is present. +#[cfg(feature = "test-universal")] +#[test] +fn export_recursive_extra_marker_on_virtual_packages() -> Result<()> { + let context = uv_test::test_context!("3.13"); + + context.temp_dir.child("pyproject.toml").write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.9" + dependencies = ["shared"] + + [tool.uv.workspace] + members = ["shared", "bridge-a", "bridge-b"] + + [tool.uv.sources] + shared = { workspace = true } + "#, + )?; + + context.temp_dir.child("shared/pyproject.toml").write_str( + r#" + [project] + name = "shared" + version = "1.0.0" + requires-python = ">=3.9" + dependencies = ["bridge-a", "bridge-b"] + + [project.optional-dependencies] + a = [] + b = [] + "#, + )?; + + context + .temp_dir + .child("bridge-a/pyproject.toml") + .write_str( + r#" + [project] + name = "bridge-a" + version = "1.0.0" + requires-python = ">=3.9" + dependencies = ["shared[a]"] + + [tool.uv] + package = false + "#, + )?; + + context + .temp_dir + .child("bridge-b/pyproject.toml") + .write_str( + r#" + [project] + name = "bridge-b" + version = "1.0.0" + requires-python = ">=3.9" + dependencies = ["shared[b]"] + + [tool.uv] + package = false + "#, + )?; + + context.temp_dir.child("uv.lock").write_str( + r#" + version = 1 + revision = 3 + requires-python = ">=3.9" + + [manifest] + members = ["root", "shared", "bridge-a", "bridge-b"] + + [[package]] + name = "root" + version = "0.1.0" + source = { virtual = "." } + dependencies = [{ name = "shared" }] + + [[package]] + name = "shared" + version = "1.0.0" + source = { editable = "shared" } + dependencies = [ + { name = "bridge-a", marker = "extra != 'b'" }, + { name = "bridge-b", marker = "extra != 'a'" }, + ] + + [package.optional-dependencies] + a = [] + b = [] + + [[package]] + name = "bridge-a" + version = "1.0.0" + source = { virtual = "bridge-a" } + dependencies = [{ name = "shared", extra = ["a"] }] + + [[package]] + name = "bridge-b" + version = "1.0.0" + source = { virtual = "bridge-b" } + dependencies = [{ name = "shared", extra = ["b"] }] + "#, + )?; + + // Requirements and pylock omit the virtual bridges, whose selection is unresolved. + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--no-annotate") + .arg("--no-hashes"), @" + exit_code: 0 (success) + ----- stdout ----- + -e ./shared + "); + + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--format").arg("pylock.toml"), @r#" + exit_code: 0 (success) + ----- stdout ----- + lock-version = "1.0" + created-by = "uv" + requires-python = ">=3.9" + + [[packages]] + name = "shared" + directory = { path = "shared", editable = true } + "#); + + // CycloneDX includes virtual packages, so it must resolve which bridge is present. + uv_snapshot!(context.filters(), context.export() + .arg("--frozen") + .arg("--no-header") + .arg("--format").arg("cyclonedx1.5"), @" + exit_code: 2 (failure) + ----- stderr ----- + warning: `uv export --format=cyclonedx1.5` is experimental and may change without warning. Pass `--preview-features sbom-export` to disable this warning. + error: Cannot determine whether to export `bridge-a==1.0.0 @ virtual+bridge-a` because its dependency markers contain recursive extra conditions + "); + + Ok(()) +} + #[cfg(feature = "test-universal")] #[test] fn requirements_txt_project_extra() -> Result<()> {