nollup icon indicating copy to clipboard operation
nollup copied to clipboard

sourceURL contains null-character at the beginning

Open derolf opened this issue 4 years ago • 6 comments

I am having a very weird. Some of the sourceURL references in my nolluped files contain a null-character at the beginning. Example:

//# sourceURL=<null-character-here>commonjsHelpers.js');

I was tracing the problem at least up to this position in PluginLifecycle.js:

        async resolveId (context, id, parentFilePath) {
            let hr = await callAsyncFirstHook(context, 'resolveId', [id, parentFilePath]);

            if (hr === false) {
                return false;
            }


            if (typeof hr === 'string') {
                if (hr[0] === '\000') {
                    throw Error(`"${hr}" contains a null-character at the beginning. encodeURI yields: ${encodeURI(hr)}`);
                }

With this debug code, I get this on the console:

[Nollup] Listening on http://localhost:8081
Error: "commonjsHelpers.js" contains a null-character at the beginning. encodeURI yields: %00commonjsHelpers.js
    at Object.resolveId (/Users/darolf/dev/superapp/node_modules/nollup/lib/impl/PluginLifecycle.js:138:27)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Since I am lost in the details how plugin/hooks get called by nollup, I don't know how to trace this to the source problem.

Any idea how this can happen???

derolf avatar Sep 15 '20 16:09 derolf

The CommonJS plugin likely includes the \0 null byte at the beginning to prevent other plugins from trying to process the helper files as an optimisation and to prevent a loop internally.

Is it causing any build failures or something to break?

PepsRyuu avatar Sep 15 '20 16:09 PepsRyuu

Yeah, I am loading the file through a C++ layer into JavaScriptCore. It broke when the C++ standard library terminated the string at the 0-character. The error message was SyntaxError: Unexpected EOF or something...

I created a unit-test and once I removed all 0-characters, the test passed.

derolf avatar Sep 15 '20 16:09 derolf

...That's a new one, sounds interesting! The fix wouldn't be hard anyways, just a matter of determining where in Nollup to filter out the \0. Can probably release something for this shortly.

PepsRyuu avatar Sep 15 '20 16:09 PepsRyuu

I can confirm that after removing null-characters also the "live app" works!

derolf avatar Sep 15 '20 16:09 derolf

https://github.com/rollup/plugins/blob/621768bfa45e934c9a3eb8462c2d8ea3005762ed/packages/commonjs/src/helpers.js#L19

derolf avatar Sep 15 '20 17:09 derolf

https://github.com/PepsRyuu/nollup/pull/146 This should hopefully take care of it. If you could give it a try and confirm, it'd be much appreciated! :)

PepsRyuu avatar Sep 15 '20 20:09 PepsRyuu