@@ -10,6 +10,7 @@ use dojo_world::local::WorldLocal;
1010use scarb_interop:: fsx;
1111use scarb_metadata:: { DepKind , Metadata , MetadataCommand , MetadataCommandError , PackageMetadata } ;
1212use serde:: Serialize ;
13+ use tracing:: debug;
1314
1415#[ derive( Debug , PartialEq ) ]
1516pub enum TestRunner {
@@ -20,7 +21,7 @@ pub enum TestRunner {
2021
2122const CAIRO_TEST_RUNNER_NAME : & str = "cairo_test" ;
2223const SNF_TEST_RUNNER_NAME : & str = "snforge_std" ;
23- const DOJO_MACROS_NAME : & str = "dojo_macros " ;
24+ const DOJO_WORLD_NAME : & str = "dojo::world::world_contract::world " ;
2425
2526impl From < & String > for TestRunner {
2627 fn from ( value : & String ) -> Self {
@@ -297,30 +298,61 @@ enum WorldPackageResult {
297298/// performing some tasks like binding generation and migrations.
298299///
299300/// To solve the issue where a virtual workspace has no package name, this function looks for a
300- /// package that is *not* a `lib` target and uses `dojo_macros` as a dependency.
301- /// If it finds exactly one package that matches the criteria, it returns the package name.
301+ /// package that has a starknet-contract target that has a build-external-contracts parameter that
302+ /// contains the dojo world. We can't rely on the package being a library, since some libraries
303+ /// might be used in the contracts to be deployed with a world. For this reason, we first look for a
304+ /// package that is not a library and then a package that is a library. If it finds exactly one
305+ /// package that matches the criteria, it returns the package name.
302306///
303307/// Otherwise, it returns a `MultiplePackages` variant, which will be handled by the caller, which
304308/// generally should indicate to the user that using multiple packages with contracts to be deployed
305309/// with a world is not supported at the workspace level. Therefore, the user will want to build and
306310/// migrate the package with contracts themselves by going into the package directory and running
307311/// the commands.
308312fn default_dojo_package_name ( packages : & [ PackageMetadata ] ) -> WorldPackageResult {
309- let mut candidates = Vec :: new ( ) ;
313+ let mut not_lib_candidates = Vec :: new ( ) ;
314+ let mut lib_candidates = Vec :: new ( ) ;
310315
311316 for p in packages {
312- if p. targets . iter ( ) . all ( |t| t. kind != "lib" ) {
313- for d in & p. dependencies {
314- if d. name == DOJO_MACROS_NAME {
315- candidates. push ( p. name . clone ( ) ) ;
317+ let is_lib = p. targets . iter ( ) . any ( |t| t. kind == "lib" ) ;
318+
319+ debug ! ( %p. name, is_lib, "Verifying package type." ) ;
320+
321+ for t in & p. targets {
322+ if t. kind == "starknet-contract" {
323+ if let Some ( build_external_contracts) =
324+ t. params . as_object ( ) . and_then ( |obj| obj. get ( "build-external-contracts" ) )
325+ {
326+ if let Some ( build_external_contracts) = build_external_contracts. as_array ( ) {
327+ if build_external_contracts
328+ . contains ( & serde_json:: Value :: String ( DOJO_WORLD_NAME . to_string ( ) ) )
329+ {
330+ if is_lib {
331+ lib_candidates. push ( p. name . clone ( ) ) ;
332+ } else {
333+ not_lib_candidates. push ( p. name . clone ( ) ) ;
334+ }
335+ }
336+ }
316337 }
317338 }
318339 }
319340 }
320341
321- match candidates. len ( ) {
322- 0 => WorldPackageResult :: NoPackage ,
323- 1 => WorldPackageResult :: Ok ( candidates[ 0 ] . clone ( ) ) ,
324- _ => WorldPackageResult :: MultiplePackages ( candidates) ,
325- }
342+ debug ! ( ?not_lib_candidates, ?lib_candidates, "Collect candidates for Sozo build." ) ;
343+
344+ // Prioritize not lib candidates, then lib candidates.
345+ let r = match not_lib_candidates. len ( ) {
346+ 0 => match lib_candidates. len ( ) {
347+ 0 => WorldPackageResult :: NoPackage ,
348+ 1 => WorldPackageResult :: Ok ( lib_candidates[ 0 ] . clone ( ) ) ,
349+ _ => WorldPackageResult :: MultiplePackages ( lib_candidates) ,
350+ } ,
351+ 1 => WorldPackageResult :: Ok ( not_lib_candidates[ 0 ] . clone ( ) ) ,
352+ _ => WorldPackageResult :: MultiplePackages ( not_lib_candidates) ,
353+ } ;
354+
355+ debug ! ( ?r, "Extract default dojo package name from workspace." ) ;
356+
357+ r
326358}
0 commit comments