Skip to content

Commit

Permalink
feat(sozo): ensure sozo errors with dojo-core version mismatch (#2364)
Browse files Browse the repository at this point in the history
* feat: ensure sozo errors with dojo-core version mismatch

* fix: ensure only tag with git is checked for the match
  • Loading branch information
glihm authored Aug 29, 2024
1 parent d13fe8f commit 1d32ab1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use scarb_ui::args::{FeaturesSpec, PackagesFilter};
use sozo_ops::statistics::{get_contract_statistics_for_dir, ContractStatistics};
use tracing::trace;

use crate::commands::check_package_dojo_version;
use crate::commands::clean::CleanArgs;

const BYTECODE_SIZE_LABEL: &str = "Bytecode size [in felts]\n(Sierra, Casm)";
Expand Down Expand Up @@ -58,6 +59,10 @@ impl BuildArgs {
ws.members().collect()
};

for p in &packages {
check_package_dojo_version(&ws, p)?;
}

let profile_name =
ws.current_profile().expect("Scarb profile is expected at this point.").to_string();

Expand Down
46 changes: 45 additions & 1 deletion bin/sozo/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt;

use anyhow::Result;
use clap::Subcommand;
use scarb::core::Config;
use scarb::core::{Config, Package, Workspace};

pub(crate) mod account;
pub(crate) mod auth;
Expand Down Expand Up @@ -133,3 +133,47 @@ pub fn run(command: Commands, config: &Config) -> Result<()> {
Commands::Completions(args) => args.run(),
}
}

/// Checks if the package has a compatible version of dojo-core.
/// In case of a workspace with multiple packages, each package is individually checked
/// and the workspace manifest path is returned in case of virtual workspace.
pub fn check_package_dojo_version(ws: &Workspace<'_>, package: &Package) -> anyhow::Result<()> {
if let Some(dojo_dep) =
package.manifest.summary.dependencies.iter().find(|dep| dep.name.as_str() == "dojo")
{
let dojo_version = env!("CARGO_PKG_VERSION");
println!("Dojo version: {}", dojo_version);

let dojo_dep_str = dojo_dep.to_string();
println!("Dojo dep: {}", dojo_dep_str);

// Only in case of git dependency with an explicit tag, we check if the tag is the same as
// the current version.
if dojo_dep_str.contains("git+")
&& dojo_dep_str.contains("tag=v")
&& !dojo_dep_str.contains(dojo_version)
{
if let Ok(cp) = ws.current_package() {
let path =
if cp.id == package.id { package.manifest_path() } else { ws.manifest_path() };

anyhow::bail!(
"Found dojo-core version mismatch: expected {}. Please verify your dojo \
dependency in {}",
dojo_version,
path
)
} else {
// Virtual workspace.
anyhow::bail!(
"Found dojo-core version mismatch: expected {}. Please verify your dojo \
dependency in {}",
dojo_version,
ws.manifest_path()
)
}
}
}

Ok(())
}
6 changes: 6 additions & 0 deletions bin/sozo/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use scarb::ops::{self, CompileOpts};
use scarb_ui::args::{FeaturesSpec, PackagesFilter};
use tracing::trace;

use super::check_package_dojo_version;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::test";

#[derive(Debug, Clone, PartialEq, clap::ValueEnum)]
Expand Down Expand Up @@ -81,6 +83,10 @@ impl TestArgs {
ws.members().collect()
};

for p in &packages {
check_package_dojo_version(&ws, p)?;
}

let resolve = ops::resolve_workspace(&ws)?;

let opts = CompileOpts {
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "1.0.0-alpha.4"
version = "1.0.0-alpha.8"
dependencies = [
"armory",
"bestiary",
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
cairo-version = "=2.7.0"
name = "dojo_examples"
version = "1.0.0-alpha.4"
version = "1.0.0-alpha.8"
# Use the prelude with the less imports as possible
# from corelib.
edition = "2024_07"
Expand Down

0 comments on commit 1d32ab1

Please sign in to comment.