From 2d5d10eb24dfdb4cdc609b162ac63b8b5f0ac87e Mon Sep 17 00:00:00 2001 From: Josh Stevens Date: Tue, 17 Sep 2024 13:43:05 +0100 Subject: [PATCH] fix: throw error if contract names are not unique --- core/src/manifest/yaml.rs | 17 +++++++++++++++++ documentation/docs/pages/docs/changelog.mdx | 1 + 2 files changed, 18 insertions(+) diff --git a/core/src/manifest/yaml.rs b/core/src/manifest/yaml.rs index 0037e91..061bad3 100644 --- a/core/src/manifest/yaml.rs +++ b/core/src/manifest/yaml.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashSet, env, fs::File, io::{Read, Write}, @@ -35,6 +36,9 @@ fn substitute_env_variables(contents: &str) -> Result { #[derive(thiserror::Error, Debug)] pub enum ValidateManifestError { + #[error("Contract names {0} must be unique")] + ContractNameMustBeUnique(String), + #[error("Invalid network mapped to contract: network - {0} contract - {1}")] InvalidNetworkMappedToContract(String, String), @@ -70,6 +74,19 @@ fn validate_manifest( project_path: &Path, manifest: &Manifest, ) -> Result<(), ValidateManifestError> { + let mut seen = HashSet::new(); + let duplicates_contract_names: Vec = manifest + .contracts + .iter() + .filter_map(|c| if seen.insert(&c.name) { None } else { Some(c.name.clone()) }) + .collect(); + + if !duplicates_contract_names.is_empty() { + return Err(ValidateManifestError::ContractNameMustBeUnique( + duplicates_contract_names.join(", "), + )); + } + for contract in &manifest.contracts { let events = ABIItem::read_abi_items(project_path, contract) .map_err(|e| ValidateManifestError::InvalidABI(contract.name.clone(), e.to_string()))?; diff --git a/documentation/docs/pages/docs/changelog.mdx b/documentation/docs/pages/docs/changelog.mdx index 91e7851..b371fae 100644 --- a/documentation/docs/pages/docs/changelog.mdx +++ b/documentation/docs/pages/docs/changelog.mdx @@ -8,6 +8,7 @@ ### Bug fixes ------------------------------------------------- +fix: throw error if contract names are not unique ### Breaking changes -------------------------------------------------