Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug in backtick evaluation #694

Closed
casey opened this issue Oct 8, 2020 · 2 comments
Closed

Possible bug in backtick evaluation #694

casey opened this issue Oct 8, 2020 · 2 comments
Labels

Comments

@casey
Copy link
Owner

casey commented Oct 8, 2020

Quoting @kenden from #674 (comment):

I have another example where conditional logic would help.

Here, I would like to define TAG_HASH at the beginning of the justfile.
This variable is reused in many targets so I don't want to re-set it in each target: it should be set once.

# if running in jenkins, set the tag to the git sha of the folder, else, create a hash of the folder
FOLDER_HASH := `find docker/myfolder -type f | sort -u | xargs cat | md5sum | cut -c1-7`
FOLDER_GIT_SHA := `git log -1 --oneline docker/myfolder | cut -d' ' -f 1`
TAG_HASH := `[[ "{{JENKINS_URL}}" != "" ]] && echo {{FOLDER_GIT_SHA}} || echo {{FOLDER_HASH}}`

That last line is already not very readable, but {{JENKINS_URL}}, {{FOLDER_GIT_SHA}} and {{FOLDER_HASH}} are empty in the line above.
(because the variables inside {{ }} are not replaced by their values (this happens only at the beginning of the file, it works in targets. Could it be a bug?).

Thanks for posting this! Could you elaborate on what's happening here? Backticks in variables outside of recipes are evaluated before any recipe runs. Is that not happening?

@casey casey added the bug label Oct 8, 2020
@kenden
Copy link
Contributor

kenden commented Oct 8, 2020

Backticks in variables outside of recipes are evaluated before any recipe runs, yes, but it doesn't seem possible to use the result of this evalation inside other backticks. Here is a simpler example:

Here is a justfile:

var1 := 'Hello'
var2 := `echo World`
var3 := `echo {{var1}} {{var2}}!`

debug:

And the execution:

$ just --evaluate
var1 := "Hello"
var2 := "World"
var3 := "{{var1}} {{var2}}!"

I would have expected the result:

$ just --evaluate
var1 := "Hello"
var2 := "World"
var3 := "Hello World!"

@casey
Copy link
Owner Author

casey commented Oct 9, 2020

Ah, okay, gotcha. I would like to come up with a backwards compatible way to use interpolations inside of strings and backticks. I think copying python's f-strings would be a good idea:

var1 := 'Hello'
var2 := `echo World`
var3 := f`echo {{var1}} {{var2}}!`

Only strings and backticks prefixed with f for "format" would be interpolated. I updated #11 (from all the way back in 2016!) to track this.

@casey casey closed this as completed Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants