Skip to content

Commit

Permalink
chore!: Require package names in Nargo.toml files
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jul 26, 2023
1 parent be44c7b commit ef6792b
Show file tree
Hide file tree
Showing 138 changed files with 313 additions and 213 deletions.
3 changes: 2 additions & 1 deletion crates/nargo/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct WorkspaceConfig {
#[allow(dead_code)]
#[derive(Default, Debug, Deserialize, Clone)]
pub struct PackageMetadata {
pub name: Option<String>,
pub name: String,
// Note: a package name is not needed unless there is a registry
authors: Vec<String>,
// If not compiler version is supplied, the latest is used
Expand All @@ -91,6 +91,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());
}
23 changes: 6 additions & 17 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.as_string()).unwrap_or_else(|| "[unnamed]".to_string())
}

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

match member_manifest {
Manifest::Package(inner) => {
let name = inner
.package
.name
.map(|name| {
CrateName::new(&name)
.map_err(|_name| DependencyResolutionError::InvalidPackageName)
})
.transpose()?;
let name = CrateName::new(&inner.package.name)
.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.as_string()));
}

if local_package.is_none() && workspace.default_member.as_ref() == Some(member) {
local_package = name.as_ref().map(CrateName::as_string);
local_package = Some(name.as_string());
}
}
Manifest::Workspace(_) => {
Expand All @@ -215,14 +205,13 @@ fn resolve_workspace_manifest(

let local_package = match local_package {
Some(local_package) => CrateName::new(&local_package)
.map_err(|_| DependencyResolutionError::InvalidPackageName)?
.into(),
.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.as_string()))?;

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]
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "bool_not"
authors = [""]
compiler_version = "0.1"

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

[dependencies]

[dependencies]
11 changes: 5 additions & 6 deletions crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "bool_or"
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 = "brillig_acir_as_brillig"
authors = [""]
compiler_version = "0.1"

[dependencies]
[dependencies]
Loading

0 comments on commit ef6792b

Please sign in to comment.