Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty files break sourcemap #66

Closed
xealot opened this issue Sep 23, 2014 · 12 comments
Closed

Empty files break sourcemap #66

xealot opened this issue Sep 23, 2014 · 12 comments

Comments

@xealot
Copy link

xealot commented Sep 23, 2014

Occasionally you have a step (in my case, typescript compilation) that generates an empty file. This causes concat to fail.

<root>/applications/gulp-tools/node_modules/gulp-concat/node_modules/concat-with-sourcemaps/node_modules/source-map/lib/source-map/source-map-generator.js:273
        throw new Error('Invalid mapping: ' + JSON.stringify({
              ^
Error: Invalid mapping: {"generated":{"line":1,"column":0},"original":{"line":1,"column":0},"name":null}
    at SourceMapGenerator_validateMapping [as _validateMapping] (<root>/applications/gulp-tools/node_modules/gulp-concat/node_modules/concat-with-sourcemaps/node_modules/source-map/lib/source-map/source-map-generator.js:273:15)
    at SourceMapGenerator_addMapping [as addMapping] (<root>/applications/gulp-tools/node_modules/gulp-concat/node_modules/concat-with-sourcemaps/node_modules/source-map/lib/source-map/source-map-generator.js:102:12)
    at Concat.add (<root>/applications/gulp-tools/node_modules/gulp-concat/node_modules/concat-with-sourcemaps/index.js:75:25)
    at Stream.bufferContents (<root>/applications/gulp-tools/node_modules/gulp-concat/index.js:35:12)
    at Stream.stream.write (<root>/applications/gulp-tools/node_modules/gulp-concat/node_modules/through/index.js:26:11)
    at write (_stream_readable.js:583:24)
    at flow (_stream_readable.js:592:7)
    at CompileStream.pipeOnReadable (_stream_readable.js:624:5)
    at CompileStream.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at CompileStream.Readable.push (_stream_readable.js:127:10)
    at emit (<root>/applications/gulp-tools/node_modules/gulp-typescript/release/project.js:245:22)
    at sortedEmit (<root>/applications/gulp-tools/node_modules/gulp-typescript/release/project.js:267:17)
    at Project.compile (<root>/applications/gulp-tools/node_modules/gulp-typescript/release/project.js:273:17)
    at CompileStream.compile (<root>/applications/gulp-tools/node_modules/gulp-typescript/release/main.js:53:23)
    at CompileStream.end (<root>/applications/gulp-tools/node_modules/gulp-typescript/release/main.js:63:14)
    at DestroyableTransform.onend (<root>/applications/gulp-tools/node_modules/gulp-sourcemaps/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:523:10)
    at DestroyableTransform.g (events.js:180:16)
    at DestroyableTransform.EventEmitter.emit (events.js:117:20)
    at <root>/applications/gulp-tools/node_modules/gulp-sourcemaps/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:965:16
    at process._tickDomainCallback (node.js:459:13)

Here is an example of the task that causes this failure.

var ts = require('gulp-typescript');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('compile-ts', function () {
    var compile = gulp.src(conf.tscSources)
        .pipe(sourcemaps.init())
        .pipe(ts({
            declarationFiles: true,
            noExternalResolve: true,
            sortOutput: true,
            target: 'ES5'
        }));

    return compile.js
        .pipe(concat('app.js'))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest(conf.distDir));
});

While it's relatively trivial to filter empty files from a stream I believe not doing so shouldn't cause concat to tank.

@ivogabe
Copy link

ivogabe commented Sep 23, 2014

Actually, empty files aren't the problem but files with empty source maps (source maps whose property mappings is "") are.

A bit shorter gulp file that reproduces the problem:

var gulp = require('gulp');
var ts = require('gulp-typescript');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('default', function () {
    return gulp.src('empty.ts')
        .pipe(sourcemaps.init())
        .pipe(ts())
        .pipe(concat('app.js'))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('release'));
});

The source map is:

{ version: 3,
  file: 'empty.ts',
  sourceRoot: '...',
  sources: [ 'source.ts' ],
  names: [],
  mappings: '' }

@yocontra
Copy link
Member

cc @floridoo

@BenjaminMalley BenjaminMalley mentioned this issue Oct 10, 2014
4 tasks
@karlvr
Copy link

karlvr commented Oct 16, 2014

I get this fault too with an input file that is empty, with a source map. It sounds like exactly the same case as above.

I had a subsequent interest situation where the gulp-newer plugin swallowed the error, so the pipe just stopped. I've raised an issue there. It then occurred that the error out of gulp-concat isn't very useful. It doesn't tell you which file triggered the fault, for example.

Would it be cool to catch these errors and augment them with the filename? I'm not sure the best practice for doing something like that. I tried a simple try {} catch {} around concat.add(file.relative, file.contents.toString(), file.sourceMap); in bufferContents(file), and added the following in the catch:

return this.emit('error', new PluginError('gulp-concat', 'Failed to concat file: ' + file.relative + ', Caused by: ' + e.message));

Would this be a useful addition?

@bsr203
Copy link

bsr203 commented Nov 2, 2014

Having the same issue. any update?

@yocontra
Copy link
Member

yocontra commented Nov 2, 2014

This error is actually coming from a module that gulp-concat uses, not gulp-concat. Can you open this issue there? https://github.com/floridoo/concat-with-sourcemaps

Include the stacktrace from the original post and link back to this issue so people here can track it there once it is created

@floridoo
Copy link
Contributor

floridoo commented Nov 7, 2014

I could not reproduce the error. Nor with plain concat-with-sourcemaps, nor with the example by @ivogabe.

@MartinJohns
Copy link

@floridoo I am having the same issue. I prepared a gist for you.

Just do the following:

git clone https://gist.github.com/MartinJohns/7bca27eddd9fc85f421f
cd 7bca27eddd9fc85f421f
npm install
gulp watch

Then, while the watch task is running, open the test.ts and save it. The watcher should notice it, restart the script-task and the error will occur.

@ivogabe
Copy link

ivogabe commented Nov 8, 2014

I re-tested my gulpfile, and it does throw an error here. I used the latest releases of everything on npm. Did empty.ts exists and was it empty when you tested it? I've tested it on windows & mac and both failed.

@floridoo
Copy link
Contributor

floridoo commented Nov 8, 2014

Thanks @MartinJohns! Found the issue and fixed it. It was the combination of an empty mappings property and an empty sources array that produced the error.

@MartinJohns
Copy link

Can confirm, it does not crash anymore after one npm update. :-)

@yocontra
Copy link
Member

yocontra commented Nov 9, 2014

Thanks @floridoo

@yocontra yocontra closed this as completed Nov 9, 2014
@hupad
Copy link

hupad commented Jan 11, 2017

@floridoo could you elaborate on this fix you made please. I am running into the issue and not sure what exactly went wrong.

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

No branches or pull requests

8 participants