From 6e2633470a0fffab00ec0e3f9f866158ffe3110a Mon Sep 17 00:00:00 2001 From: "remy.baranx@gmail.com" Date: Mon, 15 Jul 2024 11:18:59 +0200 Subject: [PATCH] fix cairo file metadata for contracts and models --- crates/dojo-lang/src/compiler.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/dojo-lang/src/compiler.rs b/crates/dojo-lang/src/compiler.rs index 11e79380cb..688caa8583 100644 --- a/crates/dojo-lang/src/compiler.rs +++ b/crates/dojo-lang/src/compiler.rs @@ -20,7 +20,7 @@ use cairo_lang_starknet_classes::contract_class::ContractClass; use cairo_lang_utils::UpcastMut; use camino::Utf8PathBuf; use convert_case::{Case, Casing}; -use dojo_world::contracts::naming; +use dojo_world::contracts::naming::{self}; use dojo_world::manifest::{ AbiFormat, Class, ComputedValueEntrypoint, DojoContract, DojoModel, Manifest, ManifestMethods, ABIS_DIR, BASE_CONTRACT_TAG, BASE_DIR, BASE_QUALIFIED_PATH, CONTRACTS_DIR, MANIFESTS_DIR, @@ -553,7 +553,25 @@ fn save_expanded_source_file( contract_basename: &str, contract_tag: &str, ) -> anyhow::Result<()> { - if let Ok(file_id) = db.module_main_file(module_id) { + if let Ok(files) = db.module_files(module_id) { + let contract_name = naming::get_name_from_tag(contract_tag); + + // search among all the module files (real and virtual), the one named with + // the contract/model name. This is the file containing the Cairo code generated + // from Dojo plugins. + let res = files.iter().filter(|f| f.file_name(db).eq(&contract_name)).collect::>(); + + let file_id = if res.is_empty() { + // if there is no virtual file with the name of the contract/model, just use the main + // module file + match db.module_main_file(module_id) { + Ok(f) => f, + Err(_) => return Err(anyhow!("failed to get source file: {contract_tag}")), + } + } else { + *res[0] + }; + if let Some(file_content) = db.file_content(file_id) { let src_file_name = format!("{contract_basename}.cairo");