From b75ba676720ef039dfd3280686e5439998463406 Mon Sep 17 00:00:00 2001 From: Tom French Date: Sat, 18 Mar 2023 20:33:44 +0000 Subject: [PATCH] feat(nargo): search parent dirs for Nargo.toml --- crates/nargo/src/cli/mod.rs | 31 ++++++++++++++++++------------- crates/nargo/src/lib.rs | 30 +++++++++++++++++++----------- 2 files changed, 37 insertions(+), 24 deletions(-) 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 e9e519d3335..47da06be117 100644 --- a/crates/nargo/src/lib.rs +++ b/crates/nargo/src/lib.rs @@ -28,20 +28,28 @@ mod git; mod manifest; mod resolver; -/// 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 pacakge 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 { - match find_file(current_path, "Nargo", "toml") { - Some(p) => Ok(p), - None => Err(CliError::Generic(format!( - "cannot find a Nargo.toml in {}", + current_path.ancestors().find_map(|dir| find_file(dir, "Nargo", "toml")).ok_or_else(|| { + CliError::Generic(format!( + "could not find Nargo.toml in {} or any parent directory", current_path.display() - ))), - } + )) + }) } fn lib_or_bin(current_path: &Path) -> Result<(PathBuf, CrateType), CliError> {