node-source-map-support icon indicating copy to clipboard operation
node-source-map-support copied to clipboard

Command line scripts - wrong error line number

Open simov opened this issue 10 years ago • 7 comments
trafficstars

:wave:

I just noticed that if I have:

#!/usr/bin/env node

on top of my file, the error line is not the correct one. The error message itself is correct but the error line number is the next one after the actual error.

simov avatar Oct 16 '15 08:10 simov

How did that get there? If the tool that generates the source map also inserts that line before it generates the source map, then the mapping should be correct. If that line is inserted after the source map is generated though, then the source map is incorrect because inserting new lines at the beginning of the generated JavaScript file means the source map has to be updated too.

evanw avatar Oct 16 '15 16:10 evanw

I'm not exactly sure, here is my gulpfile.

I'm using the latest version of everything. Here is my test file:

#!/usr/bin/env node

import {install} from 'source-map-support'
install()

if (poop.wqw.boo) {

}

var v = 'something'

Running: node build/index.js:

/media/SSD/tmp/sourcemaps-bug/index.js:8
}
 ^
ReferenceError: poop is not defined
    at Object.<anonymous> (/media/SSD/tmp/sourcemaps-bug/index.js:8:2)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

As you can see the error message is correct, but the line should be 6 where the if statement is. If I remove the #!/usr/bin/env node line it works as expected.

And the generated stuff:

#!/usr/bin/env node
'use strict';

var _sourceMapSupport = require('source-map-support');

(0, _sourceMapSupport.install)();

if (poop.wqw.boo) {}

var v = 'something';
//# sourceMappingURL=index.js.map
{"version":3,"sources":["index.js"],"names":[],"mappings":";;gCAEsB,oBAAoB;;AAC1C,gCAAS,CAAA;;AAET,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAEjB;;AAED,IAAI,CAAC,GAAG,WAAW,CAAA","file":"index.js","sourcesContent":["#!/usr/bin/env node\n\nimport {install} from 'source-map-support'\ninstall()\n\nif (poop.wqw.boo) {\n  \n}\n\nvar v = 'something'\n"],"sourceRoot":"/media/SSD/tmp/sourcemaps-bug"}

simov avatar Oct 16 '15 16:10 simov

I was too facing this just now. Line number off by 2.

However retainLines: 'true' (in babel options) seem to solve it.

I'm suspecting babel-polyfill or some other helper function is injecting extra 2 lines.

Funnily I was trying to get rid of that retainLines option with this library. great succes/s!

laggingreflex avatar Nov 19 '15 16:11 laggingreflex

@laggingreflex can you paste your gulpfile here?

simov avatar Nov 19 '15 18:11 simov

gulp@4 [email protected]

const gulp = require('gulp');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');

gulp.task('babel', done =>
    gulp.src('src/**/*.es6')
    .pipe(sourcemaps.init())
    .pipe(babel({
        presets: ['es2015', 'stage-0'],
        retainLines: 'true',
    }))
    .pipe(sourcemaps.write('.', { sourceRoot: 'src' }))
    .pipe(gulp.dest('lib')));


gulp.task('build', gulp.series('babel'));

gulp.task('watch', done =>
    gulp.watch('src/**/*.es6', gulp.series('babel')));

gulp.task('default', gulp.series('build', 'watch'));

laggingreflex avatar Nov 19 '15 18:11 laggingreflex

@laggingreflex thanks for your config, but I get the exact same wrong line number with it. Using gulp@4 [email protected] and the test file from my second comment from above.

simov avatar Nov 20 '15 07:11 simov

I was just passing by, and I thought maybe since this is a command line script you're making that this might be a conflict with how node works. Before node runs a command script it removes the shebang from the script's string. That might put your error off by one line.

hollowdoor avatar Dec 19 '16 16:12 hollowdoor