just icon indicating copy to clipboard operation
just copied to clipboard

Guard sigil

Open casey opened this issue 1 year ago • 5 comments

We have @ and - sigils which control linewise recipe execution. I thought that we could consider having a "guard" sigil which indicates that an error on a particular line should terminate the execution of the current recipe, but not the whole run.

For example, to make a recipe only execute if the environment variable FOO is set to yes:

foo:
  ?[[ $FOO == yes]]
  # the rest of the recipe

This is backwards incompatible, so would need a setting to opt-in to the new behavior. (Although it's unlikely to break most justfile, ? at the beginning of the line is unlikely in sh and derivatives, and I think most scripting languages that people are likely to use.)

casey avatar Dec 22 '24 21:12 casey

A further enhancement that would be nice is a way for the condition to be evaluated without invoking the shell e.g.:

foo:
  ?{{ assert(foo == 'yes') }}
  # the rest of the recipe

liquidaty avatar Dec 24 '24 20:12 liquidaty

@liquidaty There would have to be a different syntax for this, since I believe the pattern of having an interpolation first is common.

For example, if someone wanted to use Python to perform a test, they might do:

python := '/usr/bin/python3'

foo:
  ?{{ python }} …

casey avatar Dec 24 '24 20:12 casey

There would have to be a different syntax for this

Got it. Any syntax would be fine by me, but as I'm a new just user, that doesn't count for much.

That said, if it can be done, then, it seems logical (to me at least) to allow {{ assert(...) }} inside the recipe (and if it wasn't allowed before, it wouldn't seem to cause any compatibility issues to allow it). Currently, I can do this outside of a recipe:

a := if b == c { ... } else { ... }
d := assert(...)

and I can do this inside a recipe:

    {{ if b == c { ... } else { ... } }}

So it seemed logical to me that anything to the right of := outside a recipe, could also be put inside {{ }} inside a recipe, and that therefore this would also work inside a recipe:

    {{ assert(...) }}

Though I guess maybe the issue is that assert raises an error or returns a different data type than a shell command's exit code.

liquidaty avatar Dec 24 '24 20:12 liquidaty

it seemed logical to me that anything to the right of := outside a recipe, could also be put inside {{ }} inside a recipe

Correct.

and that therefore this would also work inside a recipe:

    {{ assert(...) }}

Yes it does work. I have justfiles that use assert() like that.

laniakea64 avatar Dec 24 '24 22:12 laniakea64

Yes it does work. I have justfiles that use assert() like that.

I was looking for something like an assert function...

Is it documented anywhere? I didn't find it here https://just.systems/man/en/functions.html or via the search feature

anentropic avatar Sep 08 '25 13:09 anentropic