Skip to content

Converting circular structure to JSON when using package command with self reference #3509

Closed

Description

@pmuens

This is a Bug Report

Refs #3344 and #3467

/cc @HyperBrain @eahefnawy

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

  1. added this to the 1.12 milestone on Apr 24, 2017
  2. HyperBrain commented on Apr 26, 2017

    @HyperBrain
    Contributor

    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.

  3. pmuens commented on Apr 26, 2017

    @pmuens
    ContributorAuthor

    @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

  4. modified the milestones: 1.13, 1.12 on Apr 26, 2017
  5. HyperBrain commented on Apr 26, 2017

    @HyperBrain
    Contributor

    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 === environment and prop === options would yield true as the object references are equal and point to the same object.

  6. erikerikson commented on Apr 26, 2017

    @erikerikson
    Contributor

    @HyperBrain - pretty sure only the ${self:} reference will have to be detected. The rest aren't circular.

  7. HyperBrain commented on Apr 26, 2017

    @HyperBrain
    Contributor

    @erikerikson Just started working on that. PR should be ready soon

  8. pmuens commented on Apr 27, 2017

    @pmuens
    ContributorAuthor

    Super nice @HyperBrain 👍 Will review it ASAP.

    Thanks @erikerikson for the clarifications!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions