diff --git a/tooling/nargo_cli/Cargo.toml b/tooling/nargo_cli/Cargo.toml index acd5623871e..ef2cd548ebe 100644 --- a/tooling/nargo_cli/Cargo.toml +++ b/tooling/nargo_cli/Cargo.toml @@ -17,6 +17,7 @@ workspace = true [[bin]] name = "nargo" path = "src/main.rs" +required-features = ["build-binary"] [build-dependencies] build-data.workspace = true @@ -64,8 +65,8 @@ notify-debouncer-full = "0.3.1" termion = "3.0.0" # Logs -tracing-subscriber.workspace = true -tracing-appender = "0.2.3" +tracing-appender = { version = "0.2.3", optional = true } +tracing-subscriber = { workspace = true, optional = true } [target.'cfg(not(unix))'.dependencies] tokio-util = { version = "0.7.8", features = ["compat"] } @@ -97,3 +98,4 @@ harness = false [features] codegen-docs = ["dep:clap-markdown"] +build-binary = ["dep:tracing-subscriber", "dep:tracing-appender"] diff --git a/tooling/nargo_cli/src/cli/check_cmd.rs b/tooling/nargo_cli/src/cli/check_cmd.rs index 9059f1dd8e8..c99d437fbc8 100644 --- a/tooling/nargo_cli/src/cli/check_cmd.rs +++ b/tooling/nargo_cli/src/cli/check_cmd.rs @@ -23,21 +23,21 @@ use super::NargoConfig; /// Checks the constraint system for errors #[derive(Debug, Clone, Args)] #[clap(visible_alias = "c")] -pub(crate) struct CheckCommand { +pub struct CheckCommand { /// The name of the package to check #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Check all packages in the workspace #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, /// Force overwrite of existing files #[clap(long = "overwrite")] - allow_overwrite: bool, + pub allow_overwrite: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, } pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 0ad07c91ff4..53c5361dbe9 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -26,21 +26,21 @@ use rayon::prelude::*; /// Compile the program and its secret execution trace into ACIR format #[derive(Debug, Clone, Args)] -pub(crate) struct CompileCommand { +pub struct CompileCommand { /// The name of the package to compile #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Compile all packages in the workspace. #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, /// Watch workspace and recompile on changes. #[clap(long, hide = true)] - watch: bool, + pub watch: bool, } pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/dap_cmd.rs b/tooling/nargo_cli/src/cli/dap_cmd.rs index a84e961cfe7..eaf695577f0 100644 --- a/tooling/nargo_cli/src/cli/dap_cmd.rs +++ b/tooling/nargo_cli/src/cli/dap_cmd.rs @@ -28,28 +28,28 @@ use super::NargoConfig; use noir_debugger::errors::{DapError, LoadError}; #[derive(Debug, Clone, Args)] -pub(crate) struct DapCommand { +pub struct DapCommand { /// Override the expression width requested by the backend. #[arg(long, value_parser = parse_expression_width, default_value = "4")] - expression_width: ExpressionWidth, + pub expression_width: ExpressionWidth, #[clap(long)] - preflight_check: bool, + pub preflight_check: bool, #[clap(long)] - preflight_project_folder: Option, + pub preflight_project_folder: Option, #[clap(long)] - preflight_package: Option, + pub preflight_package: Option, #[clap(long)] - preflight_prover_name: Option, + pub preflight_prover_name: Option, #[clap(long)] - preflight_generate_acir: bool, + pub preflight_generate_acir: bool, #[clap(long)] - preflight_skip_instrumentation: bool, + pub preflight_skip_instrumentation: bool, } fn parse_expression_width(input: &str) -> Result { diff --git a/tooling/nargo_cli/src/cli/debug_cmd.rs b/tooling/nargo_cli/src/cli/debug_cmd.rs index e837f297475..6b2170944db 100644 --- a/tooling/nargo_cli/src/cli/debug_cmd.rs +++ b/tooling/nargo_cli/src/cli/debug_cmd.rs @@ -28,28 +28,28 @@ use crate::errors::CliError; /// Executes a circuit in debug mode #[derive(Debug, Clone, Args)] -pub(crate) struct DebugCommand { +pub struct DebugCommand { /// Write the execution witness to named file - witness_name: Option, + pub witness_name: Option, /// The name of the toml file which contains the inputs for the prover #[clap(long, short, default_value = PROVER_INPUT_FILE)] - prover_name: String, + pub prover_name: String, /// The name of the package to execute #[clap(long)] - package: Option, + pub package: Option, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, /// Force ACIR output (disabling instrumentation) #[clap(long)] - acir_mode: bool, + pub acir_mode: bool, /// Disable vars debug instrumentation (enabled by default) #[clap(long)] - skip_instrumentation: Option, + pub skip_instrumentation: Option, } pub(crate) fn run(args: DebugCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index 8dc71b1c7e5..625de0aedcc 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -24,30 +24,30 @@ use crate::errors::CliError; /// Executes a circuit to calculate its return value #[derive(Debug, Clone, Args)] #[clap(visible_alias = "e")] -pub(crate) struct ExecuteCommand { +pub struct ExecuteCommand { /// Write the execution witness to named file /// /// Defaults to the name of the package being executed. - witness_name: Option, + pub witness_name: Option, /// The name of the toml file which contains the inputs for the prover #[clap(long, short, default_value = PROVER_INPUT_FILE)] - prover_name: String, + pub prover_name: String, /// The name of the package to execute #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Execute all packages in the workspace #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, /// JSON RPC url to solve oracle calls #[clap(long)] - oracle_resolver: Option, + pub oracle_resolver: Option, } pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/export_cmd.rs b/tooling/nargo_cli/src/cli/export_cmd.rs index c3752db7fbd..54a4b2471bd 100644 --- a/tooling/nargo_cli/src/cli/export_cmd.rs +++ b/tooling/nargo_cli/src/cli/export_cmd.rs @@ -28,17 +28,17 @@ use super::NargoConfig; /// Exports functions marked with #[export] attribute #[derive(Debug, Clone, Args)] -pub(crate) struct ExportCommand { +pub struct ExportCommand { /// The name of the package to compile #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Compile all packages in the workspace #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, } pub(crate) fn run(args: ExportCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/fmt_cmd.rs b/tooling/nargo_cli/src/cli/fmt_cmd.rs index 66496db517c..69788ae383e 100644 --- a/tooling/nargo_cli/src/cli/fmt_cmd.rs +++ b/tooling/nargo_cli/src/cli/fmt_cmd.rs @@ -13,10 +13,10 @@ use super::NargoConfig; /// Format the Noir files in a workspace #[derive(Debug, Clone, Args)] -pub(crate) struct FormatCommand { +pub struct FormatCommand { /// Run noirfmt in check mode #[arg(long)] - check: bool, + pub check: bool, } pub(crate) fn run(args: FormatCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index f2e44fa893c..93230891925 100644 --- a/tooling/nargo_cli/src/cli/info_cmd.rs +++ b/tooling/nargo_cli/src/cli/info_cmd.rs @@ -27,24 +27,24 @@ use super::{ /// 2. Counts the final number gates in the circuit used by a backend #[derive(Debug, Clone, Args)] #[clap(visible_alias = "i")] -pub(crate) struct InfoCommand { +pub struct InfoCommand { /// The name of the package to detail #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Detail all packages in the workspace #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, /// Output a JSON formatted report. Changes to this format are not currently considered breaking. #[clap(long, hide = true)] - json: bool, + pub json: bool, #[clap(long, hide = true)] - profile_info: bool, + pub profile_info: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, } pub(crate) fn run(args: InfoCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/init_cmd.rs b/tooling/nargo_cli/src/cli/init_cmd.rs index c69775d3323..e9326b9352c 100644 --- a/tooling/nargo_cli/src/cli/init_cmd.rs +++ b/tooling/nargo_cli/src/cli/init_cmd.rs @@ -10,22 +10,22 @@ use std::path::PathBuf; /// Create a Noir project in the current directory. #[derive(Debug, Clone, Args)] -pub(crate) struct InitCommand { +pub struct InitCommand { /// Name of the package [default: current directory name] #[clap(long)] - name: Option, + pub name: Option, /// Use a library template #[arg(long, conflicts_with = "bin", conflicts_with = "contract")] - pub(crate) lib: bool, + pub lib: bool, /// Use a binary template [default] #[arg(long, conflicts_with = "lib", conflicts_with = "contract")] - pub(crate) bin: bool, + pub bin: bool, /// Use a contract template #[arg(long, conflicts_with = "lib", conflicts_with = "bin")] - pub(crate) contract: bool, + pub contract: bool, } const BIN_EXAMPLE: &str = include_str!("./noir_template_files/binary.nr"); diff --git a/tooling/nargo_cli/src/cli/lsp_cmd.rs b/tooling/nargo_cli/src/cli/lsp_cmd.rs index bfaa913b33a..10fa55b83e1 100644 --- a/tooling/nargo_cli/src/cli/lsp_cmd.rs +++ b/tooling/nargo_cli/src/cli/lsp_cmd.rs @@ -16,7 +16,7 @@ use crate::errors::CliError; /// /// VS Code Noir Language Support: https://marketplace.visualstudio.com/items?itemName=noir-lang.vscode-noir #[derive(Debug, Clone, Args)] -pub(crate) struct LspCommand; +pub struct LspCommand; pub(crate) fn run(_args: LspCommand, _config: NargoConfig) -> Result<(), CliError> { use tokio::runtime::Builder; diff --git a/tooling/nargo_cli/src/cli/mod.rs b/tooling/nargo_cli/src/cli/mod.rs index 10ec38ad1d5..d3812d05769 100644 --- a/tooling/nargo_cli/src/cli/mod.rs +++ b/tooling/nargo_cli/src/cli/mod.rs @@ -8,18 +8,18 @@ use color_eyre::eyre; mod fs; -mod check_cmd; -mod compile_cmd; -mod dap_cmd; -mod debug_cmd; -mod execute_cmd; -mod export_cmd; -mod fmt_cmd; -mod info_cmd; -mod init_cmd; -mod lsp_cmd; -mod new_cmd; -mod test_cmd; +pub mod check_cmd; +pub mod compile_cmd; +pub mod dap_cmd; +pub mod debug_cmd; +pub mod execute_cmd; +pub mod export_cmd; +pub mod fmt_cmd; +pub mod info_cmd; +pub mod init_cmd; +pub mod lsp_cmd; +pub mod new_cmd; +pub mod test_cmd; const GIT_HASH: &str = env!("GIT_COMMIT"); const IS_DIRTY: &str = env!("GIT_DIRTY"); @@ -35,25 +35,23 @@ static VERSION_STRING: &str = formatcp!( #[derive(Parser, Debug)] #[command(name="nargo", author, version=VERSION_STRING, about, long_about = None)] -struct NargoCli { +pub struct NargoCli { #[command(subcommand)] - command: NargoCommand, + pub command: NargoCommand, #[clap(flatten)] - config: NargoConfig, + pub config: NargoConfig, } -#[non_exhaustive] #[derive(Args, Clone, Debug)] -pub(crate) struct NargoConfig { +pub struct NargoConfig { // REMINDER: Also change this flag in the LSP test lens if renamed #[arg(long, hide = true, global = true, default_value = "./")] - program_dir: PathBuf, + pub program_dir: PathBuf, } -#[non_exhaustive] #[derive(Subcommand, Clone, Debug)] -enum NargoCommand { +pub enum NargoCommand { Check(check_cmd::CheckCommand), Fmt(fmt_cmd::FormatCommand), #[command(alias = "build")] @@ -71,9 +69,22 @@ enum NargoCommand { Dap(dap_cmd::DapCommand), } +/// Parse the command line arguments and execute the command. #[cfg(not(feature = "codegen-docs"))] +pub fn start_cli() -> eyre::Result<()> { + run_cmd(NargoCli::parse()) +} + +#[cfg(feature = "codegen-docs")] pub(crate) fn start_cli() -> eyre::Result<()> { - let NargoCli { command, mut config } = NargoCli::parse(); + let markdown: String = clap_markdown::help_markdown::(); + println!("{markdown}"); + Ok(()) +} + +/// Execute the CLI command. +pub fn run_cmd(cmd: NargoCli) -> eyre::Result<()> { + let NargoCli { command, mut config } = cmd; // If the provided `program_dir` is relative, make it absolute by joining it to the current directory. if !config.program_dir.is_absolute() { @@ -105,10 +116,3 @@ pub(crate) fn start_cli() -> eyre::Result<()> { Ok(()) } - -#[cfg(feature = "codegen-docs")] -pub(crate) fn start_cli() -> eyre::Result<()> { - let markdown: String = clap_markdown::help_markdown::(); - println!("{markdown}"); - Ok(()) -} diff --git a/tooling/nargo_cli/src/cli/new_cmd.rs b/tooling/nargo_cli/src/cli/new_cmd.rs index db9257b8aa0..f8a9d206cbd 100644 --- a/tooling/nargo_cli/src/cli/new_cmd.rs +++ b/tooling/nargo_cli/src/cli/new_cmd.rs @@ -7,25 +7,25 @@ use std::path::PathBuf; /// Create a Noir project in a new directory. #[derive(Debug, Clone, Args)] -pub(crate) struct NewCommand { +pub struct NewCommand { /// The path to save the new project - path: PathBuf, + pub path: PathBuf, /// Name of the package [default: package directory name] #[clap(long)] - name: Option, + pub name: Option, /// Use a library template #[arg(long, conflicts_with = "bin", conflicts_with = "contract")] - pub(crate) lib: bool, + pub lib: bool, /// Use a binary template [default] #[arg(long, conflicts_with = "lib", conflicts_with = "contract")] - pub(crate) bin: bool, + pub bin: bool, /// Use a contract template #[arg(long, conflicts_with = "lib", conflicts_with = "bin")] - pub(crate) contract: bool, + pub contract: bool, } pub(crate) fn run(args: NewCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index 7b0201226ef..13726629d82 100644 --- a/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/tooling/nargo_cli/src/cli/test_cmd.rs @@ -23,32 +23,32 @@ use super::NargoConfig; /// Run the tests for this program #[derive(Debug, Clone, Args)] #[clap(visible_alias = "t")] -pub(crate) struct TestCommand { +pub struct TestCommand { /// If given, only tests with names containing this string will be run - test_name: Option, + pub test_name: Option, /// Display output of `println` statements #[arg(long)] - show_output: bool, + pub show_output: bool, /// Only run tests that match exactly #[clap(long)] - exact: bool, + pub exact: bool, /// The name of the package to test #[clap(long, conflicts_with = "workspace")] - package: Option, + pub package: Option, /// Test all packages in the workspace #[clap(long, conflicts_with = "package")] - workspace: bool, + pub workspace: bool, #[clap(flatten)] - compile_options: CompileOptions, + pub compile_options: CompileOptions, /// JSON RPC url to solve oracle calls #[clap(long)] - oracle_resolver: Option, + pub oracle_resolver: Option, } pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError> { diff --git a/tooling/nargo_cli/src/lib.rs b/tooling/nargo_cli/src/lib.rs new file mode 100644 index 00000000000..5bd20108439 --- /dev/null +++ b/tooling/nargo_cli/src/lib.rs @@ -0,0 +1,13 @@ +#![forbid(unsafe_code)] +#![warn(unreachable_pub)] +#![warn(clippy::semicolon_if_nothing_returned)] +#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] + +//! 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. + +pub mod cli; +mod errors; + +pub 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"; diff --git a/tooling/nargo_cli/src/main.rs b/tooling/nargo_cli/src/main.rs index a407d467ced..da274a27f38 100644 --- a/tooling/nargo_cli/src/main.rs +++ b/tooling/nargo_cli/src/main.rs @@ -1,15 +1,3 @@ -#![forbid(unsafe_code)] -#![warn(unreachable_pub)] -#![warn(clippy::semicolon_if_nothing_returned)] -#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] - -//! 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 cli; -mod errors; - use std::env; use color_eyre::config::HookBuilder; @@ -17,7 +5,7 @@ use color_eyre::config::HookBuilder; use tracing_appender::rolling; use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; -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"; +use nargo_cli::{cli, PANIC_MESSAGE}; fn main() { // Setup tracing