gleam-lang/gleam · error

failed regex to match consecutive underscores

Error message

failed regex to match consecutive underscores

What it means

Second suggestion step in suggest_valid_name: regex _+ collapses consecutive underscores after character replacement, compiled with .expect("failed regex to match consecutive underscores"). Same family as 84-86: the constant pattern is always valid, so the panic is an internal invariant violation only reachable if the regex engine fails.

Solutions

  1. Rebuild or update gleam
  2. Update the regex dependency if needed
  3. File an issue with reproduce steps and platform info
Defensive patterns

Strategy: validation

Validate before calling

fn collapse_underscores(s: &str) -> String {
    let mut out = String::new();
    let mut prev_us = false;
    for c in s.chars() {
        if c == '_' { if !prev_us { out.push(c); } prev_us = true; }
        else { out.push(c); prev_us = false; }
    }
    out
}

Prevention

When it happens

Trigger: `gleam new` suggestion path for a Format-invalid name, in a build where regex compilation of the fixed pattern fails — normally unreachable.

Common situations: Dependency/build issues with the regex crate, exotic targets, corrupted binaries.

Related errors


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

Appendix: source

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

            Some(format!("{invalid_name}_app"))
        }
        InvalidProjectNameReason::GleamReservedWord => Some(format!("{invalid_name}_app")),
        InvalidProjectNameReason::GleamReservedModule => {
            if invalid_name == "gleam" {
                Some("app_gleam".into())
            } else {
                Some(format!("{invalid_name}_app"))
            }
        }
        InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()),
        InvalidProjectNameReason::Format => {
            let suggestion = regex::Regex::new(r"[^a-z0-9]")
                .expect("failed regex to match any non-lowercase and non-alphanumeric characters")
                .replace_all(&invalid_name.to_lowercase(), "_")
                .to_string();

            let suggestion = regex::Regex::new(r"_+")
                .expect("failed regex to match consecutive underscores")
                .replace_all(&suggestion, "_")
                .to_string();

            match validate_name(&suggestion) {
                Ok(_) => Some(suggestion),
                Err(_) => None,
            }
        }
    }
}

fn get_valid_project_name(
    provided_name: Option<&str>,
    project_root: &str,
    confirm: impl Fn(&str) -> Result<bool, Error>,
) -> Result<ProjectName, Error> {
    let initial_name = match provided_name {
        Some(name) => name.trim().to_string(),

View on GitHub (pinned to 15b07c7830)