diff --git a/crates/nargo_cli/src/errors.rs b/crates/nargo_cli/src/errors.rs index 45d2dbae827..e671dc2459f 100644 --- a/crates/nargo_cli/src/errors.rs +++ b/crates/nargo_cli/src/errors.rs @@ -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")] diff --git a/crates/nargo_cli/src/manifest.rs b/crates/nargo_cli/src/manifest.rs index a01fbe15c4c..adf5000d9f0 100644 --- a/crates/nargo_cli/src/manifest.rs +++ b/crates/nargo_cli/src/manifest.rs @@ -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, + }) } } @@ -116,6 +138,7 @@ struct PackageMetadata { name: String, #[serde(alias = "type")] package_type: Option, + entry: Option, description: Option, authors: Option>, // If not compiler version is supplied, the latest is used diff --git a/crates/nargo_cli/tests/test_data/config.toml b/crates/nargo_cli/tests/test_data/config.toml index 4f09fe87b12..6de06aa8a12 100644 --- a/crates/nargo_cli/tests/test_data/config.toml +++ b/crates/nargo_cli/tests/test_data/config.toml @@ -4,6 +4,7 @@ exclude = [] # List of tests (as their directory name) expecting to fail: if the test pass, we report an error. fail = [ "brillig_assert_fail", + "custom_entry_not_found", "dep_impl_primitive", "depend_on_bin", "workspace_fail", diff --git a/crates/nargo_cli/tests/test_data/custom_entry/Nargo.toml b/crates/nargo_cli/tests/test_data/custom_entry/Nargo.toml new file mode 100644 index 00000000000..a8cc75072b1 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry/Nargo.toml @@ -0,0 +1,8 @@ +[package] +name = "custom_entry" +type = "bin" +entry = "src/foobarbaz.nr" +authors = [""] +compiler_version = "0.1" + +[dependencies] diff --git a/crates/nargo_cli/tests/test_data/custom_entry/Prover.toml b/crates/nargo_cli/tests/test_data/custom_entry/Prover.toml new file mode 100644 index 00000000000..4dd6b405159 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry/Prover.toml @@ -0,0 +1 @@ +x = "1" diff --git a/crates/nargo_cli/tests/test_data/custom_entry/src/foobarbaz.nr b/crates/nargo_cli/tests/test_data/custom_entry/src/foobarbaz.nr new file mode 100644 index 00000000000..00e94414c0b --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry/src/foobarbaz.nr @@ -0,0 +1,3 @@ +fn main(x: Field) { + assert(x == 1); +} diff --git a/crates/nargo_cli/tests/test_data/custom_entry_not_found/Nargo.toml b/crates/nargo_cli/tests/test_data/custom_entry_not_found/Nargo.toml new file mode 100644 index 00000000000..0ccb94aff1a --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry_not_found/Nargo.toml @@ -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] diff --git a/crates/nargo_cli/tests/test_data/custom_entry_not_found/Prover.toml b/crates/nargo_cli/tests/test_data/custom_entry_not_found/Prover.toml new file mode 100644 index 00000000000..4dd6b405159 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry_not_found/Prover.toml @@ -0,0 +1 @@ +x = "1" diff --git a/crates/nargo_cli/tests/test_data/custom_entry_not_found/src/main.nr b/crates/nargo_cli/tests/test_data/custom_entry_not_found/src/main.nr new file mode 100644 index 00000000000..00e94414c0b --- /dev/null +++ b/crates/nargo_cli/tests/test_data/custom_entry_not_found/src/main.nr @@ -0,0 +1,3 @@ +fn main(x: Field) { + assert(x == 1); +}