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

Warn about unmatched external contracts selectors #1286

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions scarb/src/compiler/compilers/starknet_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler}
use crate::core::{PackageName, TargetKind, Utf8PathWorkspaceExt, Workspace};
use crate::internal::serdex::RelativeUtf8PathBuf;
use scarb_stable_hash::short_hash;
use scarb_ui::Ui;

const CAIRO_PATH_SEPARATOR: &str = "::";
const GLOB_PATH_SELECTOR: &str = "*";
Expand Down Expand Up @@ -219,7 +220,7 @@ impl Compiler for StarknetContractCompiler {
if let Some(external_contracts) = props.build_external_contracts.clone() {
for path in external_contracts.iter() {
ensure!(path.0.matches(GLOB_PATH_SELECTOR).count() <= 1,
"external contract path {} has multiple global path selectors, only one '*' selector is allowed",
"external contract path `{}` has multiple global path selectors, only one '*' selector is allowed",
path.0);
}
}
Expand All @@ -232,6 +233,7 @@ impl Compiler for StarknetContractCompiler {

let contracts = find_project_contracts(
db.upcast_mut(),
ws.config().ui(),
main_crate_ids,
props.build_external_contracts.clone(),
)?;
Expand Down Expand Up @@ -335,6 +337,7 @@ fn ensure_gas_enabled(db: &mut RootDatabase) -> Result<()> {

fn find_project_contracts(
mut db: &dyn SemanticGroup,
ui: Ui,
main_crate_ids: Vec<CrateId>,
external_contracts: Option<Vec<ContractSelector>>,
) -> Result<Vec<ContractDeclaration>> {
Expand Down Expand Up @@ -362,16 +365,32 @@ fn find_project_contracts(
.into_iter()
.filter(|decl| {
let contract_path = decl.module_id().full_path(db.upcast());
external_contracts.iter().any(|selector| {
if selector.is_wildcard() {
contract_path.starts_with(&selector.partial_path())
} else {
contract_path == selector.full_path()
}
})
external_contracts
.iter()
.any(|selector| contract_matches(selector, contract_path.as_str()))
})
.collect();

let never_matched = external_contracts
.iter()
.filter(|selector| {
!filtered_contracts.iter().any(|decl| {
let contract_path = decl.module_id().full_path(db.upcast());
contract_matches(selector, contract_path.as_str())
})
})
.collect_vec();
if !never_matched.is_empty() {
let never_matched = never_matched
.iter()
.map(|selector| selector.full_path())
.collect_vec()
.join("`, `");
ui.warn(format!(
"external contracts not found for selectors: `{never_matched}`"
));
}

filtered_contracts
} else {
debug!("no external contracts selected");
Expand All @@ -384,6 +403,14 @@ fn find_project_contracts(
.collect())
}

fn contract_matches(selector: &ContractSelector, contract_path: &str) -> bool {
if selector.is_wildcard() {
contract_path.starts_with(&selector.partial_path())
} else {
contract_path == selector.full_path()
}
}

fn check_allowed_libfuncs(
props: &Props,
contracts: &[&ContractDeclaration],
Expand Down
69 changes: 68 additions & 1 deletion scarb/tests/build_starknet_external_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,74 @@ fn compile_with_bad_glob_path() {
.failure()
.stdout_matches(indoc! {r#"
[..] Compiling world v0.1.0 ([..]/Scarb.toml)
error: external contract path hello::** has multiple global path selectors, only one '*' selector is allowed
error: external contract path `hello::**` has multiple global path selectors, only one '*' selector is allowed
error: could not compile `world` due to previous error
"#});
}

#[test]
fn will_warn_about_unmatched_paths() {
let t = TempDir::new().unwrap();
let hello = t.child("hello");
let world = t.child("world");

ProjectBuilder::start()
.name("hello")
.version("0.1.0")
.manifest_extra(indoc! {r#"
[lib]
[[target.starknet-contract]]
"#})
.dep_starknet()
.lib_cairo(indoc! {r#"
mod lorem;
"#})
.src(
"src/lorem.cairo",
indoc! {r#"
mod ipsum;
"#},
)
.src(
"src/lorem/ipsum.cairo",
format!("{}\n{}", BALANCE_CONTRACT, HELLO_CONTRACT),
)
.build(&hello);

ProjectBuilder::start()
.name("world")
.version("0.1.0")
.dep("hello", &hello)
.manifest_extra(indoc! {r#"
[[target.starknet-contract]]
build-external-contracts = [
"hello::lorem::ipsum::Balance",
"hello::lorem::ipsum::HelloContract",
"hello::lorem::mopsum::*",
]
"#})
.dep_starknet()
.lib_cairo(format!("{}\n{}", FORTY_TWO_CONTRACT, HELLO_CONTRACT))
.build(&world);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&world)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Compiling world v0.1.0 ([..]/Scarb.toml)
warn: external contracts not found for selectors: `hello::lorem::mopsum::*`
[..] Finished release target(s) in [..]
"#});
assert_eq!(
world.child("target/dev").files(),
vec![
"world.starknet_artifacts.json",
"world_Balance.contract_class.json",
"world_FortyTwo.contract_class.json",
"world_hello_lorem_ipsum_HelloContract.contract_class.json",
"world_world_HelloContract.contract_class.json",
]
);
}
Loading