zip-it-and-ship-it icon indicating copy to clipboard operation
zip-it-and-ship-it copied to clipboard

Dependencies are excluded in output when a duplicate function exists

Open russelldavis opened this issue 4 years ago • 6 comments

- Do you want to request a feature or report a bug? Bug

- What is the current behavior? When a duplicate function exists, dependencies are not included in the resulting zip file.

- If the current behavior is a bug, please provide the steps to reproduce. Create the following two files:

foo.js:

require("./bar");

function foo() {}
function foo() {}

module.exports = foo;

bar.js:

console.log("hello from bar");

Then run netlify build.

- What is the expected behavior? bar.js should get bundled in the zip file for foo.js, but it doesn't.

If you remove the duplicate foo function, it gets bundled as expected.

If you run DEBUG=* netlify build, you'll see this in the output:

precinct could not parse content: Identifier 'foo' has already been declared (4:9)

- Please mention your node.js, and operating system version. Node v12.20.0, MacOS 10.15.7

russelldavis avatar Jan 30 '21 00:01 russelldavis

Hi @russelldavis,

Thanks for filing this issue.

This is a bug in the library that zip-it-and-ship-it is using to parse Node.js Function files node-precinct: I would suggest filing an issue to that repository instead.

That being said, declaring twice the same function seems to be an odd pattern. It only works when declaring functions as function foo() {}, not as const foo = function() {} nor const foo = () => {}.

ehmicky avatar Feb 01 '21 12:02 ehmicky

Hey @ehmicky, thanks, yeah I'll file an issue with node-precint as well. Though there might some config or different way of using that library here that avoids the bug.

To be clear, declaring the same function twice is not an intentional pattern. But when it happens accidentally, the code still works, and zip-it-and-ship-it shouldn't break working code. (For example, my code was running fine with netlify dev, but broke when I deployed to netlify, which is obviously bad).

russelldavis avatar Feb 01 '21 17:02 russelldavis

To be clear, declaring the same function twice is not an intentional pattern. But when it happens accidentally, the code still works, and zip-it-and-ship-it shouldn't break working code. (For example, my code was running fine with netlify dev, but broke when I deployed to netlify, which is obviously bad).

I completely agree :+1:

Though there might some config or different way of using that library here that avoids the bug.

I don't think there is such an option, but if there is, please let me know. Otherwise, it looks like this should be fixed upstream in node-precinct.

ehmicky avatar Feb 01 '21 17:02 ehmicky

I tracked down where this is coming from. node-precint uses node-source-walker which in turn uses @babel/parser to do the parsing. And there's an issue about this in @babel/parser: https://github.com/babel/babel/issues/12354.

The TLDR is that babel is treating the file as a module, where a duplicate function actually is an error. The fix is to tell babel to treat the file as a script instead of a module, which can be done by passing {sourceType: "script"} in the options.

node-source-walker already takes in options that will be passed on to babel. But precint does not take in any options to pass to node-source-walker. So to fix this, you'll need to file an issue with precint to take in that option, then update zip-it-and-ship-it to pass that option to precint.

russelldavis avatar Feb 01 '21 18:02 russelldavis

Thanks for investigating this @russelldavis! Would you like to create this issue in node-precinct and link it in the current issue?

ehmicky avatar Feb 01 '21 18:02 ehmicky

Turns out there's already an issue for that: https://github.com/dependents/node-precinct/issues/68

russelldavis avatar Feb 01 '21 18:02 russelldavis