{"id":"7db677aa341e4af4","repo":"webpack/webpack","slug":"unknown-type-globaltype-valtype","errorCode":null,"errorMessage":"unknown type: ${globalType.valtype}","messagePattern":"unknown type: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/wasm-sync/WebAssemblyGenerator.js","lineNumber":163,"sourceCode":"\n/**\n * Creates an init instruction for a global type\n * @param {t.GlobalType} globalType the global type\n * @returns {t.Instruction} init expression\n */\nconst createDefaultInitForGlobal = (globalType) => {\n\tif (globalType.valtype[0] === \"i\") {\n\t\t// create NumberLiteral global initializer\n\t\treturn t.objectInstruction(\"const\", globalType.valtype, [\n\t\t\tt.numberLiteralFromRaw(66)\n\t\t]);\n\t} else if (globalType.valtype[0] === \"f\") {\n\t\t// create FloatLiteral global initializer\n\t\treturn t.objectInstruction(\"const\", globalType.valtype, [\n\t\t\tt.floatLiteral(66, false, false, \"66\")\n\t\t]);\n\t}\n\tthrow new Error(`unknown type: ${globalType.valtype}`);\n};\n\n/**\n * Rewrite the import globals:\n * - removes the ModuleImport instruction\n * - injects at the same offset a mutable global of the same type\n *\n * Since the imported globals are before the other global declarations, our\n * indices will be preserved.\n *\n * Note that globals will become mutable.\n * @param {object} state transformation state\n * @param {AST} state.ast Module's ast\n * @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function\n * @returns {ArrayBufferTransform} transform\n */\nconst rewriteImportedGlobals = (state) => (bin) => {\n\tconst additionalInitCode = state.additionalInitCode;","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/webpack/webpack/blob/318421ea8ac81371f5171236a6efb63675576528/lib/wasm-sync/WebAssemblyGenerator.js#L145-L181","documentation":"The sync WebAssembly generator rewrites every imported global into a mutable local with a default zero-initializer. createDefaultInitForGlobal only knows how to synthesize that initializer for numeric value types whose valtype starts with 'i' (i32/i64) or 'f' (f32/f64). A global whose valtype is a reference type (externref/funcref/anyref) or a SIMD vector (v128) has no numeric default, so the rewrite aborts. This only fires under experiments.syncWebAssembly; the async path does not rewrite globals this way.","triggerScenarios":"Bundling a .wasm module that imports a global of a non-numeric type (v128, externref, funcref) while experiments.syncWebAssembly is enabled. The generator's rewriteImportedGlobals transform calls createDefaultInitForGlobal for each imported global during WebAssemblyGenerator.generate.","commonSituations":"Using the deprecated experiments.syncWebAssembly experiment with a modern wasm module that uses reference-types or SIMD globals; upgrading a Rust/AssemblyScript toolchain that started emitting reference-type globals; switching a build from asyncWebAssembly to syncWebAssembly and hitting a module the async path tolerated.","solutions":["Switch experiments.syncWebAssembly to experiments.asyncWebAssembly; the async generator does not rewrite imported globals and accepts reference/SIMD types.","Rebuild the wasm module without the offending imported global (disable reference-types/SIMD in the toolchain, e.g. rustc -C target-feature=-reference-types,-simd128, or define the global inside the module instead of importing it).","Filter the offending .wasm out of the sync-wasm pipeline (different module.rules test) and load it through a side channel that does not require global rewriting."],"exampleFix":"// before\nmodule.exports = {\n  experiments: { syncWebAssembly: true }\n};\n\n// after\nmodule.exports = {\n  experiments: { asyncWebAssembly: true }\n};","handlingStrategy":"validation","validationCode":"// Pre-build: decode the wasm and reject modules whose imported globals\n// use a non-numeric valtype before webpack's sync generator trips on them.\nconst { decode } = require('@webassemblyjs/wasm-parser');\nconst t = require('@webassemblyjs/ast');\n\nfunction assertNoUnsupportedImportedGlobals(wasmBuffer) {\n  const ast = decode(wasmBuffer, { ignoreCodeSection: true, ignoreDataSection: true });\n  t.traverse(ast, {\n    ModuleImport({ node }) {\n      if (t.isGlobalType(node.descr)) {\n        const v = node.descr.valtype;\n        if (v[0] !== 'i' && v[0] !== 'f') {\n          throw new Error(`Unsupported imported global type ${v} in ${node.module}:${node.name}; rebuild without reference-types/SIMD globals or use asyncWebAssembly.`);\n        }\n      }\n    }\n  });\n}\n// assertNoUnsupportedImportedGlobals(fs.readFileSync('./module.wasm'));","typeGuard":"/** @param {string} valtype wasm global value type\n * @returns {boolean} true when createDefaultInitForGlobal can handle it */\nfunction isNumericValtype(valtype) {\n  return valtype[0] === 'i' || valtype[0] === 'f';\n}","tryCatchPattern":null,"preventionTips":["Prefer experiments.asyncWebAssembly over the deprecated syncWebAssembly experiment; the async path does not rewrite imported globals.","If you control the wasm toolchain, disable reference-types and simd128 when targeting the sync pipeline.","Pin your @webassemblyjs/* versions to the ones webpack depends on so decode() and the generator agree on the valtype alphabet."],"tags":["wasm","sync-webassembly","configuration","experiments"],"analyzedSha":"318421ea8ac81371f5171236a6efb63675576528","analyzedAt":"2026-08-03T19:39:56.731Z","schemaVersion":2}