Skip to content

Commit

Permalink
feat(nargo): Support custom entry points specified in TOML
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Aug 4, 2023
1 parent f5ce815 commit bb8c2b4
Show file tree
Hide file tree
Showing 9 changed files with 70 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}")]

Check warning on line 123 in crates/nargo_cli/src/errors.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Unxpected)
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 @@ -43,22 +43,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 @@ -116,6 +138,7 @@ struct PackageMetadata {
name: 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,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 bb8c2b4

Please sign in to comment.