Skip to content

Commit

Permalink
feat: error out if more than one package declares the same package na…
Browse files Browse the repository at this point in the history
…me in a workspace (FuelLabs#3840)

closes FuelLabs#3650
  • Loading branch information
kayagokalp authored Jan 24, 2023
1 parent 3998c10 commit d1a3f07
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 1 deletion.
29 changes: 28 additions & 1 deletion forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use forc_tracing::println_yellow_err;
use forc_util::{find_manifest_dir, validate_name};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
collections::{BTreeMap, HashMap},
path::{Path, PathBuf},
sync::Arc,
};
Expand Down Expand Up @@ -824,6 +824,7 @@ impl WorkspaceManifest {
///
/// This checks if the listed members in the `WorkspaceManifest` are indeed in the given `Forc.toml`'s directory.
pub fn validate(&self, path: &Path) -> Result<()> {
let mut pkg_name_to_paths: HashMap<String, Vec<PathBuf>> = HashMap::new();
for member in self.workspace.members.iter() {
let member_path = path.join(member).join("Forc.toml");
if !member_path.exists() {
Expand All @@ -833,6 +834,32 @@ impl WorkspaceManifest {
member_path
);
}
let member_manifest_file = PackageManifestFile::from_file(member_path.clone())?;
let pkg_name = member_manifest_file.manifest.project.name;
pkg_name_to_paths
.entry(pkg_name)
.or_default()
.push(member_path);
}

// Check for duplicate pkg name entries in member manifests of this workspace.
let duplciate_pkg_lines = pkg_name_to_paths
.iter()
.filter(|(_, paths)| paths.len() > 1)
.map(|(pkg_name, _)| {
let duplicate_paths = pkg_name_to_paths
.get(pkg_name)
.expect("missing duplicate paths");
format!("{}: {:#?}", pkg_name, duplicate_paths)
})
.collect::<Vec<_>>();

if !duplciate_pkg_lines.is_empty() {
let error_message = duplciate_pkg_lines.join("\n");
bail!(
"Duplicate package names detected in the workspace:\n\n{}",
error_message
);
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[package]]
name = 'contract'
source = 'member'

[[package]]
name = 'contract2'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]

members = ["contract", "contract2", "script", "script2"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "contract"
implicit-std = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "contract"
implicit-std = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "script"
implicit-std = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script;

fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "script"
implicit-std = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script;

fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

# check: Duplicate package names detected in the workspace:

0 comments on commit d1a3f07

Please sign in to comment.