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

feat: Add package version to Nargo.toml metadata #3427

Merged
merged 10 commits into from
Nov 28, 2023
1 change: 1 addition & 0 deletions test_programs/execution_success/1_mul/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "1_mul"
version = "0.1.0"
type = "bin"
authors = [""]

Expand Down
1 change: 1 addition & 0 deletions tooling/nargo/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Dependency {

#[derive(Clone)]
pub struct Package {
pub version: Option<String>,
// A semver string which specifies the compiler version required to compile this package
pub compiler_required_version: Option<String>,
pub root_dir: PathBuf,
Expand Down
3 changes: 3 additions & 0 deletions tooling/nargo_toml/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum ManifestError {
SemverError(SemverError),
}

#[allow(clippy::enum_variant_names)]
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum SemverError {
#[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}")]
Expand All @@ -81,4 +82,6 @@ pub enum SemverError {
},
#[error("Could not parse the required compiler version for package {package_name} in Nargo.toml. Error: {error}")]
CouldNotParseRequiredVersion { package_name: String, error: String },
#[error("Could not parse the package version for package {package_name} in Nargo.toml. Error: {error}")]
CouldNotParsePackageVersion { package_name: String, error: String },
}
15 changes: 14 additions & 1 deletion tooling/nargo_toml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
path::{Component, Path, PathBuf},
};

use errors::SemverError;
use fm::{NormalizePath, FILE_EXTENSION};
use nargo::{
package::{Dependency, Package, PackageType},
Expand Down Expand Up @@ -99,7 +100,7 @@ struct PackageConfig {

impl PackageConfig {
fn resolve_to_package(&self, root_dir: &Path) -> Result<Package, ManifestError> {
let name = if let Some(name) = &self.package.name {
let name: CrateName = if let Some(name) = &self.package.name {
name.parse().map_err(|_| ManifestError::InvalidPackageName {
toml: root_dir.join("Nargo.toml"),
name: name.into(),
Expand Down Expand Up @@ -163,7 +164,18 @@ impl PackageConfig {
}
};

// If there is a package version, ensure that it is semver compatible
if let Some(version) = &self.package.version {
semver::parse_semver_compatible_version(version).map_err(|err| {
ManifestError::SemverError(SemverError::CouldNotParsePackageVersion {
package_name: name.to_string(),
error: err.to_string(),
})
})?;
}

Ok(Package {
version: self.package.version.clone(),
compiler_required_version: self.package.compiler_version.clone(),
root_dir: root_dir.to_path_buf(),
entry_path,
Expand Down Expand Up @@ -225,6 +237,7 @@ struct WorkspaceConfig {
#[derive(Default, Debug, Deserialize, Clone)]
struct PackageMetadata {
name: Option<String>,
version: Option<String>,
#[serde(alias = "type")]
package_type: Option<String>,
entry: Option<PathBuf>,
Expand Down
15 changes: 13 additions & 2 deletions tooling/nargo_toml/src/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use nargo::{
package::{Dependency, Package},
workspace::Workspace,
};
use semver::{Version, VersionReq};
use semver::{Error, Version, VersionReq};

// Parse a semver compatible version string
pub(crate) fn parse_semver_compatible_version(version: &str) -> Result<Version, Error> {
Version::parse(version)
}

// Check that all of the packages in the workspace are compatible with the current compiler version
pub(crate) fn semver_check_workspace(
workspace: Workspace,
current_compiler_version: String,
) -> Result<(), ManifestError> {
let version = Version::parse(&current_compiler_version)
let version = parse_semver_compatible_version(&current_compiler_version)
.expect("The compiler version is not a valid semver version");
for package in &workspace.members {
semver_check_package(package, &version).map_err(ManifestError::SemverError)?;
Expand Down Expand Up @@ -83,6 +88,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("test").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};
if let Err(err) = semver_check_package(&package, &compiler_version) {
panic!("semver check should have passed. compiler version is 0.1.0 and required version from the package is 0.1.0\n error: {err:?}")
Expand Down Expand Up @@ -113,6 +119,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("test").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};

let valid_dependency = Package {
Expand All @@ -122,6 +129,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("good_dependency").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};
let invalid_dependency = Package {
compiler_required_version: Some("0.2.0".to_string()),
Expand All @@ -130,6 +138,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("bad_dependency").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};

package.dependencies.insert(
Expand Down Expand Up @@ -169,6 +178,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("test").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};

if let Err(err) = semver_check_package(&package, &compiler_version) {
Expand All @@ -187,6 +197,7 @@ mod tests {
entry_path: PathBuf::new(),
name: CrateName::from_str("test").unwrap(),
dependencies: BTreeMap::new(),
version: Some("1.0".to_string()),
};

if let Err(err) = semver_check_package(&package, &compiler_version) {
Expand Down
Loading