Skip to content

Commit

Permalink
chore: add the ability to specify a default member (#1926)
Browse files Browse the repository at this point in the history
* first stage (no tests)

* add test

* fix typo

* add docs

* use `PathBuf` instead of `String`
  • Loading branch information
kek kek kek authored Jul 14, 2023
1 parent 1f8fd51 commit 1056ba1
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 7 deletions.
19 changes: 17 additions & 2 deletions crates/nargo/src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::Deserialize;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, path::PathBuf};

mod errors;
pub use self::errors::InvalidPackageError;
Expand Down Expand Up @@ -53,8 +53,12 @@ pub struct Workspace {
}

#[derive(Default, Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct WorkspaceConfig {
pub members: Vec<String>,
/// List of members in this workspace.
pub members: Vec<PathBuf>,
/// 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<PathBuf>,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -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());
}
15 changes: 11 additions & 4 deletions crates/nargo_cli/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
members = ["a"]
default-member = "a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "1"
y = "2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.8.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
members = ["a"]
default-member = "a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "1"
y = "2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.8.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 4 additions & 1 deletion crates/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1056ba1

Please sign in to comment.