-
Notifications
You must be signed in to change notification settings - Fork 27
Fix process.env._HANDLER undefined #30
base: master
Are you sure you want to change the base?
Fix process.env._HANDLER undefined #30
Conversation
|
Can you confirm that this works when there are multiple functions and the target function isn't the first one? |
|
Yes, I'll do it tomorrow. Also I'm going to try with |
This reverts commit 65ebd20.
…s into fix-process.env._HANDLER
|
@azurelogic So the best approach I found was to inject Maybe @HyperBrain could give a quick look here too? |
plugin/index.js
Outdated
|
|
||
| // process.env.IS_LOCAL === 'true' is set when called by 'sls invoke local' | ||
| if (process.env._HANDLER === undefined && process.env.IS_LOCAL === 'true') { | ||
| const invokedFunction = functions[this.serverless.processedInput.options.function] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this.options already the processed option object given to the plugin? If that's the case using this.options would decouple the implementation from Serverless internals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap.
But there is a little difference.
If I call sls invoke local --function some-function:
this.options {
"function": "some-function"
}
this.serverless.processedInput.options {
"commands": [
"invoke",
"local"
],
"options": {
"function": "some-function"
}
}And If I call sls invoke local -f some-function:
this.options {
"f": "some-function"
}
this.serverless.processedInput.options {
"commands": [
"invoke",
"local"
],
"options": {
"f": "some-function",
"function": "some-function"
}
}So using this.serverless.processedInput.options we always have the function key.
In any case I could also do something like this:
const invokedFunction = functions[this.options.f || this.options.function]What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm... That's strange also serverless-webpack uses this.options.function throughout the whole implementation. I was quite sure that the pluginManager would populate the long version in any case (regardless if you specify -f or --function). At least that was the handling for a long time.
However, the const invokedFunction = ... looks good otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HyperBrain
You're right.
I was calling JSON.stringify inside the plugin constructor.
But if I call it later then the results are:
"options": {
"f": "some-function",
"function": "some-function"
}It looks like serverless normalize it somehow, but I didn't take a deep look why this is happening.
So it's safe to use this.options.function as long as you are not using this in the plugin constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why it is not yet normalized in the constructor is, that Serverless first loads the plugins and then starts the lifecycles of the invoked command. One of Serverless' lifecycle steps is the normalization (most likely in validate), but that can only be run as soon as Serverless knows the command (and thus the supported options, declared in the command structure).
So the same is true for any plugin - resolved and initialized properties (like options) are only available within the hooks, but not the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see.
Thanks for the explanation @HyperBrain
|
In general, I think for the invoke local case, this would work (although it should be tested thoroughly first). |
|
@azurelogic |
|
Any news here? |
|
Waiting for this PR to be merged.. |
Fixes #22