Skip to content

Option to assign env from vars #1863

Closed as duplicate
Closed as duplicate

Description

@onedr0p

Hi 👋🏼

Sometimes my taskfiles and scripts they call require both vars and env to be set to the same values. You can imagine a cmd using a var {{.VAR}} and the script it calls (or envsubst) also using the same var but as env $VAR.

It would be great if there was a way to easily have env inherit vars set on a task.

Current issue

version: '3'

tasks:
  test:
    cmds:
      - echo "using vars {{.FOO}}"
      - echo "using env $FOO and $FIZZ"
      - cat test.txt | envsubst # test.txt has content '$FOO:$FIZZ'
    vars: &vars
      FOO: bar
    env:
      <<: *vars
      FIZZ: buzz

Output with various TASK_X_MAP_VARIABLES

TASK_X_MAP_VARIABLES=0 task test
err:  maps cannot be assigned to variables
file: /tmp/Taskfile.yaml:8:11
   6 |       - echo "using vars {{.FOO}}"
   7 |       - echo "using env $FOO and $FIZZ"
>  8 |     vars: &vars
     |           ^
   9 |       FOO: bar
  10 |     env:
TASK_X_MAP_VARIABLES=1 task test
task: [test] echo "using vars bar"
using vars bar
task: [test] echo "using env $FOO and $FIZZ"
using env  and buzz
task: [test] cat test.txt | envsubst
:buzz

Purposed solution

Even if the above worked, it would still be great to have the below to avoid the anchor/alias jank.

version: '3'

tasks:
  test:
    cmds:
      - echo "using vars {{.FOO}}"
      - echo "using env $FOO and $FIZZ"
      - cat test.txt | envsubst # test.txt has content '$FOO:$FIZZ'
    vars:
      FOO: bar
    envFromVars: true # assign all vars to env
    env:
      FIZZ: buzz # and also allow adding other env

Expected Output

task test
task: [test] echo "using vars bar"
using vars bar
task: [test] echo "using env $FOO and $FIZZ"
using env bar and buzz
task: [test] cat test.txt | envsubst
bar:buzz

Activity

  1. changed the title [-]Option to easily assign env to vars[/-] [+]Option to assign env to vars[/+] on Oct 13, 2024
  2. changed the title [-]Option to assign env to vars[/-] [+]Option to assign env from vars[/+] on Oct 14, 2024
  3. rfan-debug commented on Mar 27, 2025

    @rfan-debug

    This is an important feature to structure Taskfile cleanly. Now we have to duplicate env vars almost in every sub command.

  4. pd93 commented on Mar 28, 2025

    @pd93
    Member

    Added this to the variables megathread. Would be interesting to hear your thoughts on #2036.

  5. added theissue type on Mar 28, 2025
  6. added and removed
    state: needs triageWaiting to be triaged by a maintainer.
    on Mar 28, 2025
  7. trulede commented on Dec 9, 2025

    @trulede
    Contributor

    I don't know exactly why, but this is fixed by #2551.

    version: '3'
    
    tasks:
      default:
        cmds:
          - echo "using vars FOO={{.FOO}} BAR={{.BAR}} FUBAR={{.FUBAR}}"
          - echo "using env FOO=$FOO BAR=$BAR FUBAR=$FUBAR"
        vars: &vars
          FOO: foo
          BAR: bar
        env:
          <<: *vars
          FUBAR: fubar
    
    $ task
    task: [default] echo "using vars FOO=foo BAR=bar FUBAR="
    using vars FOO=foo BAR=bar FUBAR=
    task: [default] echo "using env FOO=$FOO BAR=$BAR FUBAR=$FUBAR"
    using env FOO=foo BAR=bar FUBAR=fubar
    

    I will close this as a duplicate, please try #2551 and comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for feature.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions