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

CARGO_MAKE_CURRENT_TASK_NAME shows previous task's name when printed in a condition_script #1173

Closed
wmmc88 opened this issue Oct 9, 2024 · 5 comments
Assignees
Labels

Comments

@wmmc88
Copy link
Contributor

wmmc88 commented Oct 9, 2024

Describe The Bug

This is somewhat of a follow up to this comment I made on a pr a while back: #1132 (comment)

Right now, CARGO_MAKE_CURRENT_TASK_NAME only get updated after the condition passes:

envmnt::set("CARGO_MAKE_CURRENT_TASK_NAME", &step.name);

This has the side effect that if you were to print CARGO_MAKE_CURRENT_TASK_NAME in a condition_script, it would actually print the previous task's name. Is this intended behavior?

@sagiegurari
Copy link
Owner

@wmmc88 i think we talked about it before in some other issue.
env mutation or any other mutation is not done until condition is met in order not to modify the env wrongly.
i get you might need this data in your condition script so you can overwrite this behavior via plugins.

example of default behavior:

[tasks.first]
script = """
echo "current (first): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""

[tasks.second]
dependencies = ["first"]
condition_script = """
echo "condition (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""
script = """
echo "current (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""

running it

gitpod /workspace/misc-test (cargo_make_1173) $ cargo make second
[cargo-make] INFO - cargo make 0.37.21
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: second
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: first
current (first): first
condition (second): first
[cargo-make] INFO - Running Task: second
current (second): second
[cargo-make] INFO - Build Done in 0.01 seconds.

with a plugin that overwrites this logic for task second

[plugins.impl.set-task-name]
script = '''
set_env CARGO_MAKE_CURRENT_TASK_NAME ${task.name}
cm_plugin_run_task
'''

[tasks.first]
script = """
echo "current (first): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""

[tasks.second]
plugin = "set-task-name"
dependencies = ["first"]
condition_script = """
echo "condition (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""
script = """
echo "current (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""

running it

gitpod /workspace/misc-test (cargo_make_1173) $ cargo make second
[cargo-make] INFO - cargo make 0.37.21
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: second
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: first
current (first): first
condition (second): second
[cargo-make] INFO - Running Task: second
current (second): second
[cargo-make] INFO - Build Done in 0.01 seconds.

@wmmc88
Copy link
Contributor Author

wmmc88 commented Oct 11, 2024

@sagiegurari yes, I believe we talked about env mutation before, and it makes sense to me that the env isn't changed for most normal environment variables, but this is a question specifically to do with CARGO_MAKE_CURRENT_TASK_NAME.

What prompted my question was because there is this line of code which seems to mutate the env before the condition script but:

  1. the env var being mutated starts with CARGO_MAKE_CURRENT_TASK_
  2. the env var must be set by the task's env block (ie. there is not automatic mutation by cargo-make to update task name)

So in the following file:

[tasks.first]
script = """
#!@duckscript

echo "current (first): ${CARGO_MAKE_CURRENT_TASK_NAME}"
"""

[tasks.second]
dependencies = ["first"]
env = { "CARGO_MAKE_CURRENT_TASK_NAME" = "dummy", "ENV_VAR" = "hello!" }
condition_script = """
#!@duckscript

echo "condition (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
echo "condition env var: ${ENV_VAR}"
"""
script = """
#!@duckscript

echo "current (second): ${CARGO_MAKE_CURRENT_TASK_NAME}"
echo "current env var: ${ENV_VAR}"
"""

it outputs:

🪟 Windows 11 Enterprise(10.0.26100) (pwsh) D:\git-repos
at 08:43:04 AM ❯ cargo make second
[cargo-make] INFO - cargo make 0.37.21
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: second
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: first
current (first): first
condition (second): dummy
condition env var:
[cargo-make] INFO - Running Task: second
current (second): second
current env var: hello!
[cargo-make] INFO - Build Done in 0.69 seconds.

Notice that in the prints in the condition script, CARGO_MAKE_CURRENT_TASK_NAME had been mutated but ENV_VAR has not been. Why are the CARGO_MAKE_CURRENT_TASK_ special-cased and mutated?

@sagiegurari
Copy link
Owner

@wmmc88 hmmm you are right! i can't remember what i did :D
strange. will check.
current task and few others are special env vars that are added to the env block during the descriptor loading.
maybe i got a bug there or something. will check

@sagiegurari
Copy link
Owner

@wmmc88 thanks for finding and reporting bugs in cargo-make and insisting on them :)

i'm not sure why the code was like it was. things change but you are correct and I think I solved it and releasing it now (will take around 15 mins)

@wmmc88
Copy link
Contributor Author

wmmc88 commented Oct 12, 2024

@sagiegurari no problem! I always try to make sure to report bugs when I find them (and help fix if time permits 😁)

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