Skip to content

bugfix: Ensure proper handling of empty metafile buffers#4418

Closed
Garefild wants to merge 1 commit intoevanw:mainfrom
Garefild:bugfix/common-parsejson-zero-length
Closed

bugfix: Ensure proper handling of empty metafile buffers#4418
Garefild wants to merge 1 commit intoevanw:mainfrom
Garefild:bugfix/common-parsejson-zero-length

Conversation

@Garefild
Copy link
Copy Markdown

@Garefild Garefild commented Mar 15, 2026

Fix: Handle empty Uint8Array in parseJSON()

Backport → 0.27.4

Problem

When an optional JSON field was sent as an empty buffer, parseJSON() would decode the Uint8Array(0) into an empty string and pass it straight to JSON.parse("") — which unconditionally throws:

SyntaxError: Unexpected end of JSON input

Solution

parseJSON() now short-circuits on empty input, returning undefined before any decoding or parsing occurs. As a related change, parseJSON() is no longer called for an empty metafile buffer — result.metafile stays undefined when no metafile payload is present.

function parseJSON(bytes: Uint8Array): any {
  if (bytes.length === 0) return void 0; // absent, not malformed
  // ...
}

Fixes #4420.

@adamwong246
Copy link
Copy Markdown

downgrading to 0.27.3 worked for me

@Garefild
Copy link
Copy Markdown
Author

Garefild commented Mar 28, 2026

downgrading to 0.27.3 worked for me

Yes, that does work, but I prefer to stay in sync with the latest releases.

From what I can tell, the issue is caused by the js api: fall back to js-based metafile json parser

The fix itself should be relatively straightforward. I’ve submitted a patch and am currently waiting for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

esbuild 0.27.4 crashes when metadata is enabled

2 participants