Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nargo): allow running nargo from any directory in package #1010

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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