Skip to content

Commit 1fdf630

Browse files
feat: disallow dependency package name collision in forc-pkg (#6888)
## Description closes #6861 `forc` was allowing dependency name being the same (through the use of `package` alias and the declaration itself) as the project name. The following two cases are now invalid and produces an error on the forc-pkg side before going to the compiler. ```TOML [project] authors = ["Fuel Labs <contact@fuel.sh>"] entry = "main.sw" license = "Apache-2.0" name = "lib_contract" [dependencies] lib_contract = { path = "../lib_contract_abi/", package = "lib_contract_abi" } ``` and ```TOML [project] authors = ["Fuel Labs <contact@fuel.sh>"] entry = "main.sw" license = "Apache-2.0" name = "lib_contract_abi" [dependencies] lib_contract = { path = "../lib_contract_abi/", package = "lib_contract_abi" } ``` --------- Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
1 parent c288563 commit 1fdf630

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

forc-pkg/src/manifest/mod.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl DependencyDetails {
326326
if git.is_none() && (branch.is_some() || tag.is_some() || rev.is_some()) {
327327
bail!("Details reserved for git sources used without a git field");
328328
}
329+
329330
Ok(())
330331
}
331332
}
@@ -638,13 +639,26 @@ impl PackageManifest {
638639
/// 1. The project and organization names against a set of reserved/restricted keywords and patterns.
639640
/// 2. The validity of the details provided. Makes sure that there are no mismatching detail
640641
/// declarations (to prevent mixing details specific to certain types).
642+
/// 3. The dependencies listed does not have an alias ("package" field) that is the same as package name.
641643
pub fn validate(&self) -> Result<()> {
642644
validate_project_name(&self.project.name)?;
643645
if let Some(ref org) = self.project.organization {
644646
validate_name(org, "organization name")?;
645647
}
646-
for (_, dependency_details) in self.deps_detailed() {
648+
for (dep_name, dependency_details) in self.deps_detailed() {
647649
dependency_details.validate()?;
650+
if dependency_details
651+
.package
652+
.as_ref()
653+
.is_some_and(|package_alias| package_alias == &self.project.name)
654+
{
655+
bail!(format!("Dependency \"{dep_name}\" declares an alias (\"package\" field) that is the same as project name"))
656+
}
657+
if dep_name == &self.project.name {
658+
bail!(format!(
659+
"Dependency \"{dep_name}\" collides with project name."
660+
))
661+
}
648662
}
649663
Ok(())
650664
}
@@ -1666,4 +1680,43 @@ mod tests {
16661680
assert_eq!(original.workspace.members, deserialized.workspace.members);
16671681
assert_eq!(original.workspace.metadata, deserialized.workspace.metadata);
16681682
}
1683+
1684+
#[test]
1685+
fn test_dependency_alias_project_name_collision() {
1686+
let original_toml = r#"
1687+
[project]
1688+
authors = ["Fuel Labs <contact@fuel.sh>"]
1689+
entry = "main.sw"
1690+
license = "Apache-2.0"
1691+
name = "lib_contract_abi"
1692+
1693+
[dependencies]
1694+
lib_contract = { path = "../lib_contract_abi/", package = "lib_contract_abi" }
1695+
"#;
1696+
1697+
let project = PackageManifest::from_string(original_toml.to_string());
1698+
let err = project.unwrap_err();
1699+
assert_eq!(err.to_string(), format!("Dependency \"lib_contract\" declares an alias (\"package\" field) that is the same as project name"))
1700+
}
1701+
1702+
#[test]
1703+
fn test_dependency_name_project_name_collision() {
1704+
let original_toml = r#"
1705+
[project]
1706+
authors = ["Fuel Labs <contact@fuel.sh>"]
1707+
entry = "main.sw"
1708+
license = "Apache-2.0"
1709+
name = "lib_contract"
1710+
1711+
[dependencies]
1712+
lib_contract = { path = "../lib_contract_abi/", package = "lib_contract_abi" }
1713+
"#;
1714+
1715+
let project = PackageManifest::from_string(original_toml.to_string());
1716+
let err = project.unwrap_err();
1717+
assert_eq!(
1718+
err.to_string(),
1719+
format!("Dependency \"lib_contract\" collides with project name.")
1720+
)
1721+
}
16691722
}

0 commit comments

Comments
 (0)