grafana / tanka

Flexible, reusable and concise configuration for Kubernetes
https://tanka.dev
Apache License 2.0
2.39k stars 165 forks source link

Import finds files that don't match the specified path #673

Open curusarn opened 2 years ago

curusarn commented 2 years ago

Steps to reproduce

We are creating following directory structure:

❯ tree tanka-import-bug 
tanka-import-bug
└── tanka-root
    ├── a.jsonnet
    ├── main.jsonnet
    └── tkrc.yaml

1 directory, 3 files

And these files:

❯ cat tanka-import-bug/tanka-root/main.jsonnet
{
"import": import "../a.jsonnet",
"relative_import": import "./../a.jsonnet",
}

❯ cat tanka-import-bug/tanka-root/a.jsonnet   
{ x: "this should not work imho" }

Setup steps

cd /tmp
mkdir -p tanka-import-bug/tanka-root
touch tanka-import-bug/tanka-root/tkrc.yaml
echo -e '{\n"import": import "../a.jsonnet",\n"relative_import": import "./../a.jsonnet",\n}' > tanka-import-bug/tanka-root/main.jsonnet
echo '{ x: "this should not work imho" }' > tanka-import-bug/tanka-root/a.jsonnet
cd tanka-import-bug/tanka-root

The issue

Evaluate with jsonnet for reference:

/tmp/tanka-import-bug/tanka-root
❯ jsonnet main.jsonnet
RUNTIME ERROR: couldn't open import "../a.jsonnet": no match locally or in the Jsonnet library paths.
    main.jsonnet:2:11-32    object <anonymous>
    During manifestation    

❯ jsonnet --version
Jsonnet commandline interpreter v0.17.0

You can see that the import fails as expected because the relative path should be ./a.jsonnet (or equivalent) not ./../a.jsonnet.

Evaluate with tanka:

/tmp/tanka-import-bug/tanka-root
❯ PAGER=cat tk eval main.jsonnet
{
  "import": {
    "x": "this should not work imho"
  },
  "relative_import": {
    "x": "this should not work imho"
  }
}

❯ tk --version
tk version v0.15.1

Here we see that something weird is happening when jsonnet is evaluated in tanka. I originally thought that this is caused by paths that are added by Tanka into "jsonnetpath". But when you look above you can see that even relative import import "./../a.jsonnet" evaluates without errors. I assumed that the relative import starting with ./ is strictly relative to current file but that does not seem to be the case.

Let me know if anything is unclear of if you can't reproduce the issue.

Simon Let

Duologic commented 2 years ago

This is a peculiar edge case, for sure.

I also get a result with Jsonnet when doing this:

➜ jsonnet -J a.jsonnet main.jsonnet
{
   "import": {
      "x": "this should not work imho"
   },
   "relative_import": {
      "x": "this should not work imho"
   }
}

Not sure if bug or feature. :thinking:

curusarn commented 2 years ago

Some extra observations:

Ad. "your example how to replicate this with just jsonnet": It's definitely surprising to me that "jsonnetpath" is used even for relative imports that start with ./.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

curusarn commented 2 years ago

not stale

zerok commented 5 months ago

Can still reproduce this with v0.26.0.