Skip to content

Commit

Permalink
chore!: Require package names in Nargo.toml files (#2056)
Browse files Browse the repository at this point in the history
* chore!: Require package names in `Nargo.toml` files

* feat: add a helpful error message for missing names in Nargo.toml

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
  • Loading branch information
phated and TomAFrench authored Jul 27, 2023
1 parent 898a9fa commit bb28223
Show file tree
Hide file tree
Showing 138 changed files with 338 additions and 213 deletions.
26 changes: 24 additions & 2 deletions crates/nargo/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ pub struct WorkspaceConfig {
#[allow(dead_code)]
#[derive(Default, Debug, Deserialize, Clone)]
pub struct PackageMetadata {
pub name: Option<String>,
// Note: a package name is not needed unless there is a registry
#[serde(default = "panic_missing_name")]
pub name: String,
description: Option<String>,
authors: Vec<String>,
// If not compiler version is supplied, the latest is used
// For now, we state that all packages must be compiled under the same
Expand All @@ -77,6 +78,26 @@ pub struct PackageMetadata {
license: Option<String>,
}

// TODO: Remove this after a couple of breaking releases (added in 0.10.0)
fn panic_missing_name() -> String {
panic!(
r#"
Failed to parse `Nargo.toml`.
`Nargo.toml` now requires a "name" field for Noir packages.
```toml
[package]
name = "package_name"
```
Modify your `Nargo.toml` similarly to above and rerun the command.
"#
)
}

#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
/// Enum representing the different types of ways to
Expand All @@ -91,6 +112,7 @@ fn parse_standard_toml() {
let src = r#"
[package]
name = "test"
authors = ["kev", "foo"]
compiler_version = "0.1"
Expand Down
22 changes: 12 additions & 10 deletions crates/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ use super::fs::{create_named_dir, write_to_file};
use super::{NargoConfig, CARGO_PKG_VERSION};
use acvm::Backend;
use clap::Args;
use const_format::formatcp;
use std::path::PathBuf;

/// Create a Noir project in the current directory.
#[derive(Debug, Clone, Args)]
pub(crate) struct InitCommand;

const SETTINGS: &str = formatcp!(
r#"[package]
authors = [""]
compiler_version = "{CARGO_PKG_VERSION}"
[dependencies]"#,
);

const EXAMPLE: &str = r#"fn main(x : Field, y : pub Field) {
assert(x != y);
}
Expand All @@ -47,10 +38,21 @@ pub(crate) fn run<B: Backend>(

/// Initializes a new Noir project in `package_dir`.
pub(crate) fn initialize_project(package_dir: PathBuf) {
// TODO: Should this reject if we have non-Unicode filepaths?
let package_name = package_dir.file_name().expect("Expected a filename").to_string_lossy();
let src_dir = package_dir.join(SRC_DIR);
create_named_dir(&src_dir, "src");

write_to_file(SETTINGS.as_bytes(), &package_dir.join(PKG_FILE));
let toml_contents = format!(
r#"[package]
name = "{package_name}"
authors = [""]
compiler_version = "{CARGO_PKG_VERSION}"
[dependencies]"#
);

write_to_file(toml_contents.as_bytes(), &package_dir.join(PKG_FILE));
write_to_file(EXAMPLE.as_bytes(), &src_dir.join("main.nr"));
println!("Project successfully created! Binary located at {}", package_dir.display());
}
25 changes: 9 additions & 16 deletions crates/nargo_cli/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ fn resolve_package_manifest(
Ok(())
}

fn crate_name(name: Option<CrateName>) -> String {
name.map(|name| name.into()).unwrap_or_else(|| "[unnamed]".to_string())
}

fn resolve_workspace_manifest(
context: &mut Context,
mut local_package: Option<String>,
Expand All @@ -187,20 +183,18 @@ fn resolve_workspace_manifest(

match member_manifest {
Manifest::Package(inner) => {
let name = inner
let name: CrateName = inner
.package
.name
.map(|name| {
name.parse().map_err(|_name| DependencyResolutionError::InvalidPackageName)
})
.transpose()?;
.parse()
.map_err(|_name| DependencyResolutionError::InvalidPackageName)?;

if packages.insert(name.clone(), member_path).is_some() {
return Err(DependencyResolutionError::PackageCollision(crate_name(name)));
return Err(DependencyResolutionError::PackageCollision(name.into()));
}

if local_package.is_none() && workspace.default_member.as_ref() == Some(member) {
local_package = name.map(Into::into);
local_package = Some(name.into());
}
}
Manifest::Workspace(_) => {
Expand All @@ -213,16 +207,15 @@ fn resolve_workspace_manifest(
}

let local_package = match local_package {
Some(local_package) => local_package
.parse::<CrateName>()
.map_err(|_| DependencyResolutionError::InvalidPackageName)?
.into(),
Some(local_package) => {
local_package.parse().map_err(|_| DependencyResolutionError::InvalidPackageName)?
}
None => packages.keys().last().expect("non-empty packages").clone(),
};

let local_crate = packages
.remove(&local_package)
.ok_or_else(|| DependencyResolutionError::PackageNotFound(crate_name(local_package)))?;
.ok_or_else(|| DependencyResolutionError::PackageNotFound(local_package.into()))?;

let (entry_path, _crate_type) = super::lib_or_bin(local_crate)?;
let crate_id = create_local_crate(context, &entry_path, CrateType::Workspace);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
name = "fail_basic"
authors = [""]
compiler_version = "0.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
name = "fail_dup_func"
authors = [""]
compiler_version = "0.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
name = "pass_basic"
authors = [""]
compiler_version = "0.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
name = "pass_import"
authors = [""]
compiler_version = "0.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "pass_dev_mode_unused"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "1327_concrete_in_generic"
authors = [""]
compiler_version = "0.6.0"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "1_mul"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "2_div"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "3_add"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "4_sub"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "5_over"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
3 changes: 2 additions & 1 deletion crates/nargo_cli/tests/test_data_ssa_refactor/6/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "6"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "6_array"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
3 changes: 2 additions & 1 deletion crates/nargo_cli/tests/test_data_ssa_refactor/7/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "7"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "7_function"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "8_integration"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "9_conditional"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "arithmetic_binary_operations"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "array_dynamic"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "array_len"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "array_neq"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "array_sort"
authors = [""]
compiler_version = "0.6.0"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "assert"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "assert_statement"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "assign_ex"
authors = [""]
compiler_version = "0.1"

[package]
authors = [""]
compiler_version = "0.1"

[dependencies]

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "bit_and"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "bit_shifts_comptime"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "blackbox_func_simple_call"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Loading

0 comments on commit bb28223

Please sign in to comment.