Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider prereleases to be compatible with pre-1.0.0 releases #6580

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading