browserify / static-module

convert module usage to inline expressions
MIT License
74 stars 23 forks source link

Fatal error: Cannot overwrite a zero-length range – use appendLeft or prependRight instead #41

Closed Richard-Walton closed 6 years ago

Richard-Walton commented 6 years ago

I've recently upgraded my brfs package to 1.50 which includes the latest source map changes implemented in this module.

Unfortunately my build is now failing with the following error:

Fatal error: Cannot overwrite a zero-length range – use appendLeft or prependRight instead

After some digging around I tracked down the issue to this line: https://github.com/browserify/static-module/blob/master/index.js#L64

In my case s.offset === 0 (creating the aforementioned zero-length range error).

I have discovered a workaround for the issue. Originally my code looked like this:

var fs = require('fs')
    , anotherModule = require('anotherModule')
    , x = true;
...
var file = fs.readFileSync(....);

Moving the require('fs') to it's own line fixes the problem:

var fs = require('fs');
var anotherModule = require('anotherModule')
    , x = true;
...
var file = fs.readFileSync(....);

I wonder if the removal of the require('fs') calls during the browserify build process causes this error?

My initial thoughts to fix this issue would involve adding a guard against s.offset being 0... I'm happy to open a PR if you think this is a viable solution.

Richard-Walton commented 6 years ago

@goto-bus-stop Any thoughts?

goto-bus-stop commented 6 years ago

oh crap, thanks. adding a check for s.offset and then using one of the append or prepend methods instead sounds good to me!

goto-bus-stop commented 6 years ago

Oops, I committed on the wrong branch, meant to open a PR for that instead. If CI comes back green I'll release. Thanks for the detailed report!

goto-bus-stop commented 6 years ago

:package: 2.2.2

Richard-Walton commented 6 years ago

Thanks @goto-bus-stop - I ended up getting stuck thinking about how to write a test for the change... looks like it wasn't needed! Sorry I didn't get around to opening a PR for you.

Thanks again

goto-bus-stop commented 6 years ago

no worries! I changed an existing test to also generate source maps, which triggered the error. :)