diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 50f9de29997..27f98b33ba6 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -6,6 +6,8 @@ use std::path::{Path, PathBuf}; use color_eyre::eyre; +use crate::find_package_root; + mod fs; mod check_cmd; @@ -59,19 +61,22 @@ enum NargoCommand { } pub fn start_cli() -> eyre::Result<()> { - let matches = NargoCli::parse(); - - match matches.command { - NargoCommand::New(args) => new_cmd::run(args, matches.config), - NargoCommand::Check(args) => check_cmd::run(args, matches.config), - NargoCommand::Compile(args) => compile_cmd::run(args, matches.config), - NargoCommand::Execute(args) => execute_cmd::run(args, matches.config), - NargoCommand::Prove(args) => prove_cmd::run(args, matches.config), - NargoCommand::Verify(args) => verify_cmd::run(args, matches.config), - NargoCommand::Preprocess(args) => preprocess_cmd::run(args, matches.config), - NargoCommand::Test(args) => test_cmd::run(args, matches.config), - NargoCommand::Gates(args) => gates_cmd::run(args, matches.config), - NargoCommand::CodegenVerifier(args) => codegen_verifier_cmd::run(args, matches.config), + let NargoCli { command, mut config } = NargoCli::parse(); + + // Search through parent directories to find package root. + config.program_dir = find_package_root(&config.program_dir)?; + + match command { + NargoCommand::New(args) => new_cmd::run(args, config), + NargoCommand::Check(args) => check_cmd::run(args, config), + NargoCommand::Compile(args) => compile_cmd::run(args, config), + NargoCommand::Execute(args) => execute_cmd::run(args, config), + NargoCommand::Prove(args) => prove_cmd::run(args, config), + NargoCommand::Verify(args) => verify_cmd::run(args, config), + NargoCommand::Preprocess(args) => preprocess_cmd::run(args, config), + NargoCommand::Test(args) => test_cmd::run(args, config), + NargoCommand::Gates(args) => gates_cmd::run(args, config), + NargoCommand::CodegenVerifier(args) => codegen_verifier_cmd::run(args, config), }?; Ok(()) diff --git a/crates/nargo/src/lib.rs b/crates/nargo/src/lib.rs index 7c5a988e458..6ea9fe46058 100644 --- a/crates/nargo/src/lib.rs +++ b/crates/nargo/src/lib.rs @@ -26,14 +26,25 @@ fn nargo_crates() -> PathBuf { dirs::home_dir().unwrap().join("nargo") } -/// Searches for the Nargo.toml file +/// Returns the path of the root directory of the package containing `current_path`. /// -/// XXX: In the end, this should find the root of the project and check -/// for the Nargo.toml file there -/// However, it should only do this after checking the current path -/// This allows the use of workspace settings in the future. +/// Returns a `CliError` if no parent directories of `current_path` contain a manifest file. +fn find_package_root(current_path: &Path) -> Result { + let manifest_path = find_package_manifest(current_path)?; + + let package_root = + manifest_path.parent().expect("infallible: manifest file path can't be root directory"); + + Ok(package_root.to_path_buf()) +} + +/// Returns the path of the manifest file (`Nargo.toml`) of the package containing `current_path`. +/// +/// Returns a `CliError` if no parent directories of `current_path` contain a manifest file. fn find_package_manifest(current_path: &Path) -> Result { - find_file(current_path, "Nargo", "toml") + current_path + .ancestors() + .find_map(|dir| find_file(dir, "Nargo", "toml")) .ok_or_else(|| InvalidPackageError::MissingManifestFile(current_path.to_path_buf())) }