Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Require package names to be non-empty #2293

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::errors::CliError;

use super::fs::{create_named_dir, write_to_file};
use super::{NargoConfig, CARGO_PKG_VERSION};
use super::{crate_name_parser, NargoConfig, CARGO_PKG_VERSION};
use acvm::Backend;
use clap::Args;
use nargo::constants::{PKG_FILE, SRC_DIR};
Expand All @@ -12,7 +12,7 @@ use std::path::PathBuf;
#[derive(Debug, Clone, Args)]
pub(crate) struct InitCommand {
/// Name of the package [default: current directory name]
#[clap(long)]
#[clap(long, value_parser = crate_name_parser)]
name: Option<String>,

/// Use a library template
Expand Down
10 changes: 10 additions & 0 deletions crates/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ pub(crate) fn start_cli() -> eyre::Result<()> {

Ok(())
}

fn crate_name_parser(name: &str) -> Result<String, String> {
use noirc_frontend::graph::CrateName;

if CrateName::is_valid_name(name) {
Ok(name.into())
} else {
Err("Package names must be non-empty and cannot contain hyphens".into())
}
}
4 changes: 2 additions & 2 deletions crates/nargo_cli/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::errors::CliError;

use super::{init_cmd::initialize_project, NargoConfig};
use super::{crate_name_parser, init_cmd::initialize_project, NargoConfig};
use acvm::Backend;
use clap::Args;
use nargo::package::PackageType;
Expand All @@ -13,7 +13,7 @@ pub(crate) struct NewCommand {
path: PathBuf,

/// Name of the package [default: package directory name]
#[clap(long)]
#[clap(long, value_parser = crate_name_parser)]
name: Option<String>,

/// Use a library template
Expand Down
31 changes: 27 additions & 4 deletions crates/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ impl CrateId {
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct CrateName(SmolStr);

impl CrateName {
pub fn is_valid_name(name: &str) -> bool {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
!name.is_empty() && name.chars().all(|n| !CHARACTER_BLACK_LIST.contains(&n))
}
}

impl Display for CrateName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand All @@ -54,11 +60,28 @@ impl FromStr for CrateName {
type Err = String;

fn from_str(name: &str) -> Result<Self, Self::Err> {
let is_invalid = name.chars().any(|n| CHARACTER_BLACK_LIST.contains(&n));
if is_invalid {
Err(name.into())
} else {
if Self::is_valid_name(name) {
Ok(Self(SmolStr::new(name)))
} else {
Err(name.into())
}
}
}

#[cfg(test)]
mod crate_name {
use super::{CrateName, CHARACTER_BLACK_LIST};

#[test]
fn it_rejects_empty_string() {
assert!(!CrateName::is_valid_name(""));
}

#[test]
fn it_rejects_blacklisted_chars() {
for bad_char in CHARACTER_BLACK_LIST {
let bad_char_string = bad_char.to_string();
assert!(!CrateName::is_valid_name(&bad_char_string));
}
}
}
Expand Down