emscripten-core / emscripten-core/emscripten

"SyntaxError: Unexpected token < in JSON at position 0" on source map load failure

Open
#13,136 4 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When the code is built with `-g4` but the resulting .map file is unavailable at the page load time, the code fails with a bit mysterious error:
```
SyntaxError: Unexpected token < in JSON at position 0
printErr @ a.out.html:1246
abort @ a.out.js:1273
getSourceMap @ a.out.js:1444
(anonymous) @ a.out.js:1455
Promise.catch (async)
getSourceMapPromise @ a.out.js:1454
createWasm @ a.out.js:1668
(anonymous) @ a.out.js:7682
```

The stack trace points to the `getSourceMap()` function (https://github.com/emscripten-core/emscripten/blob/9bb322f8a7ee89d6ac67e828b9c7a7022ddf8de2/src/source_map_support.js#L105). Neither this function nor the `read_()` function (https://github.com/emscripten-core/emscripten/blob/9bb322f8a7ee89d6ac67e828b9c7a7022ddf8de2/src/web_or_worker_shell_read.js#L7) it calls have the error handling code. So when the source map loading fails with a 404 error and some non-empty HTML body, this code tries to parse this 404 error page as a JSON dump and therefore fails with this `SyntaxError`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.