google / go-jsonnet

Apache License 2.0
1.61k stars 231 forks source link

`--jpath` does not support multiple search paths #744

Closed skuzzle closed 3 months ago

skuzzle commented 7 months ago

Hi,

I was surprised by this behavior, also the console help text is not very precise regarding whether multiple search paths are even allowed or not.

Given the following directory structure:

project/
├─ vendor1/
│  ├─ lib1/
│  │  ├─ shared1.txt    // content: "shared1"
├─ vendor2/
│  ├─ lib2/
│  │  ├─ shared2.txt    // content: "shared2"

Passing multiple library directories via JSONNET_PATH seems to work as expected, properly resolving files from both library locations:

JSONNET_PATH=$(pwd)/vendor1:$(pwd)/vendor2 jsonnet -e "(importstr 'lib1/shared1.txt')+(importstr 'lib2/shared2.txt')"

> "shared1shared2"

However, the same with the -J or --jpath argument does not work:

jsonnet --jpath $(pwd)/vendor1:$(pwd)/vendor2 -e "(importstr 'lib1/shared1.txt')+(importstr 'lib2/shared2.txt')" 

RUNTIME ERROR: couldn't open import "lib1/shared1.txt": no match locally or in the Jsonnet library paths
    <cmdline>:1:2-30    $
    During evaluation   

Tested with

jsonnet -version

> Jsonnet commandline interpreter (Go implementation) v0.20.0

Quick reproducer project with the example from above can be found here: https://github.com/skuzzle/jpath-issue-reproducer

zarelit commented 5 months ago

@skuzzle you can specify the option multiple times like jsonnet -J $(pwd)/vendor1 -J $(pwd)/vendor2, in case the file is present in multiple directories the last one wins.

Issuing jsonnet --help prints an example of how JSONNET_PATH env variable and -J options interact together

Environment variables: JSONNET_PATH is a colon (semicolon on Windows) separated list of directories added in reverse order before the paths specified by --jpath (i.e. left-most wins). E.g. these are equivalent: JSONNET_PATH=a:b jsonnet -J c -J d JSONNET_PATH=d:c:a:b jsonnet jsonnet -J b -J a -J c -J d

skuzzle commented 5 months ago

Oh, not sure how I could miss that. Thanks for pointing this out. Anyway, I wonder whether there'd be some value in -J accepting multiples paths in a single parameter? To me this would at least feel a bit more consistent if -J and JSONNET_PATH accepted the same values

zarelit commented 5 months ago

AFAICT from the code it doesn't look like it, but I'll leave the actual answer to the devs

sbarzowski commented 3 months ago

Anyway, I wonder whether there'd be some value in -J accepting multiples paths in a single parameter?

No, you just specify -J option multiple times.