Repository navigation
Closed
Description
This is a Bug Report
Description
The serverless package command is currently broken when using the ${self:} serverless variable.
Use the following serverless.yml file to reproduce the issue with master by running serverless package
(The example is taken from #3208 (review))
service: self-reference
provider:
name: aws
runtime: nodejs4.3
custom:
newService: ${self:}
exportName: ${self:custom.newService.service}-export
functions:
hello:
handler: handler.hello
resources:
Outputs:
selfExport:
Value: 'A Value To Export'
Export:
Name: ${self:custom.exportName}
OptExport:
Value: 'Exported option variable'
Export:
Name: ${opt:}
EnvExport:
Value: 'Exported env variable'
Export:
Name: ${env:}Here's the stack trace:
root@14c148d98242:/app/self# ../../../bin/serverless package
Serverless: Packaging service...
Type Error ---------------------------------------------
Converting circular structure to JSON
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
Stack Trace --------------------------------------------
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at Utils.writeFileSync (/app/lib/classes/Utils.js:51:23)
at AwsPackage.saveServiceState (/app/lib/plugins/aws/package/lib/saveServiceState.js:32:27)
From previous event:
at Object.aws:package:finalize:saveServiceState [as hook] (/app/lib/plugins/aws/package/index.js:81:10)
at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
From previous event:
at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
at PluginManager.spawn (/app/lib/classes/PluginManager.js:246:17)
at AwsPackage.BbPromise.bind.then (/app/lib/plugins/aws/package/index.js:69:51)
From previous event:
at Object.package:finalize [as hook] (/app/lib/plugins/aws/package/index.js:69:10)
at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
at PluginManager.run (/app/lib/classes/PluginManager.js:253:17)
at Serverless.run (/app/lib/Serverless.js:97:31)
at serverless.init.then (/app/bin/serverless:23:50)
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless
Your Environment Information -----------------------------
OS: linux
Node Version: 7.9.0
Serverless Version: 1.11.0
Activity
HyperBrain commentedon Apr 26, 2017
Can anyone point out the exact use case for creating such a self reference? Imo it just creates an object reference to the service object, which is accessible anyway.
pmuens commentedon Apr 26, 2017
@HyperBrain thanks for chiming in.
AFAIK @erikerikson uses it in the Nordstrom "hello retail" project.
You can find more about this use case in the following PR: #3208
HyperBrain commentedon Apr 26, 2017
Ok. As I see, the ${self:} reference adds an object reference into the service hierarchy. That's the reason why the service cannot be persisted in this case to be transferred from package to deploy.
Before PR #3208 the service state was immutable and did not have any cross object references in there as only _.deepClone() was used to assemble the variable substitutions.
One possible solution would be to parse the complete service tree before persisting the state, doing a "reverse" variable substitution to eliminate the ${self:}, ${env:} and ${opt:} references as all of them break persistig the state. On state restoration the variables then should be substituted again with the object references.
The detection of the ref'd objects on state save should be quite easy as a simple
prop === service,prop === environmentandprop === optionswould yield true as the object references are equal and point to the same object.erikerikson commentedon Apr 26, 2017
@HyperBrain - pretty sure only the ${self:} reference will have to be detected. The rest aren't circular.
HyperBrain commentedon Apr 26, 2017
@erikerikson Just started working on that. PR should be ready soon
pmuens commentedon Apr 27, 2017
Super nice @HyperBrain 👍 Will review it ASAP.
Thanks @erikerikson for the clarifications!