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

Using variables in recipe parameters #1310

Closed
papertigers opened this issue Aug 15, 2022 · 4 comments
Closed

Using variables in recipe parameters #1310

papertigers opened this issue Aug 15, 2022 · 4 comments

Comments

@papertigers
Copy link
Contributor

I noticed it's not currently possible to do something like:

first_path := "foo"
second_path := "bar"

copy file:
    cp {{file}} /tmp

setup: (copy {{first_path}})
    some_cmd

setup-dev: (copy {{second_path}})
    other_cmd

Is this by design or just something that no one has added support yet? In my real life example I have two top level commands that currently take string values for file names. I would like to define those file names as a variable that I can reuse.

@casey
Copy link
Owner

casey commented Aug 15, 2022

Outside of recipes, you don't need {{…}} to do interpolation:

first_path := "foo"
second_path := "bar"

copy file:
    cp {{file}} /tmp

setup: (copy first_path)
    some_cmd

setup-dev: (copy second_path)
    other_cmd

Let me know if that works for you!

@papertigers
Copy link
Contributor Author

That works! Thanks, I should have realized that was the case.

Is the namespace global? For example if one defines the foo variable at the top of a justfile and then makes a recipe like do_stuff foo:. Will that overwrite the variable set or is it scoped to just that recipe? Thanks again.

@papertigers
Copy link
Contributor Author

PS added a short example for this in #1311 if you find it useful.

@casey
Copy link
Owner

casey commented Aug 16, 2022

Is the namespace global? For example if one defines the foo variable at the top of a justfile and then makes a recipe like do_stuff foo:. Will that overwrite the variable set or is it scoped to just that recipe? Thanks again.

You bet! The namespace is global, but if you try to redefine a variable with a function parameter, it actually complains:

error: Parameter `foo` shadows variable of the same name
  |
3 | j foo:
  |   ^^^

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

No branches or pull requests

2 participants