Skip to content

Commit c32f34a

Browse files
authored
Preserve original constraint declarations in Constraints (#21459)
`Constraints` currently stores only normalized requirements for resolution, with extras and empty constraints removed. Callers that need the original declarations and their hashes have to keep a separate collection. Preserve the original `NameRequirementSpecification`s in input order alongside the resolver map. Use this representation for build constraints in `uv pip install`, `uv pip sync`, and `uv build`, preserving their existing hash-checking and resolution behavior.
1 parent fd89f63 commit c32f34a

9 files changed

Lines changed: 147 additions & 33 deletions

File tree

‎crates/uv-configuration/src/constraints.rs‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ use std::borrow::Cow;
33
use either::Either;
44
use rustc_hash::FxHashMap;
55

6-
use uv_distribution_types::{Requirement, RequirementSource};
6+
use uv_distribution_types::{NameRequirementSpecification, Requirement, RequirementSource};
77
use uv_normalize::PackageName;
88
use uv_pep508::MarkerTree;
99

1010
/// A set of constraints for a set of requirements.
1111
#[derive(Debug, Default, Clone)]
12-
pub struct Constraints(FxHashMap<PackageName, Vec<Requirement>>);
12+
pub struct Constraints {
13+
/// Original declarations, including hashes, for hash verification.
14+
specifications: Vec<NameRequirementSpecification>,
15+
/// Constraints grouped by package name.
16+
requirements: FxHashMap<PackageName, Vec<Requirement>>,
17+
}
1318

1419
impl Constraints {
1520
/// Create a new set of constraints from a set of requirements.
1621
pub fn from_requirements(requirements: impl Iterator<Item = Requirement>) -> Self {
22+
Self::from_specifications(requirements.map(NameRequirementSpecification::from))
23+
}
24+
25+
/// Create constraints while retaining their hashes and original declarations.
26+
pub fn from_specifications(
27+
specifications: impl IntoIterator<Item = NameRequirementSpecification>,
28+
) -> Self {
29+
let specifications: Vec<_> = specifications.into_iter().collect();
1730
let mut constraints: FxHashMap<PackageName, Vec<Requirement>> = FxHashMap::default();
18-
for requirement in requirements {
31+
for specification in &specifications {
32+
let requirement = &specification.requirement;
1933
// Skip empty constraints.
2034
if let RequirementSource::Registry { specifier, .. } = &requirement.source
2135
&& specifier.is_empty()
@@ -29,20 +43,28 @@ impl Constraints {
2943
.push(Requirement {
3044
// We add and apply constraints independent of their extras.
3145
extras: Box::new([]),
32-
..requirement
46+
..requirement.clone()
3347
});
3448
}
35-
Self(constraints)
49+
Self {
50+
specifications,
51+
requirements: constraints,
52+
}
53+
}
54+
55+
/// Return the original declarations, including hashes, in input order.
56+
pub fn specifications(&self) -> impl Iterator<Item = &NameRequirementSpecification> {
57+
self.specifications.iter()
3658
}
3759

3860
/// Return an iterator over all [`Requirement`]s in the constraint set.
3961
pub fn requirements(&self) -> impl Iterator<Item = &Requirement> {
40-
self.0.values().flat_map(|requirements| requirements.iter())
62+
self.requirements.values().flatten()
4163
}
4264

4365
/// Get the constraints for a package.
4466
pub fn get(&self, name: &PackageName) -> Option<&Vec<Requirement>> {
45-
self.0.get(name)
67+
self.requirements.get(name)
4668
}
4769

4870
/// Apply the constraints to a set of requirements.

‎crates/uv/src/commands/build_frontend.rs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use uv_distribution_filename::{
2323
DistFilename, SourceDistExtension, SourceDistFilename, WheelFilename,
2424
};
2525
use uv_distribution_types::{
26-
ConfigSettings, DependencyMetadata, ExtraBuildVariables, IndexLocations, PackageConfigSettings,
27-
Requirement, SourceDist,
26+
ConfigSettings, DependencyMetadata, ExtraBuildVariables, IndexLocations,
27+
NameRequirementSpecification, PackageConfigSettings, Requirement, SourceDist,
2828
};
2929
use uv_errors::{ErrorOptions, Hinted, Hints, write_error_chain_with_options};
3030
use uv_fs::{Simplified, normalize_path, relative_to};
@@ -652,11 +652,13 @@ async fn build_package(
652652
HashStrategy::default()
653653
};
654654

655-
let build_constraints = Constraints::from_requirements(
656-
build_constraints
657-
.into_iter()
658-
.map(|constraint| constraint.requirement)
659-
.chain(build_constraints_from_workspace.iter().cloned()),
655+
let build_constraints = Constraints::from_specifications(
656+
build_constraints.into_iter().chain(
657+
build_constraints_from_workspace
658+
.iter()
659+
.cloned()
660+
.map(NameRequirementSpecification::from),
661+
),
660662
);
661663

662664
// Initialize the registry client.

‎crates/uv/src/commands/pip/install.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub(crate) async fn pip_install(
196196
.collect();
197197

198198
// Read build constraints.
199-
let build_constraints: Vec<NameRequirementSpecification> =
199+
let build_constraints = Constraints::from_specifications(
200200
operations::read_constraints(build_constraints, &client_builder)
201201
.await?
202202
.into_iter()
@@ -205,8 +205,8 @@ pub(crate) async fn pip_install(
205205
.iter()
206206
.cloned()
207207
.map(NameRequirementSpecification::from),
208-
)
209-
.collect();
208+
),
209+
);
210210

211211
// Detect the current Python interpreter.
212212
let environment = if target.is_some() || prefix.is_some() {
@@ -473,19 +473,14 @@ pub(crate) async fn pip_install(
473473
HashStrategy::from_requirements(
474474
std::iter::empty(),
475475
build_constraints
476-
.iter()
476+
.specifications()
477477
.map(|entry| (&entry.requirement, entry.hashes.as_slice())),
478478
Some(&marker_env),
479479
HashCheckingMode::Verify,
480480
)?
481481
} else {
482482
HashStrategy::default()
483483
};
484-
let build_constraints = Constraints::from_requirements(
485-
build_constraints
486-
.iter()
487-
.map(|constraint| constraint.requirement.clone()),
488-
);
489484

490485
// Initialize any shared state.
491486
let state = SharedState::default();

‎crates/uv/src/commands/pip/sync.rs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ pub(crate) async fn pip_sync(
150150
}
151151

152152
// Read build constraints.
153-
let build_constraints =
154-
operations::read_constraints(build_constraints, &client_builder).await?;
153+
let build_constraints = Constraints::from_specifications(
154+
operations::read_constraints(build_constraints, &client_builder).await?,
155+
);
155156

156157
// Validate that the requirements are non-empty.
157158
if !allow_empty_requirements {
@@ -344,19 +345,14 @@ pub(crate) async fn pip_sync(
344345
HashStrategy::from_requirements(
345346
std::iter::empty(),
346347
build_constraints
347-
.iter()
348+
.specifications()
348349
.map(|entry| (&entry.requirement, entry.hashes.as_slice())),
349350
Some(&marker_env),
350351
HashCheckingMode::Verify,
351352
)?
352353
} else {
353354
HashStrategy::default()
354355
};
355-
let build_constraints = Constraints::from_requirements(
356-
build_constraints
357-
.iter()
358-
.map(|constraint| constraint.requirement.clone()),
359-
);
360356

361357
// Initialize any shared state.
362358
let state = SharedState::default();

‎crates/uv/tests/build/build.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,18 @@ fn build_sha() -> Result<()> {
13461346
.touch()?;
13471347
project.child("README").touch()?;
13481348

1349-
// Reject an incorrect hash.
1349+
// Validate the original declaration, including extras, before dropping empty constraints.
13501350
let constraints = project.child("constraints.txt");
1351+
constraints.write_str("hatchling[foo]")?;
1352+
1353+
uv_snapshot!(context.filters(), context.build().arg("--build-constraint").arg("constraints.txt").arg("--require-hashes").current_dir(&project), @"
1354+
exit_code: 2 (failure)
1355+
----- stderr -----
1356+
error: Failed to build `[TEMP_DIR]/project`
1357+
cause: In `--require-hashes` mode, all requirements must have their versions pinned with `==`, but found: hatchling[foo]
1358+
");
1359+
1360+
// Reject an incorrect hash.
13511361
constraints.write_str(indoc::indoc! {r"
13521362
hatchling==1.22.4 \
13531363
--hash=sha256:a248cb506794bececcddeddb1678bc722f9cfcacf02f98f7c0af6b9ed893caf2 \

‎crates/uv/tests/it/main.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ mod network;
2121
#[cfg(feature = "test-pypi")]
2222
mod publish;
2323

24+
mod requirements;
25+
2426
#[cfg(unix)]
2527
mod resource_limits;
2628

‎crates/uv/tests/it/requirements.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use anyhow::Result;
2+
use assert_fs::prelude::*;
3+
use indoc::indoc;
4+
5+
use uv_client::BaseClientBuilder;
6+
use uv_requirements::{RequirementsSource, RequirementsSpecification};
7+
8+
#[tokio::test]
9+
async fn constraint_specifications_preserve_hashes() -> Result<()> {
10+
let temp_dir = assert_fs::TempDir::new()?;
11+
let constraints_txt = temp_dir.child("constraints.txt");
12+
constraints_txt.write_str(indoc! {r"
13+
packaging==23.2 \
14+
--hash=sha256:1111111111111111111111111111111111111111111111111111111111111111
15+
hatchling==1.20.0 \
16+
--hash=sha256:2222222222222222222222222222222222222222222222222222222222222222 \
17+
--hash=sha256:3333333333333333333333333333333333333333333333333333333333333333
18+
"})?;
19+
20+
let specification = RequirementsSpecification::from_sources(
21+
&[],
22+
&[RequirementsSource::RequirementsTxt(
23+
constraints_txt.to_path_buf(),
24+
)],
25+
&[],
26+
&[],
27+
None,
28+
&BaseClientBuilder::default(),
29+
)
30+
.await?;
31+
32+
insta::assert_debug_snapshot!(
33+
specification
34+
.constraints
35+
.iter()
36+
.map(|entry| (entry.requirement.to_string(), entry.hashes.as_slice()))
37+
.collect::<Vec<_>>(),
38+
@r#"
39+
[
40+
(
41+
"packaging==23.2",
42+
[
43+
"sha256:1111111111111111111111111111111111111111111111111111111111111111",
44+
],
45+
),
46+
(
47+
"hatchling==1.20.0",
48+
[
49+
"sha256:2222222222222222222222222222222222222222222222222222222222222222",
50+
"sha256:3333333333333333333333333333333333333333333333333333333333333333",
51+
],
52+
),
53+
]
54+
"#
55+
);
56+
57+
Ok(())
58+
}

‎crates/uv/tests/pip/pip_sync.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5330,10 +5330,12 @@ fn compatible_build_constraint() -> Result<()> {
53305330
requirements_txt.write_str("requests==1.2")?;
53315331

53325332
let constraints_txt = context.temp_dir.child("build_constraints.txt");
5333-
constraints_txt.write_str("setuptools>=40")?;
5333+
// Verify mode ignores hashes on unpinned constraints and does not activate extras.
5334+
constraints_txt.write_str("setuptools[foo]>=40 --hash=sha256:incorrect")?;
53345335

53355336
uv_snapshot!(context.pip_sync()
53365337
.arg("requirements.txt")
5338+
.arg("--verify-hashes")
53375339
.arg("--build-constraint")
53385340
.arg("build_constraints.txt"), @"
53395341
exit_code: 0 (success)

‎crates/uv/tests/pip_install/pip_install.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8546,6 +8546,33 @@ fn require_hashes_build_dependencies() -> Result<()> {
85468546
"
85478547
);
85488548

8549+
// Build constraints retain their hashes while ignoring extras during resolution.
8550+
let constraints_txt = context.temp_dir.child("build_constraints.txt");
8551+
constraints_txt.write_str("hatchling[foo]==1.20.0 --hash=sha256:incorrect")?;
8552+
8553+
uv_snapshot!(context.pip_install()
8554+
.arg("--index-url").arg(server.index_url())
8555+
.arg("--no-binary").arg("a")
8556+
.arg("-r").arg("requirements.txt")
8557+
.arg("--require-hashes")
8558+
.arg("--build-constraint").arg("build_constraints.txt")
8559+
.arg("--reinstall")
8560+
.arg("--no-cache"), @"
8561+
exit_code: 1 (failure)
8562+
----- stderr -----
8563+
Resolved 1 package in [TIME]
8564+
error: Failed to download and build `a==1.0.0`
8565+
cause: Failed to install requirements from `build-system.requires`
8566+
cause: Failed to download `hatchling==1.20.0`
8567+
cause: Hash mismatch for `hatchling==1.20.0`
8568+
8569+
Expected:
8570+
sha256:incorrect
8571+
8572+
Computed:
8573+
sha256:872c63aa7e8aca85e8dba07b05c6a9b28d5a149fe00638f1a47e36930197248f
8574+
");
8575+
85498576
Ok(())
85508577
}
85518578

0 commit comments

Comments
 (0)