invoke local --watch is not working
crysislinux opened this issue · comments
This is a Bug Report
Description
-
What went wrong?
start with
sls invoke local --function some-function --watch
when change the code, it recompiles the code, but it still seems invoke the old compiled result -
What did you expect should have happened?
It should run the latest compiled result just as what the documentation says. -
What was the config you used?
service: some-service
plugins:
- serverless-webpack
- serverless-offline
custom:
webpack:
webpackIncludeModules: true
package:
individually: true
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: us-east-2
functions:
some-function:
handler: some-function/handler.handler
the second time it should output 5 instead of 6 because the code has changed.
Similar or dependent issue(s):
Additional Data
- Serverless-Webpack Version you're using: 5.1.0
- Webpack version you're using: 4.1.1
- Serverless Framework Version you're using: 1.26.1
- Operating System: macOs High Sierra 10.13.3
- Stack Trace (if available):
I used to use --watch with sls offline start --watch
. It worked. This time I don't have a http event, so it's invoked directly, but it does not work.
@crysislinux Thanks for reporting 👍
It seems that Webpack 4 changed something with its watch mode. This comment #279 (comment) also points to that because for @stormit-vn it looks like the issue with watch happens with serverless-offline too.
This should be investigated, to see what exactly changed on Webpack's side, so that we can implement a proper fix.
Just to be completely sure. Can you try with serverless-webpack 5.0.0 and Webpack 3, to see if it is really a change in Webpack 4 that triggers the problem? If yes, we should put "Webpack 4" into the issue subject.
Further question: Did you set the new Webpack mode option to "development" for local invocations in your webpack.config? This enables debug functionality for the built output.
mode: slsw.lib.webpack.isLocal ? "development" : "production"
@HyperBrain Thanks for the quick response.
I tried with the following combinations
- serverless-webpack 5.1.0 webpack 3
- serverless-webpack 5.1.0 webpack 4
- serverless-webpack 4 webpack 3
It seems that sls invoke local -f some-function --watch
does not work for all of the combinations.
But it do work with sls offline start --watch
, by "work" I mean when send a http request to the endpoint, it will response with the latest code ("good" changes to "good1").
And yes, I have this in webpack.config.js
mode: slsw.lib.webpack.isLocal ? "development" : "production"
Ok. Then it looks like a general issue restricted to invoke local --watch
. This is a bug then and has to be fixed.
The problem might be due to the require cache. Invoke local
requires our handler and we do not terminate the Serverless process when running in watch mode. On a watch trigger, invoke local
is executed again - and fetches our module under test from the require cache.
A solution would be to make sure that the invocation fetches a fresh module from disk.
The code of the second answer here would make it possible to remove the old cached module and let the next invocation start freshly.
https://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
@crysislinux Can you check with #351 if that works for you? I did some local tests and it seems that the handler is now loaded again correctly after a watch compile.
@HyperBrain I am really sorry that I provide some misunderstanding information. I tried again and found that the bug happens only when
package:
individually: true #enable this
I tried your fix, it magically fix this problem anyway 😃