Skip to content

Commit 303df4f

Browse files
authored
Fix exclude-newer handling when dists for the same package straddle the cutoff (#21539)
## Summary Apply `exclude-newer` before build and binary settings so files uploaded after the cutoff are recognised as excluded. Previously, `--no-build` or `--only-binary` could make a post-cutoff sdist look merely build-disabled; `--no-binary` could do the same for a wheel. Their hashes could then appear in generated requirements, or the files and hashes could be included in lockfiles. Existing lockfiles containing affected dists may change when regenerated with `uv lock --refresh`. I was unaware that it was a bug that we emit hashes for the `no-build`/`no-binary` cases so I preserved them. But this PR doesn't majorly conflict with that fix. > **Note**: There are separate commits for ease of review. ## Test Plan I added some additional stuff to the test context and packse to make it easier to test this without needing to rely on PyPI or using horrific mock messes. There are two new tests which cover the two variations of the issue.
1 parent 6b05fc3 commit 303df4f

7 files changed

Lines changed: 333 additions & 57 deletions

File tree

‎crates/uv-distribution-types/src/prioritized_distribution.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,12 @@ impl PrioritizedDist {
540540
adjusted_wheels.push(wheel.clone());
541541
}
542542

543-
let sdist = self.0.source.as_ref().map(|(sdist, _)| sdist.clone());
543+
let sdist = self
544+
.0
545+
.source
546+
.as_ref()
547+
.filter(|(_, compatibility)| !compatibility.is_excluded())
548+
.map(|(sdist, _)| sdist.clone());
544549
Some(RegistryBuiltDist {
545550
wheels: adjusted_wheels,
546551
best_wheel_index: adjusted_best_index,
@@ -565,6 +570,7 @@ impl PrioritizedDist {
565570
.0
566571
.wheels
567572
.iter()
573+
.filter(|(_, compatibility)| !compatibility.is_excluded())
568574
.map(|(wheel, _)| wheel.clone())
569575
.collect();
570576
Some(sdist)

‎crates/uv-resolver/src/version_map.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -729,18 +729,18 @@ impl VersionMapLazy {
729729
excluded: bool,
730730
upload_time: Option<i64>,
731731
) -> SourceDistCompatibility {
732-
// Check if builds are disabled
733-
if self.no_build {
734-
return SourceDistCompatibility::Incompatible(IncompatibleSource::NoBuild);
735-
}
736-
737732
// Check if after upload time cutoff
738733
if excluded {
739734
return SourceDistCompatibility::Incompatible(IncompatibleSource::ExcludeNewer(
740735
upload_time,
741736
));
742737
}
743738

739+
// Check if builds are disabled
740+
if self.no_build {
741+
return SourceDistCompatibility::Incompatible(IncompatibleSource::NoBuild);
742+
}
743+
744744
// Check if yanked
745745
if let Some(yanked) = yanked {
746746
if yanked.is_yanked()
@@ -790,16 +790,16 @@ impl VersionMapLazy {
790790
excluded: bool,
791791
upload_time: Option<i64>,
792792
) -> WheelCompatibility {
793-
// Check if binaries are disabled
794-
if self.no_binary {
795-
return WheelCompatibility::Incompatible(IncompatibleWheel::NoBinary);
796-
}
797-
798793
// Check if after upload time cutoff
799794
if excluded {
800795
return WheelCompatibility::Incompatible(IncompatibleWheel::ExcludeNewer(upload_time));
801796
}
802797

798+
// Check if binaries are disabled
799+
if self.no_binary {
800+
return WheelCompatibility::Incompatible(IncompatibleWheel::NoBinary);
801+
}
802+
803803
// Check if yanked
804804
if let Some(yanked) = yanked {
805805
if yanked.is_yanked() && !self.allowed_yanks.contains(name, version) {

‎crates/uv-test/src/lib.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,13 @@ impl TestContext {
776776
self
777777
}
778778

779+
/// Add custom filters to the `TestContext`.
780+
#[must_use]
781+
pub fn with_filters(mut self, filters: impl IntoIterator<Item = (String, String)>) -> Self {
782+
self.filters.extend(filters);
783+
self
784+
}
785+
779786
// Unsets the git credential helper using temp home gitconfig
780787
#[must_use]
781788
pub fn with_unset_git_credential_helper(self) -> Self {

‎crates/uv-test/src/packse/scenario.rs‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,19 @@ pub struct PackageMetadata {
106106
#[serde(default)]
107107
pub extras: BTreeMap<ExtraName, Vec<Requirement>>,
108108

109-
/// Whether to produce a source distribution.
110-
#[serde(default = "default_true")]
111-
pub sdist: bool,
112-
113-
/// Whether to produce a wheel.
114-
#[serde(default = "default_true")]
115-
pub wheel: bool,
109+
/// Whether to produce a source distribution, and optionally its metadata.
110+
#[serde(
111+
default = "default_artifact",
112+
deserialize_with = "deserialize_artifact"
113+
)]
114+
pub sdist: Option<ArtifactMetadata>,
115+
116+
/// Whether to produce wheels, and optionally their shared metadata.
117+
#[serde(
118+
default = "default_artifact",
119+
deserialize_with = "deserialize_artifact"
120+
)]
121+
pub wheel: Option<ArtifactMetadata>,
116122

117123
/// Whether this version is yanked.
118124
#[serde(default)]
@@ -124,6 +130,33 @@ pub struct PackageMetadata {
124130
pub wheel_tags: Vec<WheelTag>,
125131
}
126132

133+
fn deserialize_artifact<'de, D>(
134+
deserializer: D,
135+
) -> std::result::Result<Option<ArtifactMetadata>, D::Error>
136+
where
137+
D: serde::Deserializer<'de>,
138+
{
139+
#[derive(Debug, Deserialize)]
140+
#[serde(untagged)]
141+
enum Helper {
142+
Bool(bool),
143+
Metadata(ArtifactMetadata),
144+
}
145+
146+
match Helper::deserialize(deserializer)? {
147+
Helper::Bool(false) => Ok(None),
148+
Helper::Bool(true) => Ok(Some(ArtifactMetadata::default())),
149+
Helper::Metadata(metadata) => Ok(Some(metadata)),
150+
}
151+
}
152+
153+
/// Metadata advertised for an artifact by the Packse Simple API.
154+
#[derive(Debug, Default, Deserialize)]
155+
#[serde(deny_unknown_fields)]
156+
pub struct ArtifactMetadata {
157+
pub upload_time: Option<String>,
158+
}
159+
127160
/// A validated three-component compatibility tag for generated wheels.
128161
#[derive(Clone, Debug)]
129162
pub struct WheelTag(String);
@@ -294,8 +327,9 @@ fn default_requires_python() -> Option<VersionSpecifiers> {
294327
Some(VersionSpecifiers::from_str(">=3.12").expect("default requires-python should be valid"))
295328
}
296329

297-
fn default_true() -> bool {
298-
true
330+
#[expect(clippy::unnecessary_wraps)] // Must return `Option` for serde `default`
331+
fn default_artifact() -> Option<ArtifactMetadata> {
332+
Some(ArtifactMetadata::default())
299333
}
300334

301335
fn default_python() -> PythonVersion {

‎crates/uv-test/src/packse/server.rs‎

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct DistInfo {
3636
filename: String,
3737
sha256: String,
3838
requires_python: Option<VersionSpecifiers>,
39-
upload_time: &'static str,
39+
upload_time: Option<String>,
4040
yanked: bool,
4141
}
4242

@@ -73,6 +73,7 @@ struct ServerIndex {
7373
/// When [`PackseServer`] is dropped, the background thread and server are shut down.
7474
pub struct PackseServer {
7575
server: HttpServer,
76+
index: Arc<ServerIndex>,
7677
}
7778

7879
impl PackseServer {
@@ -105,11 +106,12 @@ impl PackseServer {
105106

106107
fn start(scenario: &Scenario, hashes: bool) -> Self {
107108
let index = Arc::new(build_server_index(scenario));
109+
let server_index = Arc::clone(&index);
108110
let server = HttpServer::start(move |request, server_uri| {
109-
handle_request(request, server_uri, &index, hashes)
111+
handle_request(request, server_uri, &server_index, hashes)
110112
});
111113

112-
Self { server }
114+
Self { server, index }
113115
}
114116

115117
/// The Simple API index URL (e.g., `http://127.0.0.1:PORT/simple/`).
@@ -121,6 +123,15 @@ impl PackseServer {
121123
pub fn file_url(&self, filename: &str) -> String {
122124
format!("{}/files/{filename}", self.server.url())
123125
}
126+
127+
/// Return the filename and advertised SHA-256 digest of each distribution.
128+
pub fn files(&self) -> impl Iterator<Item = (&str, &str)> {
129+
self.index
130+
.packages
131+
.values()
132+
.flat_map(|package| package.dists.iter())
133+
.map(|dist| (dist.filename.as_str(), dist.sha256.as_str()))
134+
}
124135
}
125136

126137
/// Build the complete [`ServerIndex`] from a scenario and cached build dependencies.
@@ -132,7 +143,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
132143
let mut dists = Vec::new();
133144

134145
for (version, meta) in &package.versions {
135-
if meta.wheel {
146+
if let Some(wheel_metadata) = &meta.wheel {
136147
let tags = if meta.wheel_tags.is_empty() {
137148
vec!["py3-none-any"]
138149
} else {
@@ -154,13 +165,13 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
154165
filename,
155166
sha256,
156167
requires_python: meta.requires_python.clone(),
157-
upload_time: PACKSE_UPLOAD_TIME,
168+
upload_time: wheel_metadata.upload_time.clone(),
158169
yanked: meta.yanked,
159170
});
160171
}
161172
}
162173

163-
if meta.sdist {
174+
if let Some(sdist_metadata) = &meta.sdist {
164175
let (filename, bytes) = generate_sdist(
165176
package_name,
166177
version,
@@ -174,7 +185,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
174185
filename,
175186
sha256,
176187
requires_python: meta.requires_python.clone(),
177-
upload_time: PACKSE_UPLOAD_TIME,
188+
upload_time: sdist_metadata.upload_time.clone(),
178189
yanked: meta.yanked,
179190
});
180191
}
@@ -203,7 +214,7 @@ fn build_server_index(scenario: &Scenario) -> ServerIndex {
203214
filename: artifact.filename.to_string(),
204215
sha256: artifact.sha256.to_string(),
205216
requires_python: None,
206-
upload_time: PACKSE_UPLOAD_TIME,
217+
upload_time: None,
207218
yanked: false,
208219
});
209220
}
@@ -355,7 +366,7 @@ fn build_simple_api_response(
355366
"filename": dist.filename,
356367
"url": url,
357368
"hashes": {},
358-
"upload-time": dist.upload_time,
369+
"upload-time": dist.upload_time.as_deref().unwrap_or(PACKSE_UPLOAD_TIME),
359370
});
360371
if hashes {
361372
file_obj["hashes"] = json!({ "sha256": dist.sha256 });
@@ -430,6 +441,67 @@ mod tests {
430441
);
431442
}
432443

444+
#[tokio::test]
445+
async fn serves_artifact_upload_times() -> Result<()> {
446+
let scenario = toml::from_str::<Scenario>(
447+
r#"
448+
name = "artifact-upload-times"
449+
450+
[root]
451+
requires = ["a"]
452+
453+
[expected]
454+
satisfiable = true
455+
456+
[packages.a.versions."1.0.0"]
457+
sdist = { upload_time = "2024-03-23T00:00:00Z" }
458+
wheel = { upload_time = "2024-03-26T00:00:00Z" }
459+
wheel_tags = ["py3-none-any", "cp312-abi3-win_amd64"]
460+
461+
[packages.b.versions."1.0.0"]
462+
sdist = false
463+
wheel = {}
464+
465+
[packages.c.versions."1.0.0"]
466+
wheel = false
467+
"#,
468+
)?;
469+
let server = PackseServer::from_scenario(&scenario);
470+
471+
let a: serde_json::Value = reqwest::get(format!("{}a/", server.index_url()))
472+
.await?
473+
.json()
474+
.await?;
475+
assert_eq!(a["files"].as_array().map(Vec::len), Some(3));
476+
assert_eq!(a["files"][0]["filename"], "a-1.0.0-py3-none-any.whl");
477+
assert_eq!(a["files"][0]["upload-time"], "2024-03-26T00:00:00Z");
478+
assert_eq!(
479+
a["files"][1]["filename"],
480+
"a-1.0.0-cp312-abi3-win_amd64.whl"
481+
);
482+
assert_eq!(a["files"][1]["upload-time"], "2024-03-26T00:00:00Z");
483+
assert_eq!(a["files"][2]["filename"], "a-1.0.0.tar.gz");
484+
assert_eq!(a["files"][2]["upload-time"], "2024-03-23T00:00:00Z");
485+
486+
let b: serde_json::Value = reqwest::get(format!("{}b/", server.index_url()))
487+
.await?
488+
.json()
489+
.await?;
490+
assert_eq!(b["files"].as_array().map(Vec::len), Some(1));
491+
assert_eq!(b["files"][0]["filename"], "b-1.0.0-py3-none-any.whl");
492+
assert_eq!(b["files"][0]["upload-time"], "2024-03-24T00:00:00Z");
493+
494+
let c: serde_json::Value = reqwest::get(format!("{}c/", server.index_url()))
495+
.await?
496+
.json()
497+
.await?;
498+
assert_eq!(c["files"].as_array().map(Vec::len), Some(1));
499+
assert_eq!(c["files"][0]["filename"], "c-1.0.0.tar.gz");
500+
assert_eq!(c["files"][0]["upload-time"], "2024-03-24T00:00:00Z");
501+
502+
Ok(())
503+
}
504+
433505
#[tokio::test]
434506
async fn file_requests_support_byte_ranges() -> Result<()> {
435507
let scenario = toml::from_str::<Scenario>(

0 commit comments

Comments
 (0)