Skip to content

Intersect hashes from repeated constraints - #21956

Open
charliermarsh wants to merge 4 commits into
mainfrom
charlie/intersect-constraint-hashes
Open

charliermarsh wants to merge 4 commits into
mainfrom
charlie/intersect-constraint-hashes

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

When constraints repeat the same registry pin with different hash lists, we currently keep the last list. Reordering constraints, including sorting them when writing a lockfile, can therefore change which artifact hashes we accept.

Intersect those lists using the same rules as requirement/constraint pairs. For example, {A, B} and {B, C} allow only B; disjoint lists report a conflict. Hashless constraints add no restriction, and archive URL hashes retain their existing validation rules.

This applies to ordinary and build constraints and makes their hash restrictions independent of declaration order.

Related to #21951.

@astral-sh-bot

astral-sh-bot Bot commented Sep 24, 2026

Copy link
Copy Markdown

uv test inventory changes

This PR changes the tests when compared with the main base revision.

  • Added tests: 2
  • Removed tests: 0
  • Changed suites: 1
uv::sync: +2 / -0

Added:

  • uv::sync::sync::project_build_hashes_conflicting_constraints
  • uv::sync::sync::project_build_hashes_constraint_intersection

Removed: none

@charliermarsh
charliermarsh force-pushed the charlie/intersect-constraint-hashes branch from 1db9344 to 2050ea8 Compare October 24, 2026 15:40
Comment on lines +699 to +700
#[error("Conflicting hashes for `{0}`: no hash is allowed by all requirements and constraints")]
NoIntersection(String),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Update the remaining NoIntersection snapshots

require_hashes_repeated_requirements_constraint still contains four snapshots expecting the old diagnostic in pip_install.rs at lines 9867, 9879, 9896, and 9908. Those scenarios still reach NoIntersection, so this message change makes their assertions fail. Update those expectations alongside require_hashes_constraint.

Comment thread crates/uv/tests/build/build.rs Outdated

Computed:
sha256:[BUILD_HASH]
cause: Conflicting hashes for `build-dependency @ file://[TEMP_DIR]/wheels/build_dependency-1.0.0-py3-none-any.whl`: no hash is allowed by all requirements and constraints

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Expect the direct-reference conflict diagnostic

These file URL constraints produce VersionId::Path, so the new combination step calls merge_digests and returns ConflictingArchiveUrlHashes, including both conflicting SHA-256 digests. NoIntersection is only returned for registry identities. This snapshot therefore cannot match the implementation; update it to expect the direct-reference diagnostic.

Comment on lines 4213 to +4216
context
.temp_dir
.child("backend-executed")
.assert(predicate::path::exists());
.assert(predicate::path::missing());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reset conflicting constraints before later build scenarios

After this assertion, the command-line constraint still contains incorrect_hash while the workspace contains build_hash. The subsequent isolation and dynamic-requirement scenarios reuse that conflicting pair. build_package constructs the hash strategy before handling isolation, so they now stop at NoIntersection instead of reaching their expected behavior. Explicitly reset the constraint inputs between scenarios, using a valid common build-dependency hash before the dynamic-requirement checks.

