From db40235b186459bf6666a81696cf90fa72089183 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 8 Aug 2023 16:30:06 -0700 Subject: [PATCH] chore: Move the remaining nargo_cli lib funcs into nargo core --- Cargo.lock | 3 +- crates/nargo/Cargo.toml | 1 + crates/nargo/src/lib.rs | 39 ++++++++++++++++++++ crates/nargo_cli/Cargo.toml | 4 +- crates/nargo_cli/src/cli/check_cmd.rs | 7 +--- crates/nargo_cli/src/cli/compile_cmd.rs | 2 +- crates/nargo_cli/src/cli/mod.rs | 2 +- crates/nargo_cli/src/cli/test_cmd.rs | 4 +- crates/nargo_cli/src/lib.rs | 49 ------------------------- crates/nargo_cli/src/main.rs | 14 ++++++- 10 files changed, 60 insertions(+), 65 deletions(-) delete mode 100644 crates/nargo_cli/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 91445bfa37f..1b55de79526 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1979,6 +1979,7 @@ version = "0.9.0" dependencies = [ "acvm", "base64", + "fm", "iter-extended", "noirc_abi", "noirc_driver", @@ -2001,7 +2002,6 @@ dependencies = [ "assert_fs", "async-lsp", "build-data", - "cfg-if", "clap", "color-eyre", "const_format", @@ -2026,7 +2026,6 @@ dependencies = [ "tokio", "toml", "tower", - "url", ] [[package]] diff --git a/crates/nargo/Cargo.toml b/crates/nargo/Cargo.toml index 3039268281c..32ca04ad34f 100644 --- a/crates/nargo/Cargo.toml +++ b/crates/nargo/Cargo.toml @@ -12,6 +12,7 @@ rustc_version = "0.4.0" [dependencies] acvm.workspace = true +fm.workspace = true noirc_abi.workspace = true noirc_driver.workspace = true noirc_frontend.workspace = true diff --git a/crates/nargo/src/lib.rs b/crates/nargo/src/lib.rs index fda02cf98c2..44261307886 100644 --- a/crates/nargo/src/lib.rs +++ b/crates/nargo/src/lib.rs @@ -14,4 +14,43 @@ pub mod ops; pub mod package; pub mod workspace; +use std::collections::BTreeMap; + +use fm::FileManager; +use noirc_driver::{add_dep, prepare_crate}; +use noirc_frontend::{ + graph::{CrateGraph, CrateId, CrateName}, + hir::Context, +}; +use package::{Dependency, Package}; + pub use self::errors::NargoError; + +pub fn prepare_dependencies( + context: &mut Context, + parent_crate: CrateId, + dependencies: &BTreeMap, +) { + for (dep_name, dep) in dependencies.iter() { + match dep { + Dependency::Remote { package } | Dependency::Local { package } => { + let crate_id = prepare_crate(context, &package.entry_path); + add_dep(context, parent_crate, crate_id, dep_name.clone()); + prepare_dependencies(context, crate_id, &package.dependencies); + } + } + } +} + +pub fn prepare_package(package: &Package) -> (Context, CrateId) { + // TODO: FileManager continues to leak into various crates + let fm = FileManager::new(&package.root_dir); + let graph = CrateGraph::default(); + let mut context = Context::new(fm, graph); + + let crate_id = prepare_crate(&mut context, &package.entry_path); + + prepare_dependencies(&mut context, crate_id, &package.dependencies); + + (context, crate_id) +} diff --git a/crates/nargo_cli/Cargo.toml b/crates/nargo_cli/Cargo.toml index 52194a898d4..0486e2ee234 100644 --- a/crates/nargo_cli/Cargo.toml +++ b/crates/nargo_cli/Cargo.toml @@ -18,10 +18,8 @@ build-data = "0.1.3" toml.workspace = true [dependencies] -cfg-if.workspace = true clap.workspace = true dirs.workspace = true -url.workspace = true iter-extended.workspace = true nargo.workspace = true nargo_toml.workspace = true @@ -31,7 +29,6 @@ noirc_frontend.workspace = true noirc_abi.workspace = true noirc_errors.workspace = true acvm.workspace = true -fm.workspace = true toml.workspace = true serde.workspace = true serde_json.workspace = true @@ -56,6 +53,7 @@ tempdir = "0.3.7" assert_cmd = "2.0.8" assert_fs = "1.0.10" predicates = "2.1.5" +fm.workspace = true [features] default = ["plonk_bn254"] diff --git a/crates/nargo_cli/src/cli/check_cmd.rs b/crates/nargo_cli/src/cli/check_cmd.rs index f44a6057ef8..44d4dca1a49 100644 --- a/crates/nargo_cli/src/cli/check_cmd.rs +++ b/crates/nargo_cli/src/cli/check_cmd.rs @@ -1,11 +1,8 @@ -use crate::{ - errors::{CliError, CompileError}, - prepare_package, -}; +use crate::errors::{CliError, CompileError}; use acvm::Backend; use clap::Args; use iter_extended::btree_map; -use nargo::package::Package; +use nargo::{package::Package, prepare_package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_abi::{AbiParameter, AbiType, MAIN_RETURN_NAME}; use noirc_driver::{check_crate, compute_function_signature, CompileOptions}; diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index 5df6d6d563b..4430b9bb851 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -3,6 +3,7 @@ use acvm::{acir::circuit::Circuit, Backend}; use iter_extended::try_vecmap; use iter_extended::vecmap; use nargo::package::Package; +use nargo::prepare_package; use nargo::{artifacts::contract::PreprocessedContract, NargoError}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_driver::{ @@ -16,7 +17,6 @@ use clap::Args; use nargo::ops::{preprocess_contract_function, preprocess_program}; use crate::errors::{CliError, CompileError}; -use crate::prepare_package; use super::fs::{ common_reference_string::{ diff --git a/crates/nargo_cli/src/cli/mod.rs b/crates/nargo_cli/src/cli/mod.rs index da4ff7d5a66..817bfabf57d 100644 --- a/crates/nargo_cli/src/cli/mod.rs +++ b/crates/nargo_cli/src/cli/mod.rs @@ -60,7 +60,7 @@ enum NargoCommand { Lsp(lsp_cmd::LspCommand), } -pub fn start_cli() -> eyre::Result<()> { +pub(crate) fn start_cli() -> eyre::Result<()> { let NargoCli { command, mut config } = NargoCli::parse(); // Search through parent directories to find package root if necessary. diff --git a/crates/nargo_cli/src/cli/test_cmd.rs b/crates/nargo_cli/src/cli/test_cmd.rs index 188b3d03ff5..ecbee5b4668 100644 --- a/crates/nargo_cli/src/cli/test_cmd.rs +++ b/crates/nargo_cli/src/cli/test_cmd.rs @@ -2,13 +2,13 @@ use std::io::Write; use acvm::{acir::native_types::WitnessMap, Backend}; use clap::Args; -use nargo::{ops::execute_circuit, package::Package}; +use nargo::{ops::execute_circuit, package::Package, prepare_package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_driver::{compile_no_check, CompileOptions}; use noirc_frontend::{graph::CrateName, hir::Context, node_interner::FuncId}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; -use crate::{cli::check_cmd::check_crate_and_report_errors, errors::CliError, prepare_package}; +use crate::{cli::check_cmd::check_crate_and_report_errors, errors::CliError}; use super::{compile_cmd::optimize_circuit, NargoConfig}; diff --git a/crates/nargo_cli/src/lib.rs b/crates/nargo_cli/src/lib.rs deleted file mode 100644 index 52a277ba48d..00000000000 --- a/crates/nargo_cli/src/lib.rs +++ /dev/null @@ -1,49 +0,0 @@ -#![forbid(unsafe_code)] -#![warn(unused_extern_crates)] -#![warn(unreachable_pub)] -#![warn(clippy::semicolon_if_nothing_returned)] - -//! Nargo is the package manager for Noir -//! This name was used because it sounds like `cargo` and -//! Noir Package Manager abbreviated is npm, which is already taken. - -use fm::FileManager; -use nargo::package::{Dependency, Package}; -use noirc_driver::{add_dep, prepare_crate}; -use noirc_frontend::{ - graph::{CrateGraph, CrateId, CrateName}, - hir::Context, -}; -use std::collections::BTreeMap; - -mod backends; -pub mod cli; -mod errors; - -fn prepare_dependencies( - context: &mut Context, - parent_crate: CrateId, - dependencies: &BTreeMap, -) { - for (dep_name, dep) in dependencies.iter() { - match dep { - Dependency::Remote { package } | Dependency::Local { package } => { - let crate_id = prepare_crate(context, &package.entry_path); - add_dep(context, parent_crate, crate_id, dep_name.clone()); - prepare_dependencies(context, crate_id, &package.dependencies); - } - } - } -} - -fn prepare_package(package: &Package) -> (Context, CrateId) { - let fm = FileManager::new(&package.root_dir); - let graph = CrateGraph::default(); - let mut context = Context::new(fm, graph); - - let crate_id = prepare_crate(&mut context, &package.entry_path); - - prepare_dependencies(&mut context, crate_id, &package.dependencies); - - (context, crate_id) -} diff --git a/crates/nargo_cli/src/main.rs b/crates/nargo_cli/src/main.rs index a79c43dad48..734dbdca2e7 100644 --- a/crates/nargo_cli/src/main.rs +++ b/crates/nargo_cli/src/main.rs @@ -1,7 +1,17 @@ #![forbid(unsafe_code)] +#![warn(unused_extern_crates)] +#![warn(unreachable_pub)] +#![warn(clippy::semicolon_if_nothing_returned)] + +//! Nargo is the package manager for Noir +//! This name was used because it sounds like `cargo` and +//! Noir Package Manager abbreviated is npm, which is already taken. + +mod backends; +mod cli; +mod errors; use color_eyre::{config::HookBuilder, eyre}; -use nargo_cli::cli::start_cli; const PANIC_MESSAGE: &str = "This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.\nIf there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml"; @@ -11,5 +21,5 @@ fn main() -> eyre::Result<()> { HookBuilder::default().display_env_section(false).panic_section(PANIC_MESSAGE).into_hooks(); panic_hook.install(); - start_cli() + cli::start_cli() }