Skip to content

Commit be82ff8

Browse files
authored
Match prerelease versions with annotation wildcard (#1716)
Fixes #1690
1 parent 36c7f28 commit be82ff8

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

crate_universe/private/crate.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def _annotation(
9090
"""A collection of extra attributes and settings for a particular crate
9191
9292
Args:
93-
version (str, optional): The version or semver-conditions to match with a crate.
93+
version (str, optional): The version or semver-conditions to match with a crate. The wildcard `*`
94+
matches any version, including prerelease versions.
9495
additive_build_file_content (str, optional): Extra contents to write to the bottom of generated BUILD files.
9596
additive_build_file (str, optional): A file containing extra contents to write to the bottom of
9697
generated BUILD files.

crate_universe/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ impl CrateId {
356356
return true;
357357
}
358358

359+
// If the version provided is the wildcard "*", it matches. Do not
360+
// delegate to the semver crate in this case because semver does not
361+
// consider "*" to match prerelease packages. That's expected behavior
362+
// in the context of declaring package dependencies, but not in the
363+
// context of declaring which versions of preselected packages an
364+
// annotation applies to.
365+
if self.version == "*" {
366+
return true;
367+
}
368+
359369
// Next, check to see if the version provided is a semver req and
360370
// check if the package matches the condition
361371
if let Ok(semver) = VersionReq::parse(&self.version) {
@@ -497,6 +507,10 @@ mod test {
497507
id.version = "*".to_owned();
498508
assert!(id.matches(&package));
499509

510+
let mut prerelease = mock_cargo_metadata_package();
511+
prerelease.version = cargo_metadata::semver::Version::parse("1.0.0-pre.0").unwrap();
512+
assert!(id.matches(&prerelease));
513+
500514
id.version = "<1".to_owned();
501515
assert!(!id.matches(&package));
502516
}

docs/crate_universe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ A collection of extra attributes and settings for a particular crate
518518

519519
| Name | Description | Default Value |
520520
| :------------- | :------------- | :------------- |
521-
| <a id="crate.annotation-version"></a>version | The version or semver-conditions to match with a crate. | `"*"` |
521+
| <a id="crate.annotation-version"></a>version | The version or semver-conditions to match with a crate. The wildcard <code>*</code> matches any version, including prerelease versions. | `"*"` |
522522
| <a id="crate.annotation-additive_build_file"></a>additive_build_file | A file containing extra contents to write to the bottom of generated BUILD files. | `None` |
523523
| <a id="crate.annotation-additive_build_file_content"></a>additive_build_file_content | Extra contents to write to the bottom of generated BUILD files. | `None` |
524524
| <a id="crate.annotation-build_script_data"></a>build_script_data | A list of labels to add to a crate's <code>cargo_build_script::data</code> attribute. | `None` |

0 commit comments

Comments
 (0)