Repository navigation
Closed
Labels
Milestone
Description
This is a Bug Report
Description
When packaging a serverless project, the dev dependency exclusion does not happen. It throws an Error that is caught with no action.
The excludeNodeDevDependencies function in /lib/plugins/package/lib/zipService.js throws when it calls:
const prodDependencies = childProcess
.execSync('npm ls --prod=true --parseable=true --silent', {
maxBuffer: 2000*1024
})
.toString().trim();When catching the thrown error, it seems that the command is partially run, as there are some directories listed in stdout, but they stop in the middle. The error property is null, but the status is 1. Here's a stack trace:
Error: Command failed: npm ls --prod=true --parseable=true --silent
at checkExecSyncError (child_process.js:481:13)
at Object.execSync (child_process.js:521:13)
at _.forEach (/Users/rylewis/Documents/Dev/Repo/serverless/lib/plugins/package/lib/zipService.js:145:10)
at arrayEach (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/lodash/lodash.js:537:11)
at Function.forEach (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/lodash/lodash.js:9359:14)
at excludeNodeDevDependencies (/Users/rylewis/Documents/Dev/Repo/serverless/lib/plugins/package/lib/zipService.js:136:7)
at Package.excludeDevDependencies (/Users/rylewis/Documents/Dev/Repo/serverless/lib/plugins/package/lib/zipService.js:30:25)
at Package.tryCatcher (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/rylewis/Documents/Dev/Repo/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)I've tried increasing the timeout and maxBuffer options, but no change. Running the command npm ls --prod=true --parseable=true --silent in my shell works correctly and returns all the directories.
Additional Data
- Serverless Framework Version you're using: 1.16.0
- Operating System: macOS
Activity
ryanmurakami commentedon Jun 22, 2017
I've also tried pulling the dependencies/devDependencies into a fresh project's
package.json. This issue does not reproduce. It seems like it's only happening for existing projects. I can try it with a few other services we have tomorrow.pmuens commentedon Jun 22, 2017
Thanks for the in-depth analysis @ryanmurakami 👍
That's really helpful. We'll try to reproduce it as well.
cjelger commentedon Jun 22, 2017
Try to replace the code with
childProcess.execSync('npm ls --prod=true --parseable=true || true);which ensures that the npm command returns 0 to the shell. Somehownpm lsreturns 1 as soon as a minor issue happens when parsing the dependencies.It's what I did here when I was trying to debug @pmuens's code.
pmuens commentedon Jun 22, 2017
Thinking through this once again, my assumption is that the
execSyncbuffer is just full since there a dozens of packages which should be excluded in large projects.Will try to reproduce it tomorrow by adding a bunch of dev dependencies and running the
serverless packagecommand. That should break it once there are too many dev-dependencies. This should hold true for just onepackage.jsonfile in the root directory.I'll report back once I tested it out.
ryanmurakami commentedon Jun 22, 2017
I tried setting a crazy high buffer limit on
execSyncand the same thing happened.One thing I notice, the exact same project works fine when building on our Jenkins server. The code is the same, just the environment is different. I wonder if the real issue is maybe something outside of what Serverless is doing here. I'll try cloning my project somewhere clean locally and see if I can repro the issue.
ryanmurakami commentedon Jun 22, 2017
For the record, cloning into a different directory didn't change the result, still no exclusions.
pmuens commentedon Jun 23, 2017
Thanks for the further investigation @ryanmurakami 👍
That's interesting 🤔
I have one question. Does this really throw an error where Serverless exits the process? I wrapped it in a
try/catchso that it should fail silently when an error pops up. Failing loudly would be really bad.pmuens commentedon Jun 23, 2017
Ok. So I debugged this behavior today. Here are my findings:
package.jsonwith dozens of dev dependenciespackage.jsonfiles in a nested dir and keep the one which has dozens of dev dependenciesHere's the
package.jsonfile I've used in the root dir:{ "name": "test-service", "version": "1.0.0", "main": "handler.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "node-fetch": "^1.7.1" }, "devDependencies": { "ava": "^0.19.1", "babel-cli": "^6.24.1", "babel-core": "^6.25.0", "babel-preset-es2015": "^6.24.1", "browserify": "^14.4.0", "express": "^4.15.3", "jest": "^20.0.4", "mocha": "^3.4.2", "react": "^15.6.1", "react-native": "^0.45.1", "webpack": "^3.0.0" }, "description": "" }Other solution would be to use a directory traversal and reproduce the stuff
npm lsdoes, but I'm not sure if this adds too much complexity.pmuens commentedon Jun 23, 2017
Holy cow! I think I've found the problem (or at least a way to reproduce it)!
Let's assume we have the following directory structure:
In my case I had installed the packages in the root, but not in the
1st_leveldir.npm ls --prod=true --parseable=true --silentwill return the correct result of prod dependencies in the root dir (including the path of the root directory).However when you've not installed packages (like it's the case in the
1st_leveldir) and run the command in that dir you'll get the path of the directory back, but no packages (since you've not installed anything).My assumption is that it'll also fail if you only have
devdependencies listed in yourpackage.jsonfile and noproddependencies at all (but I haven't tested it).It seems like
npmreturns an exit code 1 in that case, so the solution|| true@cjelger suggested should do the trick.BTW. Is
|| trueOS independent? Is it supported on Windows?Could you reproduce / confirm this behavior @ryanmurakami @cjelger ?
cjelger commentedon Jun 23, 2017
@pmuens I finally created a small dummy project to show you the problem. Check here
Don't forget to
npm installthe top project and the 2 sub-projects undersrc/. Then globally install[email protected], runserverless package, and have a look at the generated zip files in.serverless. They contain far too many dependencies!pmuens commentedon Jun 23, 2017
Nice! Thanks for compiling that @cjelger 👍
That will be really helpful!
Don't know if you saw it already, but I too did some research and came up with some observations in #3827 (comment) and #3827 (comment).
ryanmurakami commentedon Jun 23, 2017
@pmuens Here are some quick answers to previous questions/comments:
It's throwing from that
execSynccall and then being caught by your catch. So, not failing loudly, just completely silently. It might be a nice DX improvement to announce that error somehow, just so users know that their dev dependencies weren't excluded.In reference to the nested package.json, for our project specifically, we don't have a nested structure like that. We do have several peer service directories, but each are packaged/deployed individually.
I'll keep messing around and try out @cjelger's test project too.
Thanks!
ryanmurakami commentedon Jun 23, 2017
@cjelger Thanks for making the dummy project!
I gave it a shot, and it looks like serverless is packaging it correctly on my setup. The dev dependencies
mochaandmocha-jenkins-reporterare not included in the zipped package that Serverless creates. So, it makes me think there is something specific with this project and your environment that is causing the issue.ryanmurakami commentedon Jun 23, 2017
I'm still working on it, but this issue seems exactly like what I'm experiencing:
nodejs/node#12921
I've tried mixing up the type of output from that
npm lscommand and it always breaks off at 8192 bytes for thestdiobuffer. @pmuens, what kind of environment are you running? I'm:macOS: 10.12.5
Node.js: v6.10.3
npm: 5.0.3
Our Jenkins slave is running some linux distro with:
Node.js: v6.9.4
npm: v4.3.0
I'm wondering if this is a mac bug like that issue above describes...
pmuens commentedon Jun 24, 2017
Nice findings! Thanks for yet another deep dive @ryanmurakami 👍
nodejs/node#12921 could definitely be related!
My setup is the following:
That's a good idea. We could catch the error and see if it's related to
devdependency exclusion and print the log message. Otherwise it should still fail silently IMHO.Next up I'll give @cjelger repo a spin.
ryanmurakami commentedon Jun 24, 2017
@pmuens Hmm... so, running it in that Docker instance might be why it's working. This seems to be macOS only. Unless you can't repro it locally either.
So, I was thinking of some possible work arounds:
npmcode and use thelsmodule itself to get the dependencies through that API. Not sure if this is even possible and is going to be horrible to maintain.package-lock.jsonas a fallback if thenpm lscommand fails. I've noticed each dependency in thepackage-lock.jsonfile has adevproperty that we could use to filter them out. Not sure if that property means it isn't used inprodthough.npm lsoutput into a file and then parse that file. I'm thinking this might be the best way, but would require a little file i/o.pmuens commentedon Jun 25, 2017
@ryanmurakami 🤔 it also failed in the container. The main reason is when I don't have the dependencies installed (no
node_modulesdir) or I don't have dev dependencies at all. My assumption is thatnpm lsreturns with a process 1 in that case (see also #3827 (comment)).IMHO the first proposal (use the
npm lsmodule itself) and the last one (pipe it into a file) would be possible solutions here. The last one is pretty interesting. How would you pipe the data into the file if we still rely onexec? Or is the main problem the exceeded buffer for the console output?Another possible solution could be to re-write the dev dependency picking. But this could be hard to maintain / get right.
We've discussed other solutions in #2709. Maybe we should re-visit those:
pmuens commentedon Jun 30, 2017
Just a quick update. Seems like @arabold found the root cause of this. He added a comment in the feature-PR here: #3737 (comment).
Thanks for looking into it @arabold 👍
pmuens commentedon Jul 4, 2017
Just another quick update on this one.
We've just started a PR which will include a fix for this (will be updated frequently).
It can be found here: #3889.