Skip to content

Commit

Permalink
fix: improve error when jsr specifier has a leading v in version requ…
Browse files Browse the repository at this point in the history
…irement (#553)
  • Loading branch information
dsherret authored Dec 9, 2024
1 parent 4dff5d9 commit 7c4a101
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 40 additions & 3 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use deno_semver::package::PackageReq;
use deno_semver::package::PackageReqReferenceParseError;
use deno_semver::RangeSetOrTag;
use deno_semver::Version;
use deno_semver::VersionReq;
use futures::future::LocalBoxFuture;
use futures::stream::FuturesOrdered;
use futures::stream::FuturesUnordered;
Expand Down Expand Up @@ -205,8 +206,14 @@ pub enum JsrLoadError {
pub enum JsrPackageFormatError {
#[error(transparent)]
JsrPackageParseError(PackageReqReferenceParseError),
#[error("Version tag not supported in jsr specifiers.")]
VersionTagNotSupported,
#[error("Version tag not supported in jsr specifiers ('{}').{}",
.tag,
match .tag.strip_prefix('v').and_then(|v| VersionReq::parse_from_specifier(v).ok().map(|s| s.tag().is_none())).unwrap_or(false) {
true => " Remove leading 'v' before version.",
false => ""
}
)]
VersionTagNotSupported { tag: String },
}

#[derive(Debug, Clone, Error)]
Expand Down Expand Up @@ -5055,7 +5062,9 @@ fn validate_jsr_specifier(
let package_ref = JsrPackageReqReference::from_specifier(specifier)
.map_err(JsrPackageFormatError::JsrPackageParseError)?;
match package_ref.req().version_req.inner() {
RangeSetOrTag::Tag(_) => Err(JsrPackageFormatError::VersionTagNotSupported),
RangeSetOrTag::Tag(tag) => {
Err(JsrPackageFormatError::VersionTagNotSupported { tag: tag.clone() })
}
RangeSetOrTag::RangeSet(_) => Ok(package_ref),
}
}
Expand Down Expand Up @@ -6639,4 +6648,32 @@ mod tests {
.unwrap();
assert!(module.external().is_some());
}

#[test]
fn leading_v_version_tag_err() {
{
let err = JsrPackageFormatError::VersionTagNotSupported {
tag: "v1.2".to_string(),
};
assert_eq!(err.to_string(), "Version tag not supported in jsr specifiers ('v1.2'). Remove leading 'v' before version.");
}
{
let err = JsrPackageFormatError::VersionTagNotSupported {
tag: "latest".to_string(),
};
assert_eq!(
err.to_string(),
"Version tag not supported in jsr specifiers ('latest')."
);
}
{
let err = JsrPackageFormatError::VersionTagNotSupported {
tag: "version".to_string(), // not a vversion with a leading 'v'
};
assert_eq!(
err.to_string(),
"Version tag not supported in jsr specifiers ('version')."
);
}
}
}
2 changes: 1 addition & 1 deletion tests/specs/graph/jsr/version_tag_not_supported.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import 'jsr:@scope/a@tag';
},
{
"specifier": "jsr:@scope/a@tag",
"error": "Version tag not supported in jsr specifiers."
"error": "Version tag not supported in jsr specifiers ('tag')."
}
],
"redirects": {}
Expand Down

0 comments on commit 7c4a101

Please sign in to comment.