Skip to content

Detect cyclic dependencies in Serverless Variable system #3711

Description

@pmuens

This is a Feature Proposal

Description

Given the fact that we're recursively resolving the variables it might be possible that developers create cyclic dependencies when using the variable system.

Serverless should detect those dependencies and throw an error if those are found.

One example can be found here.

originally brought up by @sammarks in #3627 (comment)

/cc @eahefnawy @brianneisler

Activity

  1. sammarks commented on May 31, 2017

    @sammarks
    Contributor

    Since it appears the cyclic dependencies are a requirement in some configuration cases, what if instead we broke the variable loading into several phases:

    • First load the file dependencies
    • Then load other dependencies...
    • Finally, load the self references.

    This would allow us to use self references inside included configuration files, as the files will have already been loaded and cached by the time the self reference is evaluated, meaning we don't get into an infinite loop between the self reference and the file import.

    And since I can see this functionality being incredibly useful in other packages, have you considered breaking out the configuration loading into another package and then just referencing it here?

  2. pmuens commented on Jun 1, 2017

    @pmuens
    ContributorAuthor

    Thank you very much for the feedback @sammarks 👍

    what if instead we broke the variable loading into several phases:

    • First load the file dependencies
    • Then load other dependencies...
    • Finally, load the self references.

    That sounds like a really good plan to me. Love the caching. This would also improve the readability of the code a lot. 👍

    Have you considered breaking out the configuration loading into another package and then just referencing it here?

    Do you mean an npm package? I'm more in favor of keeping this into core. Any thoughts @eahefnawy @brianneisler ?

  3. sammarks commented on Jun 1, 2017

    @sammarks
    Contributor

    Thanks @pmuens! And yep, I meant a separate npm package. I'll leave the internal discussions to you all, just thought that specific functionality would be very useful for the community at large 😎

  4. pmuens commented on Jun 2, 2017

    @pmuens
    ContributorAuthor

    Thanks for getting back @sammarks 👍

    just thought that specific functionality would be very useful for the community at large 😎

    That's a valid argument! We'll do our best to make it as accessible as possible. 👍

  5. brianneisler commented on Jun 2, 2017

    @brianneisler
    Contributor

    Unfortunately the order is not so simple. Our variable system can have nested variables and variables that depend on other variables which prevents any logical ordering based on type. They really need to be evaluated in the order they are encountered. I think we'll need to find a better way of dealing with cyclical dependencies.

    Usually when evaluating a dependency graph each node should stored in a map and then the map is checked to see if the node has already been evaluated. It seems we need to do the same here by storing each promise in a map and reusing the existing promise. This should solve any issues with cyclical dependencies.

  6. pmuens commented on Jun 3, 2017

    @pmuens
    ContributorAuthor

    Unfortunately the order is not so simple. Our variable system can have nested variables and variables that depend on other variables which prevents any logical ordering based on type.

    Right, haven't thought about that case. 🤔

    But I'm also in favor to untangle the Variables.js code a little bit and try to write it in a more "functional" way so that it's easier to reason about and extend later on.

    Usually when evaluating a dependency graph each node should stored in a map and then the map is checked to see if the node has already been evaluated.

    Really like this approach since it feels natural for such a use case.

  7. sammarks commented on Jun 6, 2017

    @sammarks
    Contributor

    I've done something similar in the past and found that a topological sort of the graph works nicely for figuring out which dependencies should be resolved first. I'm not sure if the topological sort with the circular dependencies will cause the sorting algorithm to go into a loop as I'm not familiar with the math behind it. If we can get it to spit out the dependencies in the correct order still, we can just cache the results for each key. If we come across a cached key, we can just return the cached value.

  8. added this to the 1.25 milestone on Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions