Skip to content

Commit d3af7c9

Browse files
authored
fix(sozo-scarb_metadata_ext): rework the detection of Dojo default package (#3327)
1 parent d9725b1 commit d3af7c9

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dojo/dojo-snf-test/Scarb.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ version = 1
33

44
[[package]]
55
name = "dojo"
6-
version = "1.6.0-alpha.1"
6+
version = "1.7.0-alpha.2"
77
dependencies = [
88
"dojo_macros",
99
]
1010

1111
[[package]]
1212
name = "dojo_macros"
13-
version = "1.6.0-alpha.1"
13+
version = "1.7.0-alpha.2"
1414

1515
[[package]]
1616
name = "dojo_snf_test"
17-
version = "1.6.0-alpha.1"
17+
version = "1.7.0-alpha.2"
1818
dependencies = [
1919
"dojo",
2020
"snforge_std",
2121
]
2222

2323
[[package]]
2424
name = "snforge_scarb_plugin"
25-
version = "0.43.1"
25+
version = "0.48.1"
2626
source = "registry+https://scarbs.xyz/"
27-
checksum = "sha256:178e1e2081003ae5e40b5a8574654bed15acbd31cce651d4e74fe2f009bc0122"
27+
checksum = "sha256:2dd27e8215eea8785b3930e9f452e11b429ca262b1c1fbb071bfc173b9ebc125"
2828

2929
[[package]]
3030
name = "snforge_std"
31-
version = "0.43.1"
31+
version = "0.48.1"
3232
source = "registry+https://scarbs.xyz/"
33-
checksum = "sha256:17bc65b0abfb9b174784835df173f9c81c9ad39523dba760f30589ef53cf8bb5"
33+
checksum = "sha256:89f759fa685d48ed0ba7152d2ac2eb168da08dfa8b84b2bee96e203dc5b2413e"
3434
dependencies = [
3535
"snforge_scarb_plugin",
3636
]

crates/sozo/scarb_metadata_ext/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ dunce.workspace = true
1616
scarb-metadata.workspace = true
1717
smol_str.workspace = true
1818
scarb-interop.workspace = true
19+
tracing.workspace = true

crates/sozo/scarb_metadata_ext/src/metadata.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use dojo_world::local::WorldLocal;
1010
use scarb_interop::fsx;
1111
use scarb_metadata::{DepKind, Metadata, MetadataCommand, MetadataCommandError, PackageMetadata};
1212
use serde::Serialize;
13+
use tracing::debug;
1314

1415
#[derive(Debug, PartialEq)]
1516
pub enum TestRunner {
@@ -20,7 +21,7 @@ pub enum TestRunner {
2021

2122
const CAIRO_TEST_RUNNER_NAME: &str = "cairo_test";
2223
const 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

2526
impl 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.
308312
fn 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

Comments
 (0)