From 1056ba1b8048ec817137a16e0b343f5e7bf15139 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Fri, 14 Jul 2023 09:05:19 -0400 Subject: [PATCH] chore: add the ability to specify a default member (#1926) * first stage (no tests) * add test * fix typo * add docs * use `PathBuf` instead of `String` --- crates/nargo/src/manifest/mod.rs | 19 +++++++++++++++++-- crates/nargo_cli/src/resolver.rs | 15 +++++++++++---- .../workspace_default_member/Nargo.toml | 3 +++ .../workspace_default_member/Prover.toml | 2 ++ .../workspace_default_member/a/Nargo.toml | 5 +++++ .../workspace_default_member/a/src/main.nr | 11 +++++++++++ .../workspace_default_member/Nargo.toml | 3 +++ .../workspace_default_member/Prover.toml | 2 ++ .../workspace_default_member/a/Nargo.toml | 5 +++++ .../workspace_default_member/a/src/main.nr | 11 +++++++++++ crates/noirc_frontend/src/hir/mod.rs | 5 ++++- 11 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data/workspace_default_member/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/workspace_default_member/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data/workspace_default_member/a/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/workspace_default_member/a/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/src/main.nr diff --git a/crates/nargo/src/manifest/mod.rs b/crates/nargo/src/manifest/mod.rs index 4d91956330b..ea51d622d82 100644 --- a/crates/nargo/src/manifest/mod.rs +++ b/crates/nargo/src/manifest/mod.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, path::PathBuf}; mod errors; pub use self::errors::InvalidPackageError; @@ -53,8 +53,12 @@ pub struct Workspace { } #[derive(Default, Debug, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] pub struct WorkspaceConfig { - pub members: Vec, + /// List of members in this workspace. + pub members: Vec, + /// Specifies the default crate to interact with in the context (similarly to how we have nargo as the default crate in this repository). + pub default_member: Option, } #[allow(dead_code)] @@ -107,3 +111,14 @@ fn parse_workspace_toml() { assert!(Manifest::from_toml_str(src).is_ok()); } + +#[test] +fn parse_workspace_default_member_toml() { + let src = r#" + [workspace] + members = ["a", "b"] + default-member = "a" + "#; + + assert!(Manifest::from_toml_str(src).is_ok()); +} diff --git a/crates/nargo_cli/src/resolver.rs b/crates/nargo_cli/src/resolver.rs index 4a4fcd241c5..5fc158d1d7f 100644 --- a/crates/nargo_cli/src/resolver.rs +++ b/crates/nargo_cli/src/resolver.rs @@ -87,15 +87,22 @@ pub(crate) fn resolve_root_manifest( resolve_manifest(&mut context, crate_id, package, pkg_root)?; } Manifest::Workspace(workspace) => { - let members = workspace.config.members; - let root = match members.last() { - Some(member) => dir_path.join(member), + let config = workspace.config; + let members = config.members; + + let maybe_local = config + .default_member + .or_else(|| members.last().cloned()) + .map(|member| dir_path.join(member)); + + let default_member = match maybe_local { + Some(member) => member, None => { return Err(DependencyResolutionError::EmptyWorkspace { path: manifest_path }) } }; - let (entry_path, _crate_type) = super::lib_or_bin(root)?; + let (entry_path, _crate_type) = super::lib_or_bin(default_member)?; let _local = create_local_crate(&mut context, entry_path, CrateType::Workspace); for member in members { diff --git a/crates/nargo_cli/tests/test_data/workspace_default_member/Nargo.toml b/crates/nargo_cli/tests/test_data/workspace_default_member/Nargo.toml new file mode 100644 index 00000000000..3828c5d0677 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/workspace_default_member/Nargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = ["a"] +default-member = "a" diff --git a/crates/nargo_cli/tests/test_data/workspace_default_member/Prover.toml b/crates/nargo_cli/tests/test_data/workspace_default_member/Prover.toml new file mode 100644 index 00000000000..8c12ebba6cf --- /dev/null +++ b/crates/nargo_cli/tests/test_data/workspace_default_member/Prover.toml @@ -0,0 +1,2 @@ +x = "1" +y = "2" diff --git a/crates/nargo_cli/tests/test_data/workspace_default_member/a/Nargo.toml b/crates/nargo_cli/tests/test_data/workspace_default_member/a/Nargo.toml new file mode 100644 index 00000000000..dc0c2f8917c --- /dev/null +++ b/crates/nargo_cli/tests/test_data/workspace_default_member/a/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.8.0" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/workspace_default_member/a/src/main.nr b/crates/nargo_cli/tests/test_data/workspace_default_member/a/src/main.nr new file mode 100644 index 00000000000..206dc46d57a --- /dev/null +++ b/crates/nargo_cli/tests/test_data/workspace_default_member/a/src/main.nr @@ -0,0 +1,11 @@ +fn main(x : Field, y : pub Field) { + assert(x != y); +} + +#[test] +fn test_main() { + main(1, 2); + + // Uncomment to make test fail + // main(1, 1); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Nargo.toml new file mode 100644 index 00000000000..3828c5d0677 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Nargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = ["a"] +default-member = "a" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Prover.toml new file mode 100644 index 00000000000..8c12ebba6cf --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/Prover.toml @@ -0,0 +1,2 @@ +x = "1" +y = "2" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/Nargo.toml new file mode 100644 index 00000000000..dc0c2f8917c --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.8.0" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/src/main.nr new file mode 100644 index 00000000000..206dc46d57a --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/workspace_default_member/a/src/main.nr @@ -0,0 +1,11 @@ +fn main(x : Field, y : pub Field) { + assert(x != y); +} + +#[test] +fn test_main() { + main(1, 2); + + // Uncomment to make test fail + // main(1, 1); +} diff --git a/crates/noirc_frontend/src/hir/mod.rs b/crates/noirc_frontend/src/hir/mod.rs index e85d054d4de..cd188f46392 100644 --- a/crates/noirc_frontend/src/hir/mod.rs +++ b/crates/noirc_frontend/src/hir/mod.rs @@ -71,7 +71,10 @@ impl Context { // Check the crate type // We don't panic here to allow users to `evaluate` libraries which will do nothing - if self.crate_graph[*crate_id].crate_type == CrateType::Binary { + if matches!( + self.crate_graph[*crate_id].crate_type, + CrateType::Binary | CrateType::Workspace + ) { // All Binaries should have a main function local_crate.main_function() } else {