{"record":{"id":"4e9638030cc7f628","repo":"parcel-bundler/parcel","slug":"dynamic-diagnostic-from-svg-transform-errors","errorCode":null,"errorMessage":"[dynamic diagnostic from SVG transform errors]","messagePattern":"\\[dynamic diagnostic from SVG transform errors\\]","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/transformers/svg/src/SVGTransformer.js","lineNumber":25,"sourceCode":"  envToRust,\n  dependencyFromRust,\n  assetFromRust,\n} from '@parcel/rust';\n\nexport default (new Transformer({\n  async transform({asset}) {\n    asset.bundleBehavior = 'isolated';\n\n    let res = transformSvg({\n      code: await asset.getBuffer(),\n      filePath: asset.filePath,\n      xml: true,\n      env: envToRust(asset.env),\n      hmr: false,\n    });\n\n    if (res.errors.length) {\n      throw new ThrowableDiagnostic({\n        diagnostic: res.errors,\n      });\n    }\n\n    asset.setBuffer(res.code);\n\n    let assets = [asset];\n    for (let dep of res.dependencies) {\n      asset.addDependency(dependencyFromRust(dep));\n    }\n\n    for (let a of res.assets) {\n      assets.push(assetFromRust(a));\n    }\n\n    return assets;\n  },\n}): Transformer);","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/transformers/svg/src/SVGTransformer.js#L7-L43","documentation":"Thrown by @parcel/transformer-svg when the underlying Rust transformSvg pass (via @parcel/rust) returns a non-empty errors array. The Rust core parses the SVG as XML and resolves references (e.g. <image href>, <use href>, <script xlink:href>); any parse failure or unresolvable reference is surfaced verbatim as a ThrowableDiagnostic. Because the message is dynamic, the actual text comes from the Rust diagnostics and may mention malformed XML, invalid attributes, or missing referenced files.","triggerScenarios":"An SVG asset is transformed (asset.getBuffer() fed to transformSvg with xml:true) and res.errors has length > 0. This happens on non-well-formed XML, undeclared namespaces, or when an href/xlink:href inside the SVG points at a path Parcel cannot resolve.","commonSituations":"Hand-edited SVG with a missing closing tag, a <use href=\"#id\"> referencing an undefined fragment, an <image href=\"./missing.png\">, copy-pasted SVG carrying an unsupported namespace, or a file accidentally saved as SVGZ/binary.","solutions":["Open the SVG in a validator or editor with XML linting and fix the exact line/column cited in the surfaced diagnostic.","Check every href/xlink:href in the file and confirm the referenced file exists relative to the SVG.","If the error names a namespace, declare it on the root <svg> element or strip the offending attribute.","Re-export the SVG from its design tool to regenerate clean XML."],"exampleFix":"// before\n<svg><use href=\"#missing\"/></svg>\n// after\n<svg xmlns=\"http://www.w3.org/2000/svg\"><defs><g id=\"missing\"/></defs><use href=\"#missing\"/></svg>","handlingStrategy":"validation","validationCode":"const buf = await asset.getBuffer();\nconst text = buf.toString('utf8');\n// cheap well-formedness pre-check: balanced root tag and parseable by DOMParser\nconst doc = new DOMParser().parseFromString(text, 'image/svg+xml');\nif (doc.querySelector('parsererror')) {\n  throw new Error('SVG is not well-formed XML; refusing to transform');\n}\n// also verify every href resolves\nfor (const el of doc.querySelectorAll('[href],[xlink\\\\:href]')) {\n  const ref = el.getAttribute('href') || el.getAttributeNS('http://www.w3.org/1999/xlink', 'href');\n  if (ref && !ref.startsWith('#') && !(await fs.exists(path.join(assetDir, ref)))) {\n    throw new Error('Missing SVG reference: ' + ref);\n  }\n}","typeGuard":"function isWellFormedSvg(text) {\n  const doc = new DOMParser().parseFromString(text, 'image/svg+xml');\n  return !doc.querySelector('parsererror') && doc.documentElement.nodeName === 'svg';\n}","tryCatchPattern":"try {\n  await transformer.transform({asset});\n} catch (e) {\n  if (e?.diagnostics?.some(d => /svg|xml/i.test(d.message))) {\n    console.warn('SVG transform failed; check XML well-formedness and href references');\n  }\n  throw e;\n}","preventionTips":["Lint SVGs with an XML validator in CI before they reach Parcel.","Generate SVGs from design tools rather than hand-editing.","Keep href references relative and committed alongside the SVG."],"tags":["svg","xml","parcel-transformer","rust-binding"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}