From 74e277163d0d27e90b5124d6cb125acdc6335d8d Mon Sep 17 00:00:00 2001 From: Piotr Magiera Date: Wed, 7 Feb 2024 18:04:37 +0100 Subject: [PATCH] fix edition --- .../src/compilation/test_collector.rs | 22 ++++-- .../src/crate_collection.rs | 4 +- .../src/metadata.rs | 79 ++++++++++++++----- 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/extensions/scarb-snforge-test-collector/src/compilation/test_collector.rs b/extensions/scarb-snforge-test-collector/src/compilation/test_collector.rs index 9c56f3bb2..39d77b2c3 100644 --- a/extensions/scarb-snforge-test-collector/src/compilation/test_collector.rs +++ b/extensions/scarb-snforge-test-collector/src/compilation/test_collector.rs @@ -6,7 +6,9 @@ use cairo_lang_defs::db::DefsGroup; use cairo_lang_defs::ids::{FreeFunctionId, FunctionWithBodyId, ModuleId, ModuleItemId}; use cairo_lang_diagnostics::ToOption; use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; -use cairo_lang_filesystem::db::{AsFilesGroupMut, CrateConfiguration, FilesGroup, FilesGroupEx}; +use cairo_lang_filesystem::db::{ + AsFilesGroupMut, CrateConfiguration, CrateSettings, FilesGroup, FilesGroupEx, +}; use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory}; use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; use cairo_lang_project::{ProjectConfig, ProjectConfigContent}; @@ -107,8 +109,14 @@ pub fn collect_tests( b.build()? }; - let main_crate_id = - insert_lib_entrypoint_content_into_db(db, crate_name, crate_root, lib_content); + let main_package_crate_settings = compilation_unit.main_package_crate_settings(); + let main_crate_id = insert_lib_entrypoint_content_into_db( + db, + crate_name, + crate_root, + lib_content, + main_package_crate_settings, + ); if build_diagnostics_reporter(compilation_unit).check(db) { return Err(anyhow!( @@ -180,13 +188,15 @@ fn insert_lib_entrypoint_content_into_db( crate_name: &str, crate_root: &Path, lib_content: &str, + main_package_crate_settings: CrateSettings, ) -> CrateId { let main_crate_id = db.intern_crate(CrateLongId::Real(SmolStr::from(crate_name))); db.set_crate_config( main_crate_id, - Some(CrateConfiguration::default_for_root(Directory::Real( - crate_root.to_path_buf(), - ))), + Some(CrateConfiguration { + root: Directory::Real(crate_root.to_path_buf()), + settings: main_package_crate_settings, + }), ); let module_id = ModuleId::CrateRoot(main_crate_id); diff --git a/extensions/scarb-snforge-test-collector/src/crate_collection.rs b/extensions/scarb-snforge-test-collector/src/crate_collection.rs index 15b1cb4ed..998da8985 100644 --- a/extensions/scarb-snforge-test-collector/src/crate_collection.rs +++ b/extensions/scarb-snforge-test-collector/src/crate_collection.rs @@ -25,9 +25,9 @@ pub fn collect_test_compilation_targets( package_path: &Utf8Path, compilation_unit: &CompilationUnit, ) -> Result> { - let package_source_file_path = compilation_unit.source_file_path(); + let package_source_file_path = compilation_unit.main_package_source_file_path(); let mut compilation_targets = vec![TestCompilationTarget { - crate_root: compilation_unit.source_root(), + crate_root: compilation_unit.main_package_source_root(), crate_name: package_name.to_string(), crate_location: CrateLocation::Lib, lib_content: std::fs::read_to_string(package_source_file_path) diff --git a/extensions/scarb-snforge-test-collector/src/metadata.rs b/extensions/scarb-snforge-test-collector/src/metadata.rs index 3591be3cb..a5367427d 100644 --- a/extensions/scarb-snforge-test-collector/src/metadata.rs +++ b/extensions/scarb-snforge-test-collector/src/metadata.rs @@ -5,7 +5,9 @@ use cairo_lang_project::AllCratesConfig; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use camino::{Utf8Path, Utf8PathBuf}; use itertools::Itertools; -use scarb_metadata::{CompilationUnitMetadata, Metadata, PackageMetadata}; +use scarb_metadata::{ + CompilationUnitComponentMetadata, CompilationUnitMetadata, Metadata, PackageMetadata, +}; use serde_json::json; use smol_str::SmolStr; use std::path::PathBuf; @@ -67,14 +69,29 @@ pub fn compilation_unit_for_package<'a>( ); } + let main_package_metadata = unit_test_cu + .components + .iter() + .find(|comp| comp.package == package_metadata.id) + .into_iter() + .collect_vec(); + + assert_eq!( + main_package_metadata.len(), + 1, + "More than one cu component with main package id found" + ); + Ok(CompilationUnit { unit_metadata: unit_test_cu, + main_package_metadata: main_package_metadata[0], metadata, }) } pub struct CompilationUnit<'a> { unit_metadata: &'a CompilationUnitMetadata, + main_package_metadata: &'a CompilationUnitComponentMetadata, metadata: &'a Metadata, } @@ -121,21 +138,7 @@ impl CompilationUnit<'_> { }); ( SmolStr::from(&component.name), - CrateSettings { - edition: if let Some(edition) = pkg.edition.clone() { - let edition_value = serde_json::Value::String(edition); - serde_json::from_value(edition_value).unwrap() - } else { - Edition::default() - }, - cfg_set: component.cfg.clone().map(build_cfg_set), - // TODO (#1040): replace this with a macro - experimental_features: ExperimentalFeaturesConfig { - negative_impls: pkg - .experimental_features - .contains(&String::from("negative_impls")), - }, - }, + get_crate_settings_for_package(pkg, component.cfg.clone().map(build_cfg_set)), ) }) .collect(); @@ -156,12 +159,48 @@ impl CompilationUnit<'_> { .unwrap_or(true) } - pub fn source_root(&self) -> Utf8PathBuf { - self.unit_metadata.target.source_root().to_path_buf() + pub fn main_package_source_root(&self) -> Utf8PathBuf { + self.main_package_metadata.source_root().to_path_buf() + } + + pub fn main_package_source_file_path(&self) -> &Utf8Path { + &self.main_package_metadata.source_path } - pub fn source_file_path(&self) -> &Utf8Path { - &self.unit_metadata.target.source_path + pub fn main_package_crate_settings(&self) -> CrateSettings { + let package = self + .metadata + .packages + .iter() + .find(|package| package.id == self.main_package_metadata.package) + .expect("Main package not found in metadata"); + + get_crate_settings_for_package(package, None) + } +} + +fn get_crate_settings_for_package( + package: &PackageMetadata, + cfg_set: Option, +) -> CrateSettings { + let edition = package + .edition + .clone() + .map_or(Edition::default(), |edition| { + let edition_value = serde_json::Value::String(edition); + serde_json::from_value(edition_value).unwrap() + }); + // TODO (#1040): replace this with a macro + let experimental_features = ExperimentalFeaturesConfig { + negative_impls: package + .experimental_features + .contains(&String::from("negative_impls")), + }; + + CrateSettings { + edition, + cfg_set, + experimental_features, } }