{"record":{"id":"20fa687a2d54ad3a","repo":"gleam-lang/gleam","slug":"javascript-generator-could-not-identify-imported-m","errorCode":null,"errorMessage":"JavaScript generator could not identify imported module name.","messagePattern":"JavaScript generator could not identify imported module name\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"compiler-core/src/javascript.rs","lineNumber":166,"sourceCode":"                None\n            },\n            stdlib_package,\n        }\n    }\n\n    fn type_reference(&self, arena: &'doc DocumentArena<'a, 'doc>) -> Document<'a, 'doc> {\n        if self.typescript == TypeScriptDeclarations::None {\n            return EMPTY_DOCUMENT;\n        }\n\n        // Get the name of the module relative the directory (similar to basename)\n        let module = self\n            .module\n            .name\n            .as_str()\n            .split('/')\n            .next_back()\n            .expect(\"JavaScript generator could not identify imported module name.\");\n\n        docvec![\n            arena,\n            REFERENCE_TYPES_DOCUMENT,\n            module,\n            DOT_D_DOT_MTS_CLOSE_QUOTE_CLOSE_TAG_DOCUMENT,\n            LINE_DOCUMENT\n        ]\n    }\n\n    fn sourcemap_reference(&self, arena: &'doc DocumentArena<'a, 'doc>) -> Document<'a, 'doc> {\n        match self.source_map_builder {\n            None => \"\".to_doc(arena),\n            Some(_) => {\n                // Get the name of the module relative the directory (similar to basename)\n                let module = self\n                    .module\n                    .name","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/gleam-lang/gleam/blob/7e623aa83da3776faee50ca4ab9a6c40124acd95/compiler-core/src/javascript.rs#L148-L184","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["No user-side action applies; if seen, capture the full backtrace and module set and report at github.com/gleam-lang/gleam.","Embedders constructing modules programmatically should validate `!module.name.as_str().is_empty()` before invoking codegen.","Maintainers: `.next_back().unwrap_or(\"\")` or an unreachable!() with a clearer message would document reality."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Embedders driving the JS generator directly: sanity-check module names first\nfn valid_module_name(name: &str) -> bool {\n    !name.trim().is_empty() && !name.starts_with('/') && !name.ends_with('/')\n}\nassert!(valid_module_name(module.name.as_str())); // before invoking codegen","typeGuard":null,"tryCatchPattern":"// Because the branch is unreachable, guarding means containing ICEs:\nlet docs = std::panic::catch_unwind(AssertUnwindSafe(|| {\n    javascript::module(&mut output, &module, &config)\n}));\nif docs.is_err() { /* log backtrace, report upstream, skip module */ }","preventionTips":["Only construct Module values through the normal compile pipeline rather than hand-built ASTs.","When embedding compiler-core, wrap codegen in catch_unwind so internal assertions don't kill your process."],"tags":["panic","javascript","codegen","typescript","module-name","unreachable","gleam-core"],"backgroundTag":"unreachable-assertion","analyzedSha":"7e623aa83da3776faee50ca4ab9a6c40124acd95","analyzedAt":"2026-08-17T00:07:02.091Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}