gleam-lang/gleam · error

failed regex to match valid name format

Error message

failed regex to match valid name format

What it means

In validate_name, the lowercase-name regex ^[a-z][a-z0-9_]*$ is compiled with .expect("failed regex to match valid name format"). The pattern is a compile-time constant and always valid, so this panic is an internal invariant violation and can only fire if the regex crate itself rejects a known-good pattern (e.g. regex crate regression or size/feature issue).

Solutions

  1. Rebuild gleam / update to the latest release — the panic indicates a build or dependency issue, not your project name
  2. Check the regex crate version in Cargo.lock and update it if a known regression exists
  3. If you see this on a custom target, ensure adequate stack size for regex compilation
  4. File a Gleam issue with your platform details
Defensive patterns

Strategy: validation

Validate before calling

fn looks_like_valid_gleam_name(name: &str) -> bool {
    !name.is_empty()
        && name.chars().next().map_or(false, |c| c.is_ascii_lowercase())
        && name.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
}

Prevention

When it happens

Trigger: Calling validate_name (via suggest_valid_name or get_valid_project_name, e.g. `gleam new`) in a build where the regex engine cannot compile the fixed pattern — effectively never in a correct build.

Common situations: A broken or unusual regex crate build (feature flags, stack size limits in exotic targets), or tampered binaries; not a user-input problem — invalid names return Err(Error::InvalidProjectName) instead.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/f7eec6147f06d0aa. Report an issue: GitHub.

Appendix: source

Thrown at compiler-cli/src/new.rs:364

            reason: InvalidProjectNameReason::ErlangReservedWord,
        })
    } else if erlang::is_erlang_standard_library_module(name) {
        Err(Error::InvalidProjectName {
            name: name.to_string(),
            reason: InvalidProjectNameReason::ErlangStandardLibraryModule,
        })
    } else if parse::lexer::string_to_keyword(name).is_some() {
        Err(Error::InvalidProjectName {
            name: name.to_string(),
            reason: InvalidProjectNameReason::GleamReservedWord,
        })
    } else if name == "gleam" {
        Err(Error::InvalidProjectName {
            name: name.to_string(),
            reason: InvalidProjectNameReason::GleamReservedModule,
        })
    } else if regex::Regex::new("^[a-z][a-z0-9_]*$")
        .expect("failed regex to match valid name format")
        .is_match(name)
    {
        Ok(())
    } else if regex::Regex::new("^[a-zA-Z][a-zA-Z0-9_]*$")
        .expect("failed regex to match valid but non-lowercase name format")
        .is_match(name)
    {
        Err(Error::InvalidProjectName {
            name: name.to_string(),
            reason: InvalidProjectNameReason::FormatNotLowercase,
        })
    } else {
        Err(Error::InvalidProjectName {
            name: name.to_string(),
            reason: InvalidProjectNameReason::Format,
        })
    }
}

View on GitHub (pinned to 15b07c7830)