Skip to content

Commit 4dd8680

Browse files
authored
Deduplicate external contracts by full path (#2001)
fixes #2003 - **Add failing test case** - **Deduplicate external contracts by full path**
1 parent 7f268dc commit 4dd8680

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

scarb/src/compiler/compilers/starknet_contract/compiler.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,18 @@ pub fn find_project_contracts(
332332
Vec::new()
333333
};
334334

335-
Ok(internal_contracts
335+
// Deduplicate found contracts by contract path.
336+
let mut contracts_found = internal_contracts
336337
.into_iter()
337338
.chain(external_contracts)
339+
.map(|decl| (decl.module_id().full_path(db.upcast()), decl))
340+
.sorted_by_key(|(path, _)| path.clone())
341+
.collect_vec();
342+
contracts_found.dedup_by_key(|(path, _)| path.clone());
343+
344+
Ok(contracts_found
345+
.into_iter()
346+
.map(|(_path, decl)| decl)
338347
.collect())
339348
}
340349

scarb/tests/build_starknet_external_contracts.rs

+51
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,54 @@ fn can_build_external_reexported_contracts() {
558558
]
559559
);
560560
}
561+
#[test]
562+
fn can_dedup_contract_reexports() {
563+
let t = TempDir::new().unwrap();
564+
let hello = t.child("hello");
565+
let world = t.child("world");
566+
567+
ProjectBuilder::start()
568+
.name("hello")
569+
.version("0.1.0")
570+
.manifest_extra(indoc! {r#"
571+
[lib]
572+
[[target.starknet-contract]]
573+
"#})
574+
.dep_starknet()
575+
.lib_cairo(indoc! {r#"
576+
// Note that Balance contract can be accessed both through mod tree and the reexport.
577+
pub mod a;
578+
pub use a::Balance;
579+
"#})
580+
.src("src/a.cairo", BALANCE_CONTRACT)
581+
.build(&hello);
582+
583+
ProjectBuilder::start()
584+
.name("world")
585+
.version("0.1.0")
586+
.dep("hello", hello)
587+
.manifest_extra(formatdoc! {r#"
588+
[[target.starknet-contract]]
589+
build-external-contracts = ["hello::*"]
590+
"#})
591+
.dep_starknet()
592+
.build(&world);
593+
594+
Scarb::quick_snapbox()
595+
.arg("build")
596+
.current_dir(&world)
597+
.assert()
598+
.success()
599+
.stdout_matches(indoc! {r#"
600+
[..] Compiling world v0.1.0 ([..]/Scarb.toml)
601+
[..] Finished `dev` profile target(s) in [..]
602+
"#});
603+
604+
assert_eq!(
605+
world.child("target/dev").files(),
606+
vec![
607+
"world.starknet_artifacts.json",
608+
"world_Balance.contract_class.json",
609+
]
610+
);
611+
}

0 commit comments

Comments
 (0)