ts-babel-node icon indicating copy to clipboard operation
ts-babel-node copied to clipboard

An error occurred while trying to read the map file

Open dan-j opened this issue 8 years ago • 6 comments

Using version 1.1.1, I get the following error and can't get round it. Downgrading to 1.1.0 fixes the issue.

It's reproducible on the most trivial example:

$ yarn add -D ts-babel-node

index.ts:

console.log('hello world');

$ ts-babel-node index.ts

/repo/node_modules/convert-source-map/index.js:40
    throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
    ^

Error: An error occurred while trying to read the map file at /repo/data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL0Rhbi9Eb2N1bWVudHMvZ2l0X3JlcG9zL3RzLXJlYWN0LWV4cHJlc3Mtc3RhcnRlci9pbmRleC50cyIsInNvdXJjZXMiOlsiL1VzZXJzL0Rhbi9Eb2N1bWVudHMvZ2l0X3JlcG9zL3RzLXJlYWN0LWV4cHJlc3Mtc3RhcnRlci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsT0FBTyxDQUFDLEdBQUcsQ0FBQyxhQUFhLENBQUMsQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKCdoZWxsbyB3b3JsZCcpO1xuIl19
Error: ENOENT: no such file or directory, open '/repo/data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL0Rhbi9Eb2N1bWVudHMvZ2l0X3JlcG9zL3RzLXJlYWN0LWV4cHJlc3Mtc3RhcnRlci9pbmRleC50cyIsInNvdXJjZXMiOlsiL1VzZXJzL0Rhbi9Eb2N1bWVudHMvZ2l0X3JlcG9zL3RzLXJlYWN0LWV4cHJlc3Mtc3RhcnRlci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsT0FBTyxDQUFDLEdBQUcsQ0FBQyxhQUFhLENBQUMsQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKCdoZWxsbyB3b3JsZCcpO1xuIl19'
    at readFromFileMap (/repo/node_modules/convert-source-map/index.js:40:11)
    at new Converter (/repo/node_modules/convert-source-map/index.js:47:32)
    at Object.exports.fromMapFileComment (/repo/node_modules/convert-source-map/index.js:110:10)
    at Object.exports.fromMapFileSource (/repo/node_modules/convert-source-map/index.js:122:22)
    at Module.compile (/repo/node_modules/ts-babel-node/index.js:46:36)
    at Module.<anonymous> (/repo/node_modules/ts-babel-node/index.js:89:15)
    at Module.m._compile (/repo/node_modules/ts-node/dist/index.js:219:29)
    at Module._extensions..js (module.js:580:10)
    at require.extensions.(anonymous function) (/repo/node_modules/ts-node/dist/index.js:221:16)
    at Object.hook (/repo/node_modules/ts-babel-node/index.js:42:3)

dan-j avatar Jul 19 '17 11:07 dan-j

The first line of this function Line 56 seems to be the culprit:

function compile(base, code, filename) {
  var sourcemap = convertSourceMap.fromMapFileSource(code, '.').toObject();
  code = convertSourceMap.removeMapFileComments(code);

  var babelOutput = babel.transform(code, getBabelOpts(filename, sourcemap));

  // babelOutput has a bunch of undocumented stuff on it. Just grab what we need to save memory
  outputs[filename] = { code: babelOutput.code, map: babelOutput.map };

  return base.call(this, babelOutput.code, filename);
}

This tries to extract a file name from the source-map comment, however this module outputs source-maps as base64 encoded comments. Changing line 56 to the following fixes the issue:

<   var sourcemap = convertSourceMap.fromMapFileSource(code, '.').toObject();
---
>   var sourcemap = convertSourceMap.fromSource(code).toObject();

Happy to make a pull request, although I'd like to know how this issue doesn't already exist because it's made the module absolutely unusable for myself.

dan-j avatar Jul 20 '17 12:07 dan-j

I've found out what's caused the issue in the first place.

I was using the most recent version of ts-node 3.2.0, ts-babel-node requires ^3.0.6. In 3.1.0 there was a change which emitted source-maps inline rather than to a separate file cache.

I've forked the repo and intend on making the fix and upgrading to ts-node 3.2.0.

dan-j avatar Jul 20 '17 12:07 dan-j

Ok, you can't upgrade to 3.2.0 because there's a bug where it doesn't work for .tsx files.

https://github.com/TypeStrong/ts-node/issues/393

Will leave this here for now, wait until ts-node have fixed their issues

dan-j avatar Jul 20 '17 13:07 dan-j

Cheers! This unblocked me after a few hours of tearing my hair out!

n8agrin avatar Jan 06 '18 03:01 n8agrin

I got the same problem, any news about fix?

pumano avatar Mar 15 '18 09:03 pumano

Pretty sure the creator isn't active on this anymore, I've had a Pull Request open since July which hasn't been looked at.

I'd say fork my fork https://github.com/dan-j/ts-babel-node (don't use mine, I might forget and delete it someday) it was July when I last committed to it but it fixes the issues discussed here. I'm no longer using it so won't be active on it, but I'll respond to messages if need be.

dan-j avatar Mar 16 '18 00:03 dan-j