Skip to content

Commit

Permalink
fix: ensure namespaces in tests are correctly fetched
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jul 24, 2024
1 parent 0754d11 commit 74b1ac4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
11 changes: 5 additions & 6 deletions bin/sozo/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,18 @@ impl TestArgs {
opts.include_target_kinds.is_empty()
|| opts.include_target_kinds.contains(&cu.main_component().target_kind())
})
// TODOL: Need to find how to filter from packages with the compilation unit. We need something
// implementing PackagesSource trait.
.collect::<Vec<_>>();

for unit in compilation_units {
tracing::trace!(unit = %unit.name(), "Adding unit to test runner.");

let unit = if let CompilationUnit::Cairo(unit) = unit {
unit
} else {
continue;
};

// Injecting the cfg_set for the unit makes compiler panics.
// We rely then on the default namespace for testing...?

let props: Props = unit.main_component().target_props()?;
let db = build_root_database(&unit)?;

Expand Down Expand Up @@ -162,10 +161,10 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result<ProjectConfig> {
let crate_roots = unit
.components
.iter()
.filter(|model| !model.package.id.is_core())
.filter(|c| !c.package.id.is_core())
// NOTE: We're taking the first target of each compilation unit, which should always be the
// main package source root due to the order maintained by scarb.
.map(|model| (model.cairo_package_name(), model.targets[0].source_root().into()))
.map(|c| (c.cairo_package_name(), c.targets[0].source_root().into()))
.collect();

let corelib =
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ dependencies = [
[[package]]
name = "dojo_plugin"
version = "0.7.3"
source = "git+https://github.com/dojoengine/dojo?rev=d90b52b#d90b52b89749ac8af82f352dc08aa0b1378cfae6"
source = "git+https://github.com/dojoengine/dojo?rev=71b1f1a4#71b1f1a467534cbeeb901356f41e612ed4187bd1"
2 changes: 1 addition & 1 deletion crates/dojo-core/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "0.7.3"
[dependencies]
# Rev points to support for Cairo 2.7.0-rc.3 without any tag yet. Should be
# updated once a release is cut with `2.7.0-rc.3` support in it.
dojo_plugin = { git = "https://github.com/dojoengine/dojo", rev = "d90b52b" }
dojo_plugin = { git = "https://github.com/dojoengine/dojo", rev = "71b1f1a4" }
starknet = "=2.7.0-rc.3"

[lib]
Expand Down
36 changes: 29 additions & 7 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cairo_lang_defs::plugin::{
PluginDiagnostic, PluginGeneratedFile, PluginResult,
};
use cairo_lang_diagnostics::Severity;
use cairo_lang_filesystem::cfg::Cfg;
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_starknet::plugin::aux_data::StarkNetEventAuxData;
use cairo_lang_syntax::attribute::structured::{AttributeArgVariant, AttributeStructurize};
Expand Down Expand Up @@ -37,6 +38,7 @@ use crate::interface::DojoInterface;
use crate::introspect::{handle_introspect_enum, handle_introspect_struct};
use crate::model::handle_model_struct;
use crate::print::{handle_print_enum, handle_print_struct};
use crate::utils;

pub const DOJO_CONTRACT_ATTR: &str = "dojo::contract";
pub const DOJO_INTERFACE_ATTR: &str = "dojo::interface";
Expand Down Expand Up @@ -348,17 +350,37 @@ impl MacroPlugin for BuiltinDojoPlugin {
item_ast: ast::ModuleItem,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
// Metadata gives information from the crates from where `item_ast` was parsed.
// During the compilation phase, we inject namespace information into the `CfgSet`
// so that it can be used here.
let namespace_config: NamespaceConfig = metadata.cfg_set.into();
let namespace_config: NamespaceConfig = if db.cfg_set().contains(&Cfg::kv("target", "test"))
{
// In test mode, we can't inject namespace information into the `CfgSet`
// as the compiler panics.
match utils::get_namespace_config(db) {
Ok(config) => config,
Err(e) => {
return PluginResult {
code: Option::None,
diagnostics: vec![PluginDiagnostic {
stable_ptr: item_ast.stable_ptr().0,
message: format!("{e}"),
severity: Severity::Error,
}],
remove_original_item: false,
};
}
}
} else {
// Metadata gives information from the crates from where `item_ast` was parsed.
// During the compilation phase, we inject namespace information into the `CfgSet`
// so that it can be used here.
metadata.cfg_set.into()
};

// Avoid the whole plugin checks if there is no default namespace.
// The compiler already checked for invalid package configuration,
// so empty default namespace can be skipped.
if namespace_config.default.is_empty() {
return PluginResult::default();
}
// if namespace_config.default.is_empty() {
// return PluginResult::default();
// }

match item_ast {
ast::ModuleItem::Module(module_ast) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-lang/src/scarb_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result<ProjectConfig> {
}

#[derive(Debug)]
struct PackageData {
pub struct PackageData {
pub namespace_config: Option<NamespaceConfig>,
}

Expand Down Expand Up @@ -280,7 +280,7 @@ fn namespace_config_from_toml(
Ok(None)
}

fn cfg_set_from_component(
pub fn cfg_set_from_component(
c: &CompilationUnitComponent,
root_package_data: &PackageData,
ui: &Ui,
Expand Down
10 changes: 0 additions & 10 deletions crates/dojo-lang/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ pub fn get_namespace_config(db: &dyn SyntaxGroup) -> Result<NamespaceConfig> {
// Super verbose print, but useful to get the CfgSet.
// debug!(cfg_set = ?db.cfg_set(), crates = ?db.crates(), "Retrieving namespace
// configuration.");

if !db.cfg_set().contains(&cairo_lang_filesystem::cfg::Cfg {
key: "target".into(),
value: Some("dojo".into()),
}) {
// When a [lib] is compiled without the target "dojo", we shouldn't care about
// the namespace being retrieved.
return Ok(NamespaceConfig { default: "ignored_namespace".into(), mappings: None });
}

let crates = db.crates();

if crates.is_empty() {
Expand Down

0 comments on commit 74b1ac4

Please sign in to comment.