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: add ability to disable dynamic variable caching #1451

Closed
wants to merge 1 commit into from

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Dec 29, 2023

See comment on discord copied below:

I came across something strange.

test:
  cmds:
    - echo {{ .FILE }} == {{ .TEMPFILE }}
  vars:
    FILE:
      sh: mktemp
    TEMPFILE:
      sh: mktemp
> task test
task: [test] echo /var/folders/6s/2k2vd19s7kg20405_blbh7fm0000gn/T/tmp.705g66B7Ku == /var/folders/6s/2k2vd19s7kg20405_blbh7fm0000gn/T/tmp.705g66B7Ku
/var/folders/6s/2k2vd19s7kg20405_blbh7fm0000gn/T/tmp.705g66B7Ku == /var/folders/6s/2k2vd19s7kg20405_blbh7fm0000gn/T/tmp.705g66B7Ku

Why FILE and TEMPFILE has the same value?

This is happening because of Task's dynamic variable cache. We should add the ability to disable this on a per-variable basis:

  without-cache:
    silent: true
    vars:
      FOO:
        sh: mktemp
        cache: false
      BAR:
        sh: mktemp
        cache: false
    cmds:
      - echo {{ .FOO }}
      - echo {{ .BAR }}
/tmp/tmp.vGKqfv8fm7
/tmp/tmp.4Jtfmihw58

@andreynering
Copy link
Member

Hey @pd93,

I think this changes needs a bit of thought:

  1. I'm not sure, but I think it may happen, with cache: false, that a given variable shell will be run multiple times, which will be unexpected (and perhaps even dangerous) to the user. That should at least be checked before merging.
  2. I have an idea (Idea: Consider having functions to access variables and envs #1065) to have "lazy variables" at some point. That would be a overhaul on how vars and envs are handled, in which a variable would only be computed once really requested by the template engine. Depending on how we implement that, that may make cache: false obsolete (because vars would not be cached by default to begin with). So this makes me question: should we introduce an option that may soon become obsolete?

@pd93
Copy link
Member Author

pd93 commented Dec 30, 2023

@andreynering Yeah, you make some very good points. I'll admit that I cobbled this together quite quickly without giving it too much thought in response to the comments on Discord.

Lazy variables are definitely the way forwards long term. I have given some thought about how this could be done. However, I'm not a massive fan of having to call a function inside of templates to get variable values. I actually think there might be a way to do it without the need for functions which would also keep compatibility with v3. Perhaps this is a discussion for another time/place though.

Let's close this for now 👍

@pd93 pd93 closed this Dec 30, 2023
@pd93 pd93 deleted the var-cache-flag branch January 11, 2024 16:16
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.

2 participants