Skip to content

Commit

Permalink
feat(nargo): allow running nargo from any directory in package (#1010)
Browse files Browse the repository at this point in the history
* feat(nargo): search parent dirs for Nargo.toml

* fix: fix bad merge commit
  • Loading branch information
TomAFrench authored Mar 21, 2023
1 parent aba1ed2 commit 761fdb5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
31 changes: 18 additions & 13 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::path::{Path, PathBuf};

use color_eyre::eyre;

use crate::find_package_root;

mod fs;

mod check_cmd;
Expand Down Expand Up @@ -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(())
Expand Down
23 changes: 17 additions & 6 deletions crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf, InvalidPackageError> {
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<PathBuf, InvalidPackageError> {
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()))
}

Expand Down

0 comments on commit 761fdb5

Please sign in to comment.