Skip to content

Add Cyclic Object Serialization Support - #4146

Merged
pmuens merged 5 commits into
serverless:circular-varsfrom
erikerikson:circular-vars-cycle-fix
Sep 8, 2017
Merged

pmuens merged 5 commits into
serverless:circular-varsfrom
erikerikson:circular-vars-cycle-fix

Conversation

@erikerikson

Copy link
Copy Markdown
Contributor

What did you implement:

Re-Closes #3509

Allow for the serialization and deserialization of cyclic objects being written to file using json-decycle. (in support of #4144)

How did you implement it:

Add json-decycle and use and instance of it as the second parameter for JSON.stringify/JSON.parse for file writing and reading.

How can we verify it:

A project with a ${self:} reference should be sufficient to evoke the issue. During serialization you would get the result:

[...] $SLS_DEBUG=* slsDev deploy
Serverless: Packaging service...
    Serverless: Excluding development dependencies...

    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 Object.stringify (native)
at writeFileSync (~/serverless/lib/utils/fs/writeFileSync.js:13:21)
at Utils.writeFileSync (~/serverless/lib/classes/Utils.js:45:12)
at AwsPackage.saveServiceState (~/serverless/lib/plugins/aws/package/lib/saveServiceState.js:39:27)
From previous event:
    at Object.aws:package:finalize:saveServiceState [as hook] (~/serverless/lib/plugins/aws/package/index.js:81:10)
at BbPromise.reduce (~/serverless/lib/classes/PluginManager.js:242:55)
From previous event:
    at PluginManager.invoke (~/serverless/lib/classes/PluginManager.js:242:22)
at PluginManager.spawn (~/serverless/lib/classes/PluginManager.js:254:17)
at AwsPackage.BbPromise.bind.then (~/serverless/lib/plugins/aws/package/index.js:69:51)
From previous event:
    at Object.package:finalize [as hook] (~/serverless/lib/plugins/aws/package/index.js:69:10)
at BbPromise.reduce (~/serverless/lib/classes/PluginManager.js:242:55)
From previous event:
    at PluginManager.invoke (~/serverless/lib/classes/PluginManager.js:242:22)
at PluginManager.spawn (~/serverless/lib/classes/PluginManager.js:254:17)
at Deploy.BbPromise.bind.then.then (~/serverless/lib/plugins/deploy/deploy.js:101:50)
From previous event:
    at Object.before:deploy:deploy [as hook] (~/serverless/lib/plugins/deploy/deploy.js:99:10)
at BbPromise.reduce (~/serverless/lib/classes/PluginManager.js:242:55)
From previous event:
    at PluginManager.invoke (~/serverless/lib/classes/PluginManager.js:242:22)
at PluginManager.run (~/serverless/lib/classes/PluginManager.js:261:17)
at variables.populateService.then (~/serverless/lib/Serverless.js:99:33)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at Serverless.run (~/serverless/lib/Serverless.js:86:74)
at serverless.init.then (~/serverless/bin/serverless:39: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:                     darwin
Node Version:           6.11.0
Serverless Version:     1.20.2

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@erikerikson

Copy link
Copy Markdown
Contributor Author

you may want to reject the non-targeted package-lock.json changes

@eahefnawy

Copy link
Copy Markdown
Contributor

@erikerikson thanks for that! if it's not much of a hassle, could you add unit test for the stringify util function? I think just a test that ensures circular object is correctly stringified or at least that the decycle package is used. Since other utils use that function, this test would cover them as well.

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @erikerikson 👍

I'm not sure if Travis is lying here but the json-decycle package has a dependency on ES6 features (https://github.com/YChebotaev/json-decycle#es6-features).

Is there any chance to remove the package and e.g. copy and paste over the parts which are necessary as util functions?

This way we'd also remove one more dependency from our (already huge) dependencies list.

@erikerikson

Copy link
Copy Markdown
Contributor Author

Both of these requests seem reasonable enough

@pmuens

pmuens commented Aug 25, 2017 •

Copy link
Copy Markdown
Contributor

Both of these requests seem reasonable enough

Thanks @erikerikson 👍. Do you have time to fix this or should we jump in here and help with this PR?

@erikerikson

Copy link
Copy Markdown
Contributor Author

I certainly hope I do. The responsibilities...

I will not be offended if you do the work but will do it if I can find time to prioritize.

@erikerikson

Copy link
Copy Markdown
Contributor Author

@eahefnawy will probably have to fix the other issue noted on his PR though... I haven't been able to dive deeply enough to resolve the core issue there yet.

@pmuens

pmuens commented Aug 25, 2017 •

Copy link
Copy Markdown
Contributor

Alright! Thanks for the update @erikerikson 👍

@pmuens

pmuens commented Aug 28, 2017 •

Copy link
Copy Markdown
Contributor

@erikerikson I just look into this and found the json-cycle package which does exactly the same as json-decycle but w/o ES2016 syntax (plus it seems to be actively maintained and has 0 dependencies (FWIW json-decycle had 0 dependencies as well)).

I updated the PR so that this package is used rather than json-decycle.

Could you share a serverless.yml file so that we can write a corresponding unit test for this? I tried the example service in #4144 but was not able to force this issue to pop up.

Thanks in advance 👍!

@pmuens

pmuens commented Aug 30, 2017 •

Copy link
Copy Markdown
Contributor

Just a quick update on this one.

I tested @eahefnawy PR #4144 with this service setup (this issue was linked in the PR description) which should trigger the error described here.

(Un)fortunately I was not able to reproduce it since the packaging works fine when using #4144. Maybe @eahefnawy added something in the meantime which fixed this "circular JSON structure" as well?

add parse self-reference test
add writeFile self-reference test
add writeFileSync self-reference test
Comment thread lib/utils/fs/writeFile.js Outdated
.then(() => {
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = JSON.stringify(contents, null, 2);
contents = jc.stringify(contents);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the 2nd and 3rd parameters are not recognized by json-cycle so it doesn't yet make sense to pass them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolving this pends upon the acceptance of valery-barysok/json-cycle#4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikerikson can't we just pass the values in for now (although they're ignored)? This way we could bump the package version number later on and it would work OOTB without the need to touch the code here again.

Comment thread lib/utils/fs/writeFileSync.js Outdated

if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = JSON.stringify(contents, null, 2);
contents = jc.stringify(contents);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the 2nd and 3rd parameters are not recognized by json-cycle so it doesn't yet make sense to pass them

@erikerikson

Copy link
Copy Markdown
Contributor Author

@eahefnawy Tests have been added for all the affected modules
@pmuens there was a little fix to make in your use of json-cycle but that's working now and passing the given tests. It's really unclear how this issue will have been resolved by the changes of #4144 but I can try to make time to test this.
Thanks for your attention!

@pmuens

pmuens commented Sep 6, 2017

Copy link
Copy Markdown
Contributor

@eahefnawy Tests have been added for all the affected modules
@pmuens there was a little fix to make in your use of json-cycle but that's working now and passing the given tests. It's really unclear how this issue will have been resolved by the changes of #4144 but I can try to make time to test this.
Thanks for your attention!

Great! Thanks for updating the PR @erikerikson 👍. We'll give it another spin the upcoming days!

@eahefnawy eahefnawy added this to the 1.22 milestone Sep 6, 2017

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikerikson thanks for updating this PR 👍

I just gave it a spin today with the following service config (the one taken from the linked issue in the PR description):

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:}

In master the packaging process fails.

This PR seems to fix the problem. But I tested the exact same service with #4144 and #4144 solves the problem as well. If both solve the same problem then I'd be in favor of #4144 because it's a smaller fix (in terms of LOC) and doesn't introduce another dependency.

Other downside of json-cycle right now is that the JSON content is not formatted in a pretty way. But this is something which needs to be done in the json-cycle package (as @erikerikson already mentioned). I just left a comment for a potential code update there.

Comment thread lib/utils/fs/writeFile.js Outdated
.then(() => {
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = JSON.stringify(contents, null, 2);
contents = jc.stringify(contents);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikerikson can't we just pass the values in for now (although they're ignored)? This way we could bump the package version number later on and it would work OOTB without the need to touch the code here again.

@erikerikson

erikerikson commented Sep 8, 2017 •

Copy link
Copy Markdown
Contributor Author

Hi @pmuens I have tried this out. I observed the same as you with a simple script. So, I looked closer...

First: here's the code that "solved" the issue for this PR: https://github.com/serverless/serverless/blob/circular-vars/lib/plugins/aws/package/lib/saveServiceState.js#L28

However, when I run this code against https://github.com/Nordstrom/hello-retail/blob/master/product-catalog/builder/serverless.yml, I get the reported problem:

$SLS_DEBUG=* slsDev package
 
 Serverless Warning --------------------------------------
 
  A valid file to satisfy the declaration 'file(../private.yml)' could not be found.
 
 
 Serverless Warning --------------------------------------
 
  A valid file to satisfy the declaration 'file(../helloRetail.yml)' could not be found.
 
Serverless: Packaging service...
Serverless: Excluding development dependencies...
 
  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 Object.stringify (native)
    at writeFileSync (~/serverless/lib/utils/fs/writeFileSync.js:13:21)
    at Utils.writeFileSync (~/serverless/lib/classes/Utils.js:45:12)
    at AwsPackage.saveServiceState (~/serverless/lib/plugins/aws/package/lib/saveServiceState.js:39:27)
From previous event:
    at Object.aws:package:finalize:saveServiceState [as hook] (~/serverless/lib/plugins/aws/package/index.js:81:10)
    at BbPromise.reduce (~/serverless/lib/classes/PluginManager.js:242:55)
From previous event:
    at PluginManager.invoke (~/serverless/lib/classes/PluginManager.js:242:22)
    at PluginManager.spawn (~/serverless/lib/classes/PluginManager.js:254:17)
    at AwsPackage.BbPromise.bind.then (~/serverless/lib/plugins/aws/package/index.js:69:51)
From previous event:
    at Object.package:finalize [as hook] (~/serverless/lib/plugins/aws/package/index.js:69:10)
    at BbPromise.reduce (~/serverless/lib/classes/PluginManager.js:242:55)
From previous event:
    at PluginManager.invoke (~/serverless/lib/classes/PluginManager.js:242:22)
    at PluginManager.run (~/serverless/lib/classes/PluginManager.js:261:17)
    at variables.populateService.then (~/serverless/lib/Serverless.js:99:33)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at Serverless.run (~/serverless/lib/Serverless.js:86:74)
    at serverless.init.then (~/serverless/bin/serverless:39: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:                     darwin
     Node Version:           6.11.0
     Serverless Version:     1.20.2

If I switch to #4146, the error does not occur.

Probably there is some bug here:
https://github.com/serverless/serverless/blob/circular-vars/lib/plugins/aws/utils/findReferences.js

.. but I haven't looked deeper yet and need to sleep.

@erikerikson

erikerikson commented Sep 8, 2017 •

Copy link
Copy Markdown
Contributor Author

Additional. I opened a PR against json-cycle that has been accepted. I'll update #4146

@erikerikson

Copy link
Copy Markdown
Contributor Author

(PR: valery-barysok/json-cycle#4)

use pretty-print capability
update tests
@erikerikson

Copy link
Copy Markdown
Contributor Author

edits 4144 => 4146 as appropriate.

Latest commit includes use of pretty printing and updates to tests

Should be GTM

@erikerikson

Copy link
Copy Markdown
Contributor Author

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the in-depth description and steps to reproduce @erikerikson 🥇! That was super helpful 👍

I was able to reproduce this and could validate that #4144 won't fix the issue with circular JSON structures.

I tested this thoroughly and it's GTM 💯

Thanks for taking the time to work on and fix this @erikerikson 💪

@pmuens
pmuens merged commit 2629672 into serverless:circular-vars Sep 8, 2017
@erikerikson
erikerikson deleted the circular-vars-cycle-fix branch December 5, 2017 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants