conftest icon indicating copy to clipboard operation
conftest copied to clipboard

Jsonnet parser cannot handle relative imports

Open coord-e opened this issue 1 year ago • 4 comments

Jsonnet has an import construct to load other jsonnet files. The following configuration successfully evaluates as follows:

# config/main.jsonnet
local data = import './data.libsonnet';
{
  data: data
}
# config/data.libsonnet
{
  hello: "world"
}
$ jsonnet config/main.jsonnet
{
   "data": {
      "hello": "world"
   }
}

However, conftest cannot handle relative imports (in directories other than .).

$ conftest test config/main.jsonnet
Error: running test: parse configurations: parser unmarshal: evaluate anonymous snippet: RUNTIME ERROR: couldn't open import "./data.libsonnet": no match locally or in the Jsonnet library paths
        1:14-39 thunk <data> from <$>
        3:9-13  object <anonymous>
        Field "data"
        During manifestation
, path: config/main.jsonnet

Although the specification seems not specifying how the import-ed relative paths are resolved, most jsonnet implementations handle these paths as if they're relative to the import-ing file.

We can work around this by jsonnet config/main.jsonnet | conftest test -, but native support is better in context of data.conftest.file support, nicer error reportings, etc.

Version information

$ conftest -v
Conftest: 0.51.0
OPA: 0.63.0

coord-e avatar Apr 11 '24 04:04 coord-e

It seems the current implementation uses the EvaluateAnonymousSnippet function which is likely the reason it does not attempt to import other files. EvaluateFile also exists which likely has the desired behavior but doesn't work well with the conftest's parser interface. Additional investigation and testing will be needed before we can add this.

jalseth avatar Apr 17 '24 00:04 jalseth

After some testing, it looks like the import path in the jsonnet config is relative to $PWD, not main.jsonnet. If you import ./config/data.libsonnet it works as expected.

jalseth avatar Apr 30 '24 02:04 jalseth

it looks like the import path in the jsonnet config is relative to $PWD, not main.jsonnet. If you import ./config/data.libsonnet it works as expected.

that is indeed behavior of conftest's jsonnet parser, but isn't that of the most jsonnet CLI implementation (such as google/go-jsonnet, google/jsonnet, etc). my request is to align conftest's import behavior to accept the exactly same jsonnet configuration as the known existing jsonnet implementation accepts.

coord-e avatar Apr 30 '24 04:04 coord-e

I would welcome a PR.

jalseth avatar Apr 30 '24 04:04 jalseth

I would welcome a PR.

I have a proposal in #1044 :+1:

thevilledev avatar Jan 20 '25 19:01 thevilledev