gleam-lang/gleam · error

JavaScript generator could not identify imported module…

Error message

JavaScript generator could not identify imported module name.

What it means

When generating JavaScript type references, the generator derives a module name by taking the last '/'-separated segment of the imported module's name. The expect asserts module names are never empty or slash-terminated; a module whose name yields None from next_back() breaches that assumption.

Solutions

  1. Ensure every module has a non-empty, validated name before compilation.
  2. Add upstream validation rejecting module names that are empty or end with '/'.
  3. Replace expect with a proper compiler error (InternalError) reporting the offending module name.

Example fix

// before
.next_back()
.expect("JavaScript generator could not identify imported module name.")
// after
.next_back()
.filter(|s| !s.is_empty())
.unwrap_or_else(|| panic!("invalid module name: {:?}", self.module.name))
Defensive patterns

Strategy: validation

Validate before calling

fn module_ref_ok(name: &str) -> bool { name.rsplit('/').next().map_or(false, |s| !s.is_empty()) }

Prevention

When it happens

Trigger: Compiling an import whose module `name` is empty or ends such that split('/').next_back() returns None/empty (e.g. name == "" or "a/b/"), then hitting the type_reference path that emits a .d.mts reference.

Common situations: Compiler development or hand-crafted module graphs (tests, codegen tools) with malformed module names; typically not reachable from valid Gleam source where module names are validated identifiers.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at compiler-core/src/javascript.rs:166

                None
            },
            stdlib_package,
        }
    }

    fn type_reference(&self, arena: &'doc DocumentArena<'a, 'doc>) -> Document<'a, 'doc> {
        if self.typescript == TypeScriptDeclarations::None {
            return EMPTY_DOCUMENT;
        }

        // Get the name of the module relative the directory (similar to basename)
        let module = self
            .module
            .name
            .as_str()
            .split('/')
            .next_back()
            .expect("JavaScript generator could not identify imported module name.");

        docvec![
            arena,
            REFERENCE_TYPES_DOCUMENT,
            module,
            DOT_D_DOT_MTS_CLOSE_QUOTE_CLOSE_TAG_DOCUMENT,
            LINE_DOCUMENT
        ]
    }

    fn sourcemap_reference(&self, arena: &'doc DocumentArena<'a, 'doc>) -> Document<'a, 'doc> {
        match self.source_map_builder {
            None => "".to_doc(arena),
            Some(_) => {
                // Get the name of the module relative the directory (similar to basename)
                let module = self
                    .module
                    .name

View on GitHub (pinned to 15b07c7830)