{"record":{"id":"5b1676ffec366e79","repo":"swc-project/swc","slug":"id-should-not-be-wrapped-with-a-function","errorCode":null,"errorMessage":"{id:?} should not be wrapped with a function","messagePattern":"(.+?) should not be wrapped with a function","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_bundler/src/bundler/chunk/computed_key.rs","lineNumber":47,"sourceCode":"    ///\n    /// ```ts\n    /// const _mod = (function(){\n    ///     const arr = [1, 2, 3];\n    ///     return {\n    ///         arr,\n    ///     };\n    /// })();\n    /// ```\n    pub(super) fn wrap_esm(\n        &self,\n        ctx: &Ctx,\n        id: ModuleId,\n        module: Modules,\n    ) -> Result<Modules, Error> {\n        let span = DUMMY_SP;\n        let module_var_name = match self.scope.wrapped_esm_id(id) {\n            Some(v) => v,\n            None => bail!(\"{id:?} should not be wrapped with a function\"),\n        };\n\n        let is_async = module.iter().any(|m| contains_top_level_await(m.1));\n\n        let mut additional_items = Vec::new();\n\n        module.iter().for_each(|(module_id, item)| {\n            match item {\n                // Handle `export *`-s from dependency modules.\n                //\n                // See: https://github.com/denoland/deno/issues/9200\n                ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {\n                    ref specifiers,\n                    with: Some(with),\n                    ..\n                })) if is_injected(with) => {\n                    for s in specifiers {\n                        if let ExportSpecifier::Named(ExportNamedSpecifier {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_bundler/src/bundler/chunk/computed_key.rs#L29-L65","documentation":"Internal invariant in swc_bundler: `wrap_esm` (which wraps a module's exports in an IIFE for computed-key / `export *` handling) was called for a ModuleId that has no registered wrapped-ESM identifier in the scope (`scope.wrapped_esm_id(id)` returned None). Modules are marked for wrapping earlier in graph analysis, so this signals the module graph and scope metadata are out of sync - a bundler bug or corrupted state, not a user configuration problem.","triggerScenarios":"Bundling via swc_bundler with ESM graphs that trigger wrapping (computed export keys, `export *` re-export chains, cycles) where scope registration for the module was skipped - typically a version-specific regression.","commonSituations":"Rare in practice; surfaces in deno-style bundling or tools embedding swc_bundler after upgrading swc_core versions.","solutions":["Upgrade swc/swc_core to a release where the bundler's scope registration is fixed","Reduce the bundle input: remove `export *` chains or computed export keys to dodge the wrapping path","If reproducible on latest, file an swc issue with the entry graph - it is an internal invariant violation"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Rust - catch the invariant and surface a reportable error\nmatch bundler.bundle(entries) {\n    Ok(v) => { /* ... */ }\n    Err(e) => {\n        let msg = format!(\"{e:#}\");\n        if msg.contains(\"should not be wrapped with a function\") {\n            // swc_bundler internal invariant: report module graph, try fewer `export *`\n            return Err(anyhow::anyhow!(\"swc_bundler invariant hit; simplify re-exports and update swc_core: {msg}\"));\n        }\n        return Err(e.into());\n    }\n}","preventionTips":["Pin swc_core versions validated against your entry graphs","Minimize `export *` chains and computed export keys in bundled sources","Keep a minimal reproducer bundle for regression testing bundler upgrades"],"tags":["swc","bundler","internal-invariant","module-graph"],"backgroundTag":"bundler-internal-invariant","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}