Mimic --tla-code
behavior from jsonnet for functions in main.jsonnet during envrionment discovery
#1251
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Some parts of tanka behave inconsistently with standard jsonnet when using inline environments with top level arguments. The differences can be illustrated by this inline environment:
main.jsonnet
And then running:
which finds no environments. Similarly running:
will export no manifests. For both commands, appending
--tla-code foo=1
will resolve the issue, listing the environment and exporting manifests.If instead main.jsonnet is a function taking no arguments, eg:
main.jsonnet
Then there is no combination of arguments that will cause
tk env list
ortk export --recursive
to succeed. It will either do nothing or fail when a--tla-code
argument is provided.Expectation
These
tk env list
andtk export
should permit the same flexibility as underlying jsonnet, namely that:--tla-code
arguments.tk env list main.jsonnet --tla-code badargument=0
on a mainfaile that isn't a function ignores the tla-code argument.This is the behavior I'd expect to see from
tk
, because its also how callingjsonnet
works.My current workaround for this is to always add a dummy
unused
TLA to my main.jsonnet and ensure that all my tk commands are run with--tla-code unused=0
, which is cumbersome.Cause
The root cause of this appears to be in how the EvalScript for finding environments is constructed. The presence of any
--tla-code
argument is used to determine if main.jsonnet should be used called as a function or not. This decision logic behaves incorrectly if a--tla-code
is provided with a non-function main.jsonnet or if no--tla-code
argument is provided to a main.jsonnet that has no required arguments.Solution in this PR
Before running an EvalScript,
jsonnet.Evaulate
is called withstd.isFunction
on the imported mainfile to determine if it needs to be called as a function. This adds an extra evaluation every time an EvalScript is used, but since thats typically only done once pertk
call and thestd.isFunction
doesn't manifest anything except a boolean, I'd expect it to have a minimal impact on performance.