Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyclic imports #28

Open
moleike opened this issue Apr 9, 2021 · 0 comments
Open

Cyclic imports #28

moleike opened this issue Apr 9, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@moleike
Copy link
Owner

moleike commented Apr 9, 2021

Cyclic imports are valid and well defined:

$ cat a.jsonnet 
{
  a:: 'a',
  c: (import 'b.jsonnet').b,
}
$ cat b.jsonnet 
{
  b:: (import 'a.jsonnet').a,
}
$ /google/data/ro/teams/jsonnet/jsonnet a.jsonnet 
{
   "c": "a"
}

So are imports on self:

$ cat a.jsonnet 
{
  a:: 'a',
  c: (import 'a.jsonnet').a,
}
$ jsonnet a.jsonnet 
{
   "c": "a"
}

The problem in the code snippet in the description is that the object in "main.jsonnet" is recursively defined in a way where the recursion doesn't have a terminating condition (i.e. bottomless). It's equivalent to:

local a = a + {};
a

Which produces similar results:

$ jsonnet main.jsonnet 
RUNTIME ERROR: max stack frames exceeded.
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        main.jsonnet:1:11       thunk <a>
        ...

But there are plenty of valid ways to define a variable or set of varibles recursively that do bottom out. For example:

{
 x: {
   a: 1,
   y: $.y,
 },
 y: {
   x: $.x,
   a: 1,
 },
}.x.y.x.y.x.y.x.a
{
  a: 1,
  b: 1,
  fib: self {
    a: super.b,
    b: super.a + super.b,
  },
}.fib.fib.fib.fib.fib.a

Are both valid, terminating programs.

Originally posted by @mikedanese in google/go-jsonnet#353 (comment)

@moleike moleike added the bug Something isn't working label Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant