AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack Build Fails On Valid Width-Only SVG With ViewBox Imported Via Next/Image

Turbopack rejects SVG images that have a width attribute and a viewBox but no height, causing dev and build failures, while webpack handles the same SVG correctly by deriving height from the viewBox aspect ratio.

highConfidence 95%Next.jsAffected V16.4.0-Canary.37

Origin Analysis

The Rust SVG dimension parser in turbopack/crates/turbopack-image/src/process/svg.rs does not implement the aspect-ratio derivation logic from the JS image-size library. When width is present but height is missing, it throws an error instead of using the viewBox to compute the missing dimension.
1. Create an SVG file (e.g., public/logo.svg) with a width attribute and a viewBox, but no height attribute. 2. Import it using next/image in a page: import logo from '../public/logo.svg'; <Image src={logo} alt="logo" />. 3. Run `npm run dev` or `npm run build` with Turbopack enabled. 4. Observe the error: "Processing image failed / Failed to parse svg source code for image dimensions / SVG source code contains only a width attribute but not height attribute". 5. Run `npm run build -- --webpack` to confirm the same SVG works with webpack.

Fixing Code Block

Edge Case Audit

The derived dimensions may produce floating-point values, potentially leading to precision issues if downstream code expects integers. Division by zero is possible if the viewBox width or height is zero; the code adds guards for this. The change may affect image caching or rendering if rounding differs from webpack. Rollback is straightforward: revert this match block to the original version. Consider adding unit tests for width-only, height-only, and both-missing SVGs with valid and invalid viewBox values before merging.

Ecosystem Topology