gleam-lang/gleam · error

Index out of bounds

Error message

Index out of bounds

What it means

A panic in error.rs inside the edit-distance (Levenshtein-style) matrix computation used to suggest 'did you mean' alternatives (e.g. for `gleam add` package names): the loop indexes rows of the DP matrix beyond its bounds. The Error type's command variant / command dispatch reached this helper with indices larger than the matrix, indicating a mismatch between the compared string lengths and the allocated matrix.

Solutions

  1. Retry with a correctly spelled package name; the panic occurs while trying to suggest alternatives for a bad name.
  2. Update gleam to the latest version where the distance-matrix bug may be fixed.
  3. File an issue with the exact command and argument at github.com/gleam-lang/gleam.

Example fix

// before (CLI)
gleam add gleam_otp  // with a misspelled/edge-case name causing the panic
// after
gleam add otp  // or check spelling; upgrade gleam if the panic persists
Defensive patterns

Strategy: retry

Validate before calling

// Validate the package name before invoking gleam add
fn valid_pkg_name(name: &str) -> bool {
    !name.is_empty() && name.len() < 64 && name.chars().all(|c| c.is_ascii_lowercase() || c == '_')
}

Type guard

fn is_usable_name(s: &str) -> bool { !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') }

Try / catch

let status = Command::new("gleam").args(["add", name]).status()?;
if !status.success() {
    eprintln!("gleam add failed; verify the package name on hex.pm");
}

Prevention

When it happens

Trigger: Running a gleam CLI command (e.g. `gleam add <name>`, which builds Error values at compiler-cli/src/add.rs) whose name-matching path passes strings whose lengths exceed the preallocated distance matrix dimensions.

Common situations: Typing a package name into `gleam add`/`gleam remove` that triggers the suggestion engine; very long names or empty strings can expose an off-by-one in the distance calculation.

Related errors


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

Appendix: source

Thrown at compiler-core/src/error.rs:870

        if let Some(element) = current.get_mut(0) {
            *element = i;
        }
        let a_idx = i - 1;

        // column by column
        for j in 1..=b.len() {
            let b_idx = j - 1;

            // There is no cost to substitute a character with itself.
            let substitution_cost = match (a.get(a_idx), b.get(b_idx)) {
                (Some(&a_char), Some(&b_char)) => {
                    if a_char == b_char {
                        0
                    } else {
                        1
                    }
                }
                _ => panic!("Index out of bounds"),
            };

            let insertion = current.get(j - 1).map_or(usize::MAX, |&x| x + 1);

            if let Some(value) = current.get_mut(j) {
                *value = std::cmp::min(
                    // deletion
                    prev.get(j).map_or(usize::MAX, |&x| x + 1),
                    std::cmp::min(
                        // insertion
                        insertion,
                        // substitution
                        prev.get(j - 1)
                            .map_or(usize::MAX, |&x| x + substitution_cost),
                    ),
                );
            }

View on GitHub (pinned to 49f8762da5)