Skip to content

Unable to use env and vars in the same task definition #843

Closed

Description

@filbotblue

Information

  • Task version: (installed by RPM)
> task --version
Task version: v3.12.0 (h1:viFy8kdDZ2iTcpTuxzzJCeKtTGt9U+5iXMVIpLjvIro=)
  • Operating System: Fedora Silverblue 36 and Toolbox
> cat /etc/os-release
NAME="Fedora Linux"
VERSION="36 (Container Image)"
ID=fedora
VERSION_ID=36
VERSION_CODENAME=""
PLATFORM_ID="platform:f36"
PRETTY_NAME="Fedora Linux 36 (Container Image)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:36"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f36/system-administrators-guide/"
SUPPORT_URL="https://ask.fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=36
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=36
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
VARIANT="Container Image"
VARIANT_ID=container

Example Taskfile showing the issue

---
# https://taskfile.dev
version: '3'

tasks:
  foobar:
    desc: Use both vars and env in a single task.
    cmds:
      - echo {{.FOO}} {{.BAR}}
    vars:
      FOO: Fizz
    env:
      BAR: Buzz
...

Output

When using the YAML file above, I get the following

> task foobar
task: [foobar] echo Fizz
Fizz

The second part of this is missing. However, if I place the env section at the root level of the YAML file, it works as expected.

Activity

  1. occamist commented on Sep 1, 2022

    @occamist

    seems like a bug regarding {{.BAR}}, when environment variables are defined in env block at the cmd level, they can not use templating but if you do export BAR=bar you can use with {{.BAR}} or if you define your env variable at the top level of the file, you can use {{.BAR}}

  2. pd93 commented on Sep 10, 2022

    @pd93
    Member

    @filbotblue env variables are made available to your shell rather than the templating engine, so to echo the value, you would do echo {{.FOO}} $BAR instead of echo {{.FOO}} {{.BAR}} as per the documentation.

    Looking back through previous versions, outputting task-level env variables via the templating engine has never worked and is not documented, so I wouldn't consider this a bug. In fact, I'm not sure if the ability to use globally defined env variables in the templating engine is intended behaviour. I'll admit, the inconsistency is definitely slightly confusing.

    @andreynering do you know if accessing globally defined env variables using the templating engine is intended/supported? If not, should we add a warning when using this feature? (since removing it would not be backward-compatible).

  3. andreynering commented on Sep 12, 2022

    @andreynering
    Member

    In theory this is a bug, but I would discourage the use of env for this propose. Why not use vars? env should be used to make environment available to the commands you're running.

  4. trulede commented on Dec 5, 2025

    @trulede
    Contributor

    Globally defined env is made available to templating engine, effectively as vars, which is a well known source of confusion. Task defined env is 'injected' into the environment of the task commands. In this case, that is confused with the operation of vars. The correct command would be:

    - echo {{.FOO}} $BAR

    The reason this exists is because some commands can only be configured/operated by environment variables. In other cases, vars and the templating system is typically less confusing to use.

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

    state: working as intendedThe issue described is working as originally intended.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions