evanw / evanw/node-source-map-support

SyntaxError: Unexpected token z in JSON at position 0

Offen
#254 4 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
2.2k
Forks
223
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I have the following situation:

* I start Karma with `node --require coffeescript/register' since my Karma config is in CoffeeScript
* Karma loads ts-node to support TypeScript modules
* I have a custom Karma preprocessor for compiling CoffeeScript, since karma-coffee-preprocessor doesn't support CoffeeScript 2.
* I configure CoffeeScript to transpile the resulting JS code with Babel
* I use Babel's preset-env
* Babel loads a plugin, plugin-proposal-async-generator-functions, which uses @babel/template which creates a stack trace by throwing and catching an Error (inside a function called `extendedTrace`).
* When this happens, source-map-support's `prepareStackTrace` method is called, since source-map-support is used by ts-node.
* source-map-support encounters coffeescript.js in the call stack (since the Error is thrown inside Babel which is invoked from CoffeeScript).
* coffeescript.js contains code for emitting source maps, so it contains code that matches the regular expression used by `retrieveSourceMapURL`.
* `retrieveSourceMapURL` extracts the following source map from coffeescript.js: "zw(u�"
* This string is passed to source-map which tries to parse it as JSON in `parseSourceMapInput`.
* At this point, bad things happen. :-)

I created a git repo that demonstrates the problem: https://github.com/provegard/sourcemap-bug

Clone it, run `npm install` and then `npm test`. It produces the following output:

```
undefined:1
zw(u�
^

SyntaxError: Unexpected token z in JSON at position 0
at JSON.parse ()
at Object.parseSourceMapInput (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\node_modules\source-map\lib\util.js:433:15)
at new SourceMapConsumer (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\node_modules\source-map\lib\source-map-consumer.js:17:22)
at mapSourcePosition (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\source-map-support.js:190:14)
at wrapCallSite (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\source-map-support.js:358:20)
at C:\kod\projects\sourcemap-bug\node_modules\source-map-support\source-map-support.js:399:26
at Array.map ()
at Function.prepareStackTrace (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\source-map-support.js:398:30)
at prepareStackTrace (internal/errors.js:30:29)
at process.emit (C:\kod\projects\sourcemap-bug\node_modules\source-map-support\source-map-support.js:457:52)
at process._fatalException (internal/process/execution.js:146:25)
```

If I modify retrieveSourceMapURL to print `lastMatch`, I get:

```
[
'//# sourceMappingURL=data:application/json;base64,${encoded}`;',
'data:application/json;base64,${encoded}`;',
undefined,
index: 8757,
```

Versions:
* Node v12.5.0
* npm 6.9.2
* source-map-support: 0.5.13

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne damit, die verlinkte sourcemap-bug-Reproduktion mit npm test auszuführen, und untersuche dann retrieveSourceMapURL und parseSourceMapInput in source-map-support.js. Verfolge, wie der Match von coffeescript.js extrahiert und an source-map übergeben wird; fertig ist die Arbeit, wenn die Reproduktion nicht mehr den JSON SyntaxError auslöst und gültige Source Maps weiterhin funktionieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
coffeescript, javascript, node.js
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.