Comment on lines +344 to +345
if let Some(existing) = constraint_hashes.get(&id) {
digests = combine_constraint_hashes(&id, digests, existing, requirement)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Allow corrected constraints to regenerate older lockfiles

An older lockfile can contain repeated build constraints with disjoint hashes because those previously used the last list. After correcting those constraints in pyproject.toml, uv lock, including --upgrade, still fails: do_lock constructs a strategy from the old manifest at uv-lock-operations/src/lock.rs:663 before checking whether the lock is stale. This new error therefore prevents regeneration without manually editing or deleting the lockfile. Handle obsolete constraint conflicts through recoverable lock validation, while continuing to reject conflicting current constraints.

Comment on lines +615 to +618
let hash_checking = match (build_isolation, build_hash_checking) {
(BuildIsolation::Shared, _) => None,
(_, HashCheckingMode::Require) => Some(HashCheckingMode::Require),
(_, HashCheckingMode::Verify) => hash_checking,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Honor package-specific disabled isolation

With disjoint hashes on repeated build constraints, uv build --wheel --no-build-isolation-package project still falls through these arms and fails during hash-strategy construction. For a project named project, the build frontend selects a shared environment and skips installing build dependencies, just as with global --no-build-isolation. This makes a previously working non-isolated build fail on unused hashes. Base the exemption on effective isolation for the package being built, retaining validation for isolated builds.

Comment on lines +19109 to +19114
// The same conflict is rejected for ordinary constraints in require-hashes mode.
context.temp_dir.child("constraints.txt").write_str(&formatdoc! {"
build-dependency==1.0.0 --hash=sha256:0000000000000000000000000000000000000000000000000000000000000000
build-dependency==1.0.0 --hash=sha256:{hash}
"})?;
uv_snapshot!(context.filters(), context.pip_install().args([

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Give the ordinary constraint regression its own pip-install test

This ordinary pip install scenario is independent of the surrounding project lock checks, but inherits their backend and three generated wheels and is omitted when running the targeted pip-install suite. Move it into a separately named test in pip_install/pip_install.rs, beside require_hashes_repeated_requirements_constraint. That nearby test provides the explicit requirements-file and local ok wheel pattern needed for a smaller fixture.

@zsol

zsol commented Oct 27, 2026

Copy link
Copy Markdown
Member

Codex found these things which seem concerning on the surface but I haven't had time to look into them and understand in any depth. Capturing them here for future Zsolt

Codex reproduced the following with builds of base 6fe4f0f5c and head 58eac24bb. The four focused integration tests passed on the head.

  1. uv lock can reject the lock it just wrote. A project depends on a wheel with hash X at runtime only. Two build constraints for that pin allow {Y} and {Y, X}. The first uv lock succeeds and records X. A second uv lock with identical inputs and a fresh uv sync --frozen both exit 2. All three pass on the base. Could the build-only conflict wait until something actually needs that dependency for a build? Hash policy merge · Frozen sync

  2. Fixing a build constraint in pyproject.toml can leave an old lockfile stuck. The base can write a lock with two disjoint lists. After removing the incorrect declaration from the project, both uv lock and uv lock --upgrade on the head still fail on the removed hash. A fresh lock succeeds with the corrected project. uv builds the old hash policy before it checks whether the lock's build constraints are stale. Old policy construction · Manifest comparison

  3. Cross-platform installs check a host wheel against the target's hash. On macOS, put a Windows-only constraint allowing the Windows wheel first, then an unconditional constraint allowing both the macOS and Windows wheels. With --python-platform windows, both uv pip install and uv pip sync pass on the base. The head selects the macOS wheel for the build dependency, then rejects it because it expects the Windows digest. pip compile already evaluates build hashes with the build interpreter's markers. Install · Sync · Compile

  4. An unused constraint can block an unrelated install. Installing a correctly hashed ok==2.0.0 with --require-hashes --no-deps succeeds on the base but exits 2 on the head if two constraints for unused==1 have disjoint hashes. Could uv record that no artifact is allowed for unused==1 and report the conflict if resolution selects it? Constraint collection · Documented constraint behavior

  5. The package-specific isolation exemption behaves differently from the global one. For a project named ok, uv build --wheel --no-build-isolation-package ok succeeds on the base and fails on the head with conflicting build hashes. Global --no-build-isolation succeeds on the head too. Both forms use the same shared environment for this project. Hash-checking guard · Frontend isolation decision

  6. Local-wheel constraints can still depend on declaration order. For one file:// wheel with two distinct SHA-256 digests A and B, a metadata-only uv lock succeeds with [A] followed by [A, B], and fails when the two lists are reversed. An actual download rejects the impossible pair in either order. The merge checks incoming digests but can miss a conflict already present in the destination list. Digest merge

One compatibility detail seems worth documenting. The correct SHA-256 and SHA-512 of the same wheel each work alone. Put them in separate registry constraints and the head rejects the pair in either order. Intersecting literal digests means both files need to share an identical algorithm and digest. Registry intersection

This branch was previously deployed

1 inactive deployment
automations — 58eac24b Deployed Oct 24, 2026 by charliermarsh via review / security review #52549
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants