serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

forceInclude array being ignored

Open ahomatthew opened this issue 9 years ago • 11 comments

This is a Bug Report

Description

Trying to deploy with the serverless-webpack plugin, with:

webpackIncludeModules:
  forceInclude:
    - mysql2

to include the mysql2 package (which is listed in my dependencies) which is a required dependency of sequelize.

What I expected to happen is to see the mysql2 is listed in the .zip that gets created, what actually happened is it is not included in the .zip.

service: myServiceName
frameworkVersion: ">=1.0.0 <2.0.0"

provider:
  name: aws
  runtime: nodejs6.10
  stage: staging

plugins:
  - serverless-webpack

custom:
  webpackIncludeModules:
    forceInclude:
      - mysql2

Additional Data

"mysql2": "^1.4.2",
"sequelize": "^4.8.2"

"serverless": "^1.21.1",
"serverless-webpack": "^3.0.0"

Running in linux 17.10

I worked around the issue for now by doing an import mysql2 myself in one of my files.

ahomatthew avatar Sep 27 '17 22:09 ahomatthew

Hi @ahomatthew . Can you run the package or deploy command with the --verbose switch and SLS_BEGUG=* set in the environment? The output should then show exactly what is evaluated and we'll see better, what is going wrong here. Please post the output. Thanks.

HyperBrain avatar Sep 28 '17 08:09 HyperBrain

Hi @HyperBrain -- here it is, personal information redacted.

sally@sunshine:~/path/to/project$ SLS_DEBUG=* yarn deploy --stage test --verbose
yarn deploy v1.0.1
$ serverless deploy "--stage" "test" "--verbose"
Serverless: WARNING: Plugin ServerlessWebpack uses deprecated hook before:deploy:createDeploymentArtifacts,
                     use package:createDeploymentArtifacts hook instead
Serverless: WARNING: Plugin ServerlessWebpack uses deprecated hook after:deploy:createDeploymentArtifacts,
                     use package:createDeploymentArtifacts hook instead
Serverless: Invoke deploy
Serverless: Invoke package
Serverless: Invoke aws:common:validate
Serverless: Invoke aws:common:cleanupTempDir
Serverless: Bundling with Webpack...
Time: 1546ms
                             Asset     Size  Chunks             Chunk Names
          handlers/foo-handler.js   105 kB    0, 5  [emitted]  handlers/foo-handler
   [0] external "lodash" 42 bytes {0} {1} {2} {3} {4} {5} [not cacheable]
   [7] external "validator" 42 bytes {0} {1} {4} {5} [not cacheable]
   [8] ./lib/foo.js 6.9 kB {0} {1} {4} {5} [built]
  [11] ./models/foo-model.js 747 bytes {0} {1} {4} {5} [built]
  [14] external "moment-timezone" 42 bytes {0} {2} [not cacheable]
    + 21 hidden modules
Serverless: Fetch dependency graph from /path/to/project/package.json
Serverless: Packing external modules: lodash@^4.17.4, [email protected], bluebird@^3.5.0, sequelize@^4.8.2, sequelize-transforms@^1.0.0, validator@^7.0.0, jsonwebtoken@^7.1.9, moment-timezone@^0.5.13, node-fetch@^1.7.3, bcrypt@^1.0.3
Serverless: Package took [9136 ms]
Serverless: Copy modules: /path/to/project/.webpack/service [1120 ms]
Serverless: Prune: /path/to/project/.webpack/service [619 ms]
Serverless: Zip service: /path/to/project/.webpack/service [2782 ms]
Serverless: Packaging service...
Serverless: Remove /path/to/project/.webpack
Serverless: Invoke aws:package:finalize
Serverless: Invoke aws:common:moveArtifactsToPackage
Serverless: Invoke aws:common:validate
Serverless: Invoke aws:deploy:deploy
Serverless: Creating Stack...
Serverless: Checking Stack create progress...

In my custom forceInclude I currently have mysql2 listed, and it is also listed in the dependency section of my package.json

ahomatthew avatar Sep 28 '17 13:09 ahomatthew

Thanks for the info. I will try to reproduce it locally.

HyperBrain avatar Sep 28 '17 14:09 HyperBrain

I tried to reproduce the issue but somehow I did not manage to do so. Here is what I did:

  • Started with the babel-dynamically-entries example project from examples
  • Added mysql2 to the dependencies (npm install mysql2 --save) - and some others
  • Added mysql2 to the forceInclude array - it is not used anywhere in the code
  • Package the service (serverless package)
  • Checked the outputs and the zip files

Here's the data:

{
  "name": "serverless-webpack-babel-example",
  "version": "1.0.0",
  "description": "Serverless webpack example using Babel",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Nicola Peduzzi <[email protected]> (http://nikso.net)",
  "license": "MIT",
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "serverless": "^1.22.0",
    "serverless-webpack": "^3.1.0",
    "webpack": "^3.3.0",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "bluebird": "^3.5.0",
    "mysql2": "^1.4.2",
    "request": "^2.82.0",
    "request-promise": "^4.2.1"
  }
}
# serverless.yml
...
custom:
  webpackIncludeModules:
    forceInclude:
      - mysql2
...
$ node node_modules/serverless/bin/serverless package
Serverless: Bundling with Webpack...
Time: 3551ms
   Asset     Size  Chunks             Chunk Names
first.js  3.29 kB       0  [emitted]  first
   [0] ./first.js 373 bytes {0} [built]
   [1] external "babel-polyfill" 42 bytes {0} [not cacheable]
   [2] external "request" 42 bytes {0} [not cacheable]
Time: 3536ms
    Asset     Size  Chunks             Chunk Names
second.js  3.27 kB       0  [emitted]  second
   [0] ./second.js 361 bytes {0} [built]
   [1] external "babel-polyfill" 42 bytes {0} [not cacheable]
   [2] external "request-promise" 42 bytes {0} [not cacheable]
Serverless: Packing external modules: request@^2.82.0, mysql2@^1.4.2, request-pr
omise@^4.2.1
Serverless: Packaging service...

As you see, the mysql package got packaged correctly, what is also reflected in the zip:

$ unzip -l .serverless/first.zip | grep "mysql2"
    18279  1980-01-01 00:00   node_modules/mysql2/Changelog.md
     1685  1980-01-01 00:00   node_modules/mysql2/index.js
     2203  1980-01-01 00:00   node_modules/mysql2/lib/auth_41.js
     2802  1980-01-01 00:00   node_modules/mysql2/lib/commands/binlog_dump.js
     1806  1980-01-01 00:00   node_modules/mysql2/lib/commands/change_user.js
     6652  1980-01-01 00:00   node_modules/mysql2/lib/commands/client_handshake.js
      414  1980-01-01 00:00   node_modules/mysql2/lib/commands/close_statement.js
     1087  1980-01-01 00:00   node_modules/mysql2/lib/commands/command.js
     3309  1980-01-01 00:00   node_modules/mysql2/lib/commands/execute.js
      254  1980-01-01 00:00   node_modules/mysql2/lib/commands/index.js
      878  1980-01-01 00:00   node_modules/mysql2/lib/commands/ping.js
     3619  1980-01-01 00:00   node_modules/mysql2/lib/commands/prepare.js
     8195  1980-01-01 00:00   node_modules/mysql2/lib/commands/query.js
      596  1980-01-01 00:00   node_modules/mysql2/lib/commands/quit.js
      712  1980-01-01 00:00   node_modules/mysql2/lib/commands/register_slave.js
     5272  1980-01-01 00:00   node_modules/mysql2/lib/commands/server_handshake.js
     6054  1980-01-01 00:00   node_modules/mysql2/lib/compile_binary_parser.js
     5854  1980-01-01 00:00   node_modules/mysql2/lib/compile_text_parser.js
     4432  1980-01-01 00:00   node_modules/mysql2/lib/compressed_protocol.js
     5313  1980-01-01 00:00   node_modules/mysql2/lib/connection_config.js
    26286  1980-01-01 00:00   node_modules/mysql2/lib/connection.js
     3403  1980-01-01 00:00   node_modules/mysql2/lib/constants/charset_encodings.js
  ...

@ahomatthew Can you check the indentation and declaration of forceInclude again in your serverless.yml? It could be that there is a tab instead of a space there or that a space is missing. Then the whole declaration would be just ignored. You also put it under custom? I'm quite sure that it has to be something in that area. And please check that it is really sls-webpack 3.1.0 that you use (check node_modules/serverless-webpack/package.json) or if yarn or npm locked it to 3.0.0

HyperBrain avatar Sep 29 '17 10:09 HyperBrain

@HyperBrain Still no dice, I do have some slightly different versions than you on some of the modules -- let me try downgrading those and see if that solves the problem.

ma-matterhorn avatar Sep 29 '17 13:09 ma-matterhorn

@HyperBrain I made all of my plugin versions the same as yours and now it's working. Also added a couple that you had that I didn't, not sure if those were required. Here is the diff in my package.json -- other than this I did not touch anything else.

package json diff

ma-matterhorn avatar Sep 29 '17 13:09 ma-matterhorn

@ahomatthew Thanks for the tests. That sounds really strange - I'm also quite sure that I did some tests even with webpack 3.6.0 and that worked too. It could be that there was a mismatch between the lock files (npm or yarn) and the actual package.json. We ran into this in the past too, so that our package-lock was broken and just led to very strange results.

From the diff itself it looks more or less like uncritical dependencies. The forceInclude evaluation is internally completely independent from the webpack compile results.

HyperBrain avatar Sep 29 '17 13:09 HyperBrain

For the ones you added,

    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",

I'm also not sure if they are really needed for your project (they were just part of the sample) and should only influence the transpile, but not the actual packaging.

HyperBrain avatar Sep 29 '17 13:09 HyperBrain

Ok, I have run into issues with yarn too in the past so, prior to making these changes, I removed my yarn.lock and the node_modules folder and ran yarn again. Let me try reverting the change and trying again and seeing if it is an issue with one of the module versions.

ma-matterhorn avatar Sep 29 '17 13:09 ma-matterhorn

I think we can just file this under random yarn bug and call it a day! Thanks for your help @HyperBrain

ma-matterhorn avatar Sep 29 '17 13:09 ma-matterhorn

Good to hear that, and yes, since the tools support this locking mechanisms they've become more robust in 90% of the cases but they got far worse for the remaining 10% 😄 . I'll close the issue now.

HyperBrain avatar Sep 29 '17 13:09 HyperBrain