task icon indicating copy to clipboard operation
task copied to clipboard

Unable to use env and vars in the same task definition

Open filbotblue opened this issue 3 years ago • 3 comments
trafficstars

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.

filbotblue avatar Aug 23 '22 16:08 filbotblue

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}}

mrwormhole avatar Sep 01 '22 13:09 mrwormhole

@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).

pd93 avatar Sep 10 '22 15:09 pd93

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.

andreynering avatar Sep 12 '22 21:09 andreynering