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

feat: Added support for lazy variables evaluation #1231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tohutohu
Copy link

This Pull Request supports delayed evaluation of dynamic variables, adding a lazy option to the vars[] schema that makes the execution of global dynamic variable shell commands happen only when necessary.

What this feature

In the current go-task, global dynamic variables always execute their commands. Therefore, with a Taskfile.yaml like the following, running task depend-on-only-fast-var always causes a 3-second wait due to SLOW_VAR.

version: "3"

vars:
  SLOW_VAR:
    sh: sleep 3 && echo "slow"
  FAST_VAR:
    sh: echo "fast"

tasks:
  depend-on-slow-var:
    cmds:
      - echo {{.SLOW_VAR}}
  depend-on-only-fast-var:
    cmds:
      - echo {{.FAST_VAR}}

With the functionality added in this PR, dynamic variable evaluations can be delayed until necessary. This means that with a Taskfile.yaml like the following, task depend-on-only-fast-var can be executed immediately.

version: "3"

vars:
  SLOW_VAR:
    sh: sleep 3 && echo "slow"
    lazy: true
  FAST_VAR:
    sh: echo "fast"
    lazy: true

tasks:
  depend-on-slow-var:
    cmds:
      - echo {{.SLOW_VAR}}
  depend-on-only-fast-var:
    cmds:
      - echo {{.FAST_VAR}}

How

Instead of passing strings as variables to the text/template, a structure that implements the Stringer interface is passed, which executes the command and uses the result as a dynamic variable the first time it's called. That result is then cached in the structure, ensuring that the same command is never executed twice. The Singleflight library guarantees that it will be executed only once even if called concurrently.

Compatibility with this PR

This PR does not break compatibility. Dynamic variables that are not marked as lazy: true will continue to operate as before.

@andreynering
Copy link
Member

Hi @tohutohu, thanks for opening this PR!

It's an interesting idea to use the Stringer interface, indeed.

I haven't reviewed the code yet (there would be some changes), but I want to discuss something first:

What if we apply this for every variable? It doesn't seem like it would be a breaking change to me initially. Behavior would change slightly in that a variable that is unused would not run, but I think it'd be unlikely that this would breaking something for a real user.

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

Successfully merging this pull request may close these issues.

None yet

2 participants