2

I feel like I don't understand how Jsonnet imports work.

There are numerous references to importing being akin to copy-and-pasting. For example Jsonnet tutorial says "The import construct is like copy/pasting Jsonnet code" and Tanka's tutorial has a clarification at one point:

It might seem odd at first sight, that this code works, because grafana.jsonnet still refers to the root object using $, even though it is outside of the file's scope. However, Jsonnet is lazy-evaluated which means that the contents of grafana.jsonnet are first "copied" into main.jsonnet (the root object) and then evaluated. This means the above code actually consists of all three objects joined to one big object, which is then converted to JSON.

This leads me to believe that given a main.jsonnet like this:

{
  config: {},
  data: import "data.jsonnet"
}

and a data.jsonnet in the same directory like this:

{
  check: $.config,
}

running jsonnet main.jsonnet should not result in a Field does not exist: config error. However, it does and I can't quite understand why. What works instead is

(import "data1.jsonnet") +
{
  config: {},
}

which doesn't feel like it's different in semantics (except for the data key, which can be moved to the imported file), but it clearly affects the importing.

At the same time, there is a comment in the imported file in Tanka's tutorial which is not explained further:

{
  // DO NOT use the root level here.
  // Include the grafana subkey, otherwise $ won't work.
  grafana: {
  [...snip...]
  }
}

which again leads me to believe that importing is not quite the copy-and-pasting it's supposed to be.

So how does importing really work in Jsonnet and why can root references fail in imported files?

Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
  • @sbarzowski If you have time, I came to this question having it myself, and would welcome to your jsonnet wisdom on the topic. – David Aug 10 '23 at 17:46

0 Answers0