Skip to content

Commit

Permalink
feat(nargo): Support custom entry points specified in TOML (#2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Aug 4, 2023
1 parent 9529157 commit effb02a
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 17 deletions.
9 changes: 7 additions & 2 deletions crates/nargo_cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ pub(crate) enum ManifestError {
#[error("Unxpected workspace definition found in {0}")]
UnexpectedWorkspace(PathBuf),

#[error("Cannot find file {0} which is required due to specifying the `{1}` package type")]
MissingEntryFile(PathBuf, PackageType),
#[error("Cannot find file {entry} which was specified as the `entry` field in {toml}")]
MissingEntryFile { toml: PathBuf, entry: PathBuf },

#[error(
r#"Cannot find file {entry} which is defaulted due to specifying `type = "{package_type}"` in {toml}"#
)]
MissingDefaultEntryFile { toml: PathBuf, entry: PathBuf, package_type: PackageType },

/// Invalid character `-` in package name
#[error("invalid character `-` in package name")]
Expand Down
51 changes: 37 additions & 14 deletions crates/nargo_cli/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,44 @@ impl PackageConfig {
None => return Err(ManifestError::MissingPackageType(root_dir.join("Nargo.toml"))),
};

let entry_path = match package_type {
PackageType::Library => root_dir.join("src").join("lib").with_extension(FILE_EXTENSION),
PackageType::Binary => root_dir.join("src").join("main").with_extension(FILE_EXTENSION),
let entry_path = if let Some(entry_path) = &self.package.entry {
let custom_entry_path = root_dir.join(entry_path);
if custom_entry_path.exists() {
custom_entry_path
} else {
return Err(ManifestError::MissingEntryFile {
toml: root_dir.join("Nargo.toml"),
entry: custom_entry_path,
});
}
} else {
let default_entry_path = match package_type {
PackageType::Library => {
root_dir.join("src").join("lib").with_extension(FILE_EXTENSION)
}
PackageType::Binary => {
root_dir.join("src").join("main").with_extension(FILE_EXTENSION)
}
};

if default_entry_path.exists() {
default_entry_path
} else {
return Err(ManifestError::MissingDefaultEntryFile {
toml: root_dir.join("Nargo.toml"),
entry: default_entry_path,
package_type,
});
}
};

if entry_path.exists() {
Ok(Package {
root_dir: root_dir.to_path_buf(),
entry_path,
package_type,
name,
dependencies,
})
} else {
Err(ManifestError::MissingEntryFile(entry_path, package_type))
}
Ok(Package {
root_dir: root_dir.to_path_buf(),
entry_path,
package_type,
name,
dependencies,
})
}
}

Expand Down Expand Up @@ -120,6 +142,7 @@ struct PackageMetadata {
name: Option<String>,
#[serde(alias = "type")]
package_type: Option<String>,
entry: Option<PathBuf>,
description: Option<String>,
authors: Option<Vec<String>>,
// If not compiler version is supplied, the latest is used
Expand Down
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ exclude = []


# List of tests (as their directory name) expecting to fail: if the test pass, we report an error.
fail = ["brillig_assert_fail", "dep_impl_primitive", "depend_on_bin", "workspace_fail", "workspace_missing_toml"]
fail = ["brillig_assert_fail", "custom_entry_not_found", "dep_impl_primitive", "depend_on_bin", "workspace_fail", "workspace_missing_toml"]
8 changes: 8 additions & 0 deletions crates/nargo_cli/tests/test_data/custom_entry/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "custom_entry"
type = "bin"
entry = "src/foobarbaz.nr"
authors = [""]
compiler_version = "0.1"

[dependencies]
1 change: 1 addition & 0 deletions crates/nargo_cli/tests/test_data/custom_entry/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(x: Field) {
assert(x == 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[1]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/82SMQ7DIAxFTSAdexYbQzBbr1JUcv8jtAODRbPVSP2LEUjP/v7sALDDt7ZRH6PibyKnWIxHSr3ETkxPjLVJxpTbISSUJb+iMHdJUmqrBSsl7nTmyueAbYYs/2G4C//O2P9mx0I9r1fnMGWn328LPMHUZ97j/eLOtPmKkPwCbgC7D7vKd7DPCBXyr3fqphm13hKQ6RMhBQAA","proving_key":null,"verification_key":null}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "custom_entry"
type = "bin"
# Testing that this file is missing and doesn't fallback to default `main.nr` file
entry = "src/foobarbaz.nr"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(x: Field) {
assert(x == 1);
}

0 comments on commit effb02a

Please sign in to comment.