gleam-lang/gleam · info

JavaScript generator could not identify imported module name

Error message

JavaScript generator could not identify imported module name.

What it means

When emitting TypeScript reference comments, the JavaScript generator derives the output filename from the module name by taking the last '/'-separated segment: `module.name.as_str().split('/').next_back().expect("JavaScript generator could not identify imported module name.")`. Rust's str::split always yields at least one item (even for the empty string), so next_back() returning None is structurally unreachable — this expect is defensive dead code guarding against an empty/non-slash module name ever reaching codegen.

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 7e623aa83d)

Solutions

  1. No user-side action applies; if seen, capture the full backtrace and module set and report at github.com/gleam-lang/gleam.
  2. Embedders constructing modules programmatically should validate `!module.name.as_str().is_empty()` before invoking codegen.
  3. Maintainers: `.next_back().unwrap_or("")` or an unreachable!() with a clearer message would document reality.
Defensive patterns

Strategy: try-catch

Validate before calling

// Embedders driving the JS generator directly: sanity-check module names first
fn valid_module_name(name: &str) -> bool {
    !name.trim().is_empty() && !name.starts_with('/') && !name.ends_with('/')
}
assert!(valid_module_name(module.name.as_str())); // before invoking codegen

Try / catch

// Because the branch is unreachable, guarding means containing ICEs:
let docs = std::panic::catch_unwind(AssertUnwindSafe(|| {
    javascript::module(&mut output, &module, &config)
}));
if docs.is_err() { /* log backtrace, report upstream, skip module */ }

Prevention

When it happens

Trigger: Not reachable through the public API: any module name — including "" — produces at least one split piece. It would require a corrupted Module name (e.g. a custom construction of the internal AST) fed directly into the JS generator by embedding code.

Common situations: Practically never seen; a crash log containing it would point to an internal compiler error upstream that produced a degenerate module name, or to misuse of compiler-core internals by external tooling.

Related errors


AI-assisted analysis of gleam-lang/gleam@7e623aa83d (2026-08-17). Data as JSON: /api/errors/20fa687a2d54ad3a. Report an issue: GitHub.