From 94f4d3c5a0891f5ef730795cdb91002a45765d73 Mon Sep 17 00:00:00 2001 From: chen <23054115+cwkang1998@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:04:53 +0200 Subject: [PATCH] chore: remove scarb as dependency (WIP) As we move towards having a universal verifier, we aim to reduce dependencies that associate with a specific version of cairo. Scarb is one such dependency that in the first place does not support being imported as a library. We want to first remove Scarb as our dependency, keeping those that are supported as a library (`scarb-metadata` & `scarb-ui`), and try to utilize cairo deps to replace their functionality. This should make the maintenance easier and also reduce bugs that are caused by this misuse of Scarb as a library. --- .../src/compiler/mod.rs | 13 ++++++----- .../src/compiler/scarb_utils.rs | 9 +++++--- .../voyager-resolver-cairo/src/utils/mod.rs | 22 +++++++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/voyager-resolver-cairo/src/compiler/mod.rs b/crates/voyager-resolver-cairo/src/compiler/mod.rs index ff47fb7..1d5d1c8 100644 --- a/crates/voyager-resolver-cairo/src/compiler/mod.rs +++ b/crates/voyager-resolver-cairo/src/compiler/mod.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use cairo_lang_compiler::db::RootDatabase; use cairo_lang_defs::ids::ModuleId; use cairo_lang_filesystem::db::FilesGroup; @@ -28,7 +28,6 @@ use crate::graph::{create_graph, get_required_module_for_contracts, EdgeWeight}; // use crate::graph::display_graphviz; use scarb::compiler::{CompilationUnit, Compiler}; use scarb::core::{TargetKind, Workspace}; -use scarb::flock::Filesystem; pub struct VoyagerGenerator; @@ -157,9 +156,6 @@ impl Compiler for VoyagerGenerator { } }); - // treat target dir as a Filesystem - let target_dir = Filesystem::new(target_dir); - create_attachment_files(&attachment_modules_data, &target_dir) .with_context(|| "Failed to create attachment files")?; @@ -177,7 +173,12 @@ impl Compiler for VoyagerGenerator { generate_scarb_updated_files(metadata, &target_dir, required_modules)?; let package_name = unit.main_component().package.id.name.to_string(); - let generated_crate_dir = target_dir.path_existent().unwrap().join(package_name); + + if !target_dir.exists() { + return Err(anyhow!("unable to locate target directory")); + } + + let generated_crate_dir = target_dir.join(package_name); //Locally run scarb build to make sure that everything compiles correctly before sending the files to voyager. run_scarb_build(generated_crate_dir.as_str())?; diff --git a/crates/voyager-resolver-cairo/src/compiler/scarb_utils.rs b/crates/voyager-resolver-cairo/src/compiler/scarb_utils.rs index 46fa875..29976cf 100644 --- a/crates/voyager-resolver-cairo/src/compiler/scarb_utils.rs +++ b/crates/voyager-resolver-cairo/src/compiler/scarb_utils.rs @@ -1,5 +1,5 @@ use anyhow::{anyhow, ensure, Context, Result}; -use scarb::flock::Filesystem; +use camino::Utf8Path; use serde::Deserialize; use std::fs; use std::path::{Path, PathBuf}; @@ -135,7 +135,7 @@ pub fn get_table_mut<'a>(doc: &'a mut Document, path: &[&str]) -> Result<&'a mut /// * Generating an updated Scarb.toml file fails. pub fn generate_scarb_updated_files( scarb_metadata: scarb_metadata::Metadata, - target_dir: &Filesystem, + target_dir: &Utf8Path, required_modules: Vec<&CairoModule>, ) -> Result<()> { let mut metadata = scarb_metadata.clone(); @@ -153,7 +153,10 @@ pub fn generate_scarb_updated_files( for package in metadata.packages { let manifest_path = package.manifest_path; - let target_path = target_dir.path_existent()?.join(package.name); + if !target_dir.exists() { + return Err(anyhow!("unable to locate target dir")); + } + let target_path = target_dir.join(package.name); generate_updated_scarb_toml( manifest_path.into_std_path_buf(), target_path.as_std_path(), diff --git a/crates/voyager-resolver-cairo/src/utils/mod.rs b/crates/voyager-resolver-cairo/src/utils/mod.rs index 6557b4b..fd3da04 100644 --- a/crates/voyager-resolver-cairo/src/utils/mod.rs +++ b/crates/voyager-resolver-cairo/src/utils/mod.rs @@ -1,10 +1,10 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; +use camino::Utf8Path; use crate::compiler::scarb_utils::read_additional_scarb_manifest_metadata; use crate::model::{CairoAttachmentModule, CairoImport, CairoModule, ModulePath}; use scarb::core::Workspace; -use scarb::flock::Filesystem; use std::collections::HashMap; use std::fs; @@ -86,15 +86,17 @@ pub fn get_import_remaps(modules_to_verify: Vec<&CairoModule>) -> Vec, - target_dir: &Filesystem, + target_dir: &Utf8Path, ) -> Result<()> { for (parent_module, attachment_module) in attachment_modules { let child_modules = &attachment_module.children; let mut filename = String::new(); let path_split = parent_module.0.split("::"); let crate_name = parent_module.get_crate(); - let target_path = target_dir.path_existent()?; - let source_path = target_path.join(crate_name).join("src"); + if !target_dir.exists() { + return Err(anyhow!("failed to create attachment files")) + } + let source_path = target_dir.join(crate_name).join("src"); filename = match path_split.clone().count() { 1 => "lib.cairo".to_string(), _ => { @@ -139,12 +141,14 @@ pub fn create_attachment_files( /// TODO: add comprehensive test for this. pub fn copy_required_files( required_modules: &Vec<&CairoModule>, - target_dir: &Filesystem, + target_dir: &Utf8Path, ws: &Workspace, ) -> Result<()> { let root_path = ws.root(); let mut root_parts = root_path.components().peekable(); - let target_path = target_dir.path_existent()?; + if !target_dir.exists() { + return Err(anyhow!("unable to resolve target dir")); + } // Skip the first component if it is the Windows or Unix root if let Some(component) = root_parts.peek() { @@ -169,7 +173,7 @@ pub fn copy_required_files( // Construct the destination path for the .cairo & readme & license file let root_dir = module.get_root_dir()?; let filepath_relative = filepath.strip_prefix(root_dir.clone())?; - let dest_path = Path::new(target_path) + let dest_path = Path::new(target_dir) .join(crate_name.clone()) .join(filepath_relative); @@ -190,7 +194,7 @@ pub fn copy_required_files( // Attempt to copy the readme and license files to the target directory let root_dir_clone = root_dir.clone(); let base_source_root_path = Path::new(&root_dir_clone); - let base_dest_root_path = Path::new(target_path).join(crate_name); + let base_dest_root_path = Path::new(target_dir).join(crate_name); let readme_source_path = base_source_root_path.join(&additional_metadata.readme); let readme_dest_path = base_dest_root_path.join(&additional_metadata.readme);