Skip to content

Commit

Permalink
fix(manifest): Improve rust-version error messages
Browse files Browse the repository at this point in the history
Since we have tests for a couple of cases, I figured we could
improve the error messages for them.
  • Loading branch information
epage committed Aug 24, 2023
1 parent 04257e7 commit 5810f8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ impl std::str::FromStr for PartialVersion {
// HACK: `PartialVersion` is a subset of the `VersionReq` syntax that only ever
// has one comparator with a required minor and optional patch, and uses no
// other features.
if is_req(value) {
anyhow::bail!("unexpected version requirement, expected a version like \"1.32\"")
}
let version_req = match semver::VersionReq::parse(value) {
// Exclude semver operators like `^` and pre-release identifiers
Ok(req) if value.chars().all(|c| c.is_ascii_digit() || c == '.') => req,
Err(_) if value.contains('+') => {
anyhow::bail!("unexpected build field, expected a version like \"1.32\"")
}
Err(_) if value.contains('-') => {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
_ => anyhow::bail!("expected a version like \"1.32\""),
};
assert_eq!(
Expand Down Expand Up @@ -211,6 +220,13 @@ impl<'de> serde::Deserialize<'de> for PartialVersion {
}
}

fn is_req(value: &str) -> bool {
let Some(first) = value.chars().next() else {
return false;
};
"<>=^~".contains(first) || value.contains('*') || value.contains(',')
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/rust_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn rust_version_bad_caret() {
error: failed to parse manifest at `[..]`
Caused by:
expected a version like \"1.32\"
unexpected version requirement, expected a version like \"1.32\"
in `package.rust-version`",
)
.run();
Expand Down Expand Up @@ -78,7 +78,7 @@ fn rust_version_bad_pre_release() {
error: failed to parse manifest at `[..]`
Caused by:
expected a version like \"1.32\"
unexpected prerelease field, expected a version like \"1.32\"
in `package.rust-version`",
)
.run();
Expand Down

0 comments on commit 5810f8d

Please sign in to comment.