Skip to content

Commit

Permalink
feat!: Support workspaces and package selection on every nargo command (
Browse files Browse the repository at this point in the history
#1992)

* feat!: Support workspaces and package selection on every nargo command

* add package name to contract directory

* print package name at the beginning of any stdout messages

* Remove circuit_name from compile command and use package name

* remove resolve_workspace_in_directory

* avoid resolving dependencies as a Workspace struct by always requiring it to be a Package

* chore: ensure workspace packages are distinct

* Update crates/nargo_cli/src/git.rs

* remove proof name argument and use package name, remove stdout printing of proof

* fix tests

* rename functions to be more descriptive

* add issue number to todo

---------

Co-authored-by: Tom French <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 1, 2023
1 parent 3a42368 commit 940b189
Show file tree
Hide file tree
Showing 55 changed files with 1,004 additions and 896 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ rustc_version = "0.4.0"
acvm.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
iter-extended.workspace = true
toml.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Directories
/// The directory for the `nargo contract` command output
pub(crate) const CONTRACT_DIR: &str = "contract";
pub const CONTRACT_DIR: &str = "contract";
/// The directory to store serialized circuit proofs.
pub(crate) const PROOFS_DIR: &str = "proofs";
pub const PROOFS_DIR: &str = "proofs";
/// The directory to store Noir source files
pub(crate) const SRC_DIR: &str = "src";
pub const SRC_DIR: &str = "src";
/// The directory to store circuits' serialized ACIR representations.
pub(crate) const TARGET_DIR: &str = "target";
pub const TARGET_DIR: &str = "target";

// Files
/// The file from which Nargo pulls prover inputs
pub(crate) const PROVER_INPUT_FILE: &str = "Prover";
pub const PROVER_INPUT_FILE: &str = "Prover";
/// The file from which Nargo pulls verifier inputs
pub(crate) const VERIFIER_INPUT_FILE: &str = "Verifier";
pub const VERIFIER_INPUT_FILE: &str = "Verifier";
/// The package definition file for a Noir project.
pub(crate) const PKG_FILE: &str = "Nargo.toml";
pub const PKG_FILE: &str = "Nargo.toml";

// Extensions
/// The extension for files containing circuit proofs.
pub(crate) const PROOF_EXT: &str = "proof";
pub const PROOF_EXT: &str = "proof";
/// The extension for files containing proof witnesses.
pub(crate) const WITNESS_EXT: &str = "tr";
pub const WITNESS_EXT: &str = "tr";
4 changes: 3 additions & 1 deletion crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
//! Noir Package Manager abbreviated is npm, which is already taken.
pub mod artifacts;
pub mod constants;
mod errors;
pub mod manifest;
pub mod ops;
pub mod package;
pub mod workspace;

pub use self::errors::NargoError;
26 changes: 0 additions & 26 deletions crates/nargo/src/manifest/errors.rs

This file was deleted.

147 changes: 0 additions & 147 deletions crates/nargo/src/manifest/mod.rs

This file was deleted.

33 changes: 33 additions & 0 deletions crates/nargo/src/package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{collections::BTreeMap, path::PathBuf};

use noirc_frontend::graph::{CrateName, CrateType};

use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};

#[derive(Clone)]
pub enum Dependency {
Local { package: Package },
Remote { package: Package },
}

#[derive(Clone)]
pub struct Package {
pub root_dir: PathBuf,
pub crate_type: CrateType,
pub entry_path: PathBuf,
pub name: CrateName,
pub dependencies: BTreeMap<CrateName, Dependency>,
}

impl Package {
pub fn prover_input_path(&self) -> PathBuf {
// TODO: This should be configurable, such as if we are looking for .json or .toml or custom paths
// For now it is hard-coded to be toml.
self.root_dir.join(format!("{PROVER_INPUT_FILE}.toml"))
}
pub fn verifier_input_path(&self) -> PathBuf {
// TODO: This should be configurable, such as if we are looking for .json or .toml or custom paths
// For now it is hard-coded to be toml.
self.root_dir.join(format!("{VERIFIER_INPUT_FILE}.toml"))
}
}
74 changes: 74 additions & 0 deletions crates/nargo/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,77 @@
// Then we use workspace to allow more than one. In the future, do not allow there to be
// both a binary and a library.
// - library will be default

use std::{
iter::{once, Once},
path::PathBuf,
slice,
};

use crate::{
constants::{CONTRACT_DIR, PROOFS_DIR, TARGET_DIR},
package::Package,
};

#[derive(Clone)]
pub struct Workspace {
pub root_dir: PathBuf,
pub members: Vec<Package>,
// If `Some()`, the `selected_package_index` is used to select the only `Package` when iterating a Workspace
pub selected_package_index: Option<usize>,
}

impl Workspace {
pub fn package_build_path(&self, package: &Package) -> PathBuf {
let name: String = package.name.clone().into();
self.target_directory_path().join(name)
}

pub fn contracts_directory_path(&self, package: &Package) -> PathBuf {
let name: String = package.name.clone().into();
self.root_dir.join(CONTRACT_DIR).join(name)
}

pub fn proofs_directory_path(&self) -> PathBuf {
self.root_dir.join(PROOFS_DIR)
}

pub fn target_directory_path(&self) -> PathBuf {
self.root_dir.join(TARGET_DIR)
}
}

pub enum IntoIter<'a, T> {
Only(Once<&'a T>),
All(slice::Iter<'a, T>),
}

impl<'a> IntoIterator for &'a Workspace {
type Item = &'a Package;
type IntoIter = IntoIter<'a, Package>;

fn into_iter(self) -> Self::IntoIter {
if let Some(index) = self.selected_package_index {
// Precondition: The selected_package_index was verified to be in-bounds before constructing workspace
let member = self
.members
.get(index)
.expect("Workspace constructed with invalid selected_package_index");

IntoIter::Only(once(member))
} else {
IntoIter::All(self.members.iter())
}
}
}

impl<'a> Iterator for IntoIter<'a, Package> {
type Item = &'a Package;

fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Only(iter) => iter.next(),
Self::All(iter) => iter.next(),
}
}
}
4 changes: 1 addition & 3 deletions crates/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ fn generate_tests(test_file: &mut File) {
if config_data["exclude"].contains(&test_name) { "#[ignore]" } else { "" };

let should_fail = config_data["fail"].contains(&test_name);
let is_workspace = test_dir.to_str().map_or(false, |s| s.contains("workspace"));

write!(
test_file,
Expand All @@ -96,8 +95,7 @@ fn execute_{test_sub_dir}_{test_name}() {{
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg(if {is_workspace} {{ "test" }} else {{ "execute" }});
cmd.arg("execute");
if {should_fail} {{
cmd.assert().failure();
Expand Down
Loading

0 comments on commit 940b189

Please sign in to comment.