Skip to content

Commit

Permalink
fix: consider prereleases to be compatible with pre-1.0.0 releases (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Nov 21, 2024
1 parent e4c66b9 commit 013e200
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tooling/nargo_toml/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum ManifestError {
#[allow(clippy::enum_variant_names)]
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum SemverError {
#[error("Invalid value for `compiler_version` in package {package_name}. Requirements may only refer to full releases")]
InvalidCompilerVersionRequirement { package_name: CrateName, required_compiler_version: String },
#[error("Incompatible compiler version in package {package_name}. Required compiler version is {required_compiler_version} but the compiler version is {compiler_version_found}.\n Update the compiler_version field in Nargo.toml to >={required_compiler_version} or compile this project with version {required_compiler_version}")]
IncompatibleVersion {
package_name: CrateName,
Expand Down
49 changes: 43 additions & 6 deletions tooling/nargo_toml/src/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use nargo::{
package::{Dependency, Package},
workspace::Workspace,
};
use semver::{Error, Version, VersionReq};
use noirc_driver::CrateName;
use semver::{Error, Prerelease, Version, VersionReq};

// Parse a semver compatible version string
pub(crate) fn parse_semver_compatible_version(version: &str) -> Result<Version, Error> {
Version::parse(version)
let mut version = Version::parse(version)?;
version.pre = Prerelease::EMPTY;
Ok(version)
}

// Check that all of the packages in the workspace are compatible with the current compiler version
Expand All @@ -25,10 +28,7 @@ pub(crate) fn semver_check_workspace(
}

// Check that a package and all of its dependencies are compatible with the current compiler version
pub(crate) fn semver_check_package(
package: &Package,
compiler_version: &Version,
) -> Result<(), SemverError> {
fn semver_check_package(package: &Package, compiler_version: &Version) -> Result<(), SemverError> {
// Check that this package's compiler version requirements are satisfied
if let Some(version) = &package.compiler_required_version {
let version_req = match VersionReq::parse(version) {
Expand All @@ -40,6 +40,9 @@ pub(crate) fn semver_check_package(
})
}
};

validate_compiler_version_requirement(&package.name, &version_req)?;

if !version_req.matches(compiler_version) {
return Err(SemverError::IncompatibleVersion {
package_name: package.name.clone(),
Expand All @@ -61,6 +64,20 @@ pub(crate) fn semver_check_package(
Ok(())
}

fn validate_compiler_version_requirement(
package_name: &CrateName,
required_compiler_version: &VersionReq,
) -> Result<(), SemverError> {
if required_compiler_version.comparators.iter().any(|comparator| !comparator.pre.is_empty()) {
return Err(SemverError::InvalidCompilerVersionRequirement {
package_name: package_name.clone(),
required_compiler_version: required_compiler_version.to_string(),
});
}

Ok(())
}

// Strip the build meta data from the version string since it is ignored by semver.
fn strip_build_meta_data(version: &Version) -> String {
let version_string = version.to_string();
Expand Down Expand Up @@ -191,6 +208,26 @@ mod tests {
};
}

#[test]
fn test_semver_prerelease() {
let compiler_version = parse_semver_compatible_version("1.0.0-beta.0").unwrap();

let package = Package {
compiler_required_version: Some(">=0.1.0".to_string()),
root_dir: PathBuf::new(),
package_type: PackageType::Library,
entry_path: PathBuf::new(),
name: CrateName::from_str("test").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
expression_width: None,
};

if let Err(err) = semver_check_package(&package, &compiler_version) {
panic!("{err}");
};
}

#[test]
fn test_semver_build_data() {
let compiler_version = Version::parse("0.1.0+this-is-ignored-by-semver").unwrap();
Expand Down

0 comments on commit 013e200

Please sign in to comment.