Skip to content

Excluding dev dependencies throws Error #3827

Closed

Description

@ryanmurakami

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

  1. ryanmurakami commented on Jun 22, 2017

    @ryanmurakami
    ContributorAuthor

    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.

  2. added this to the 1.17 milestone on Jun 22, 2017
  3. self-assigned this
    on Jun 22, 2017
  4. pmuens commented on Jun 22, 2017

    @pmuens
    Contributor

    Thanks for the in-depth analysis @ryanmurakami 👍

    That's really helpful. We'll try to reproduce it as well.

  5. removed their assignment
    on Jun 22, 2017
  6. cjelger commented on Jun 22, 2017

    @cjelger

    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. Somehow npm ls returns 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.

  7. pmuens commented on Jun 22, 2017

    @pmuens
    Contributor

    Thinking through this once again, my assumption is that the execSync buffer 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 package command. That should break it once there are too many dev-dependencies. This should hold true for just one package.json file in the root directory.

    I'll report back once I tested it out.

  8. ryanmurakami commented on Jun 22, 2017

    @ryanmurakami
    ContributorAuthor

    I tried setting a crazy high buffer limit on execSync and 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.

  9. ryanmurakami commented on Jun 22, 2017

    @ryanmurakami
    ContributorAuthor

    For the record, cloning into a different directory didn't change the result, still no exclusions.

  10. pmuens commented on Jun 23, 2017

    @pmuens
    Contributor

    Thanks for the further investigation @ryanmurakami 👍

    One thing I notice, the exact same project works fine when building on our Jenkins server.

    That's interesting 🤔

    I have one question. Does this really throw an error where Serverless exits the process? I wrapped it in a try / catch so that it should fail silently when an error pops up. Failing loudly would be really bad.

  11. pmuens commented on Jun 23, 2017

    @pmuens
    Contributor

    Ok. So I debugged this behavior today. Here are my findings:

    • Everything works fine if you've got only one package.json with dozens of dev dependencies
    • It breaks as soon as you add another package.json files in a nested dir and keep the one which has dozens of dev dependencies

    Here's the package.json file 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 ls does, but I'm not sure if this adds too much complexity.

  12. pmuens commented on Jun 23, 2017

    @pmuens
    Contributor

    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:

    |__ package.json
    |__ node_modules
    |__ 1st_level
        |__ package.json
    

    In my case I had installed the packages in the root, but not in the 1st_level dir.

    npm ls --prod=true --parseable=true --silent will 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_level dir) 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 dev dependencies listed in your package.json file and no prod dependencies at all (but I haven't tested it).

    It seems like npm returns an exit code 1 in that case, so the solution || true @cjelger suggested should do the trick.

    BTW. Is || true OS independent? Is it supported on Windows?

    Could you reproduce / confirm this behavior @ryanmurakami @cjelger ?

  13. cjelger commented on Jun 23, 2017

    @cjelger

    @pmuens I finally created a small dummy project to show you the problem. Check here

    Don't forget to npm install the top project and the 2 sub-projects under src/. Then globally install [email protected], run serverless package, and have a look at the generated zip files in .serverless. They contain far too many dependencies!

  14. pmuens commented on Jun 23, 2017

    @pmuens
    Contributor

    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).

  15. ryanmurakami commented on Jun 23, 2017

    @ryanmurakami
    ContributorAuthor

    @pmuens Here are some quick answers to previous questions/comments:

    I have one question. Does this really throw an error where Serverless exits the process? I wrapped it in a try / catch so that it should fail silently when an error pops up. Failing loudly would be really bad.

    It's throwing from that execSync call 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!

  16. ryanmurakami commented on Jun 23, 2017

    @ryanmurakami
    ContributorAuthor

    @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 mocha and mocha-jenkins-reporter are 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.

  17. ryanmurakami commented on Jun 23, 2017

    @ryanmurakami
    ContributorAuthor

    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 ls command and it always breaks off at 8192 bytes for the stdio buffer. @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...

  18. pmuens commented on Jun 24, 2017

    @pmuens
    Contributor

    Nice findings! Thanks for yet another deep dive @ryanmurakami 👍

    nodejs/node#12921 could definitely be related!

    @pmuens, what kind of environment are you running?

    My setup is the following:

    MacOS: 10.12.5
    Docker version: Version 17.03.1-ce-mac12 (17661) Channel: stable d1db12684b
    Linux Distro: Debian GNU/Linux 8 (jessie)
    Kernel version: Linux 1b2ae1b5b55e 4.9.27-moby #1 SMP Thu May 11 04:01:18 UTC 2017 x86_64 GNU/Linux
    Node.js version: v8.1.0
    Npm version: 5.0.3
    

    It might be a nice DX improvement to announce that error somehow

    That's a good idea. We could catch the error and see if it's related to dev dependency exclusion and print the log message. Otherwise it should still fail silently IMHO.

    Next up I'll give @cjelger repo a spin.

  19. ryanmurakami commented on Jun 24, 2017

    @ryanmurakami
    ContributorAuthor

    @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:

    • Import the npm code and use the ls module itself to get the dependencies through that API. Not sure if this is even possible and is going to be horrible to maintain.
    • Use the package-lock.json as a fallback if the npm ls command fails. I've noticed each dependency in the package-lock.json file has a dev property that we could use to filter them out. Not sure if that property means it isn't used in prod though.
    • Pipe the npm ls output 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.
  20. pmuens commented on Jun 25, 2017

    @pmuens
    Contributor

    @pmuens Hmm... so, running it in that Docker instance might be why it's working

    @ryanmurakami 🤔 it also failed in the container. The main reason is when I don't have the dependencies installed (no node_modules dir) or I don't have dev dependencies at all. My assumption is that npm ls returns with a process 1 in that case (see also #3827 (comment)).

    IMHO the first proposal (use the npm ls module 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 on exec? 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:

  21. modified the milestones: 1.17, 1.18 on Jun 27, 2017
  22. pmuens commented on Jun 30, 2017

    @pmuens
    Contributor

    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 👍

  23. pmuens commented on Jul 4, 2017

    @pmuens
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions