Skip to content

Environment variables are forced to upper case on Windows - breaks expectations for some CLI tools (Terraform) #2259

@Kieranties

Description

@Kieranties

Description

I discovered this issue while creating some tasks that invoke the Terraform cli.
Terraform supports the ability to set input variables via environment variables, however a specific caveat is that the environment name must be in a format that matches the variable name and case (for certain environments).

On operating systems where environment variable names are case-sensitive, Terraform matches the variable name exactly as given in configuration, and so the required environment variable name will usually have a mix of upper and lower case letters as in the above example.

E.g. if I have a variable called my_variable the environment variable to set this at runtime is TF_VAR_my_variable.

While running in a windows environment via Task I found that the variable was not being set. After further digging, it would appear that Task is forcing environment variables to be upper case in the context of the target command.

Attached is a repro that contains a Task file where

  • Some default environment variables are configured with default values if not set.
  • Each variable is created with a name that reflects its casing (upper/lower/mixed)
  • A default task which outputs the variables as read from the environment and then executes PowerShell to list the variables in the PowerShell environment.

When invoked with no environment, the output shows

task
[default] upper - default_upper
[default] lower - default_lower
[default] mixed - default_mixed
[default]
[default] Name                           Value
[default] ----                           -----
[default] UPPER_VARIABLE                 default_upper
[default] LOWER_VARIABLE                 default_lower
[default] MIXED_VARIABLE                 default_mixed
[default]

Warning

The output in PowerShell displays all environment variable names as upper case - they do not confirm to the names as defined in the task file

When invoked with an environment variable set (in PowerShell)

$env:UPPER_VARIABLE='custom_upper_value'
$env:lower_variable='custom_lower_value'
$env:MIXED_variable='custom_mixed_value'

Calling the variable list command in PowerShell directly, returns what we expect - the variable names are in the case they were declared in

ls env:\*variable
Name                           Value
----                           -----
MIXED_variable                 custom_mixed_value
UPPER_VARIABLE                 custom_upper_value
lower_variable                 custom_lower_value

But if we call task again

task
[default] upper - custom_upper_value
[default] lower - custom_lower_value
[default] mixed - custom_mixed_value
[default]
[default] Name                           Value
[default] ----                           -----
[default] UPPER_VARIABLE                 custom_upper_value
[default] MIXED_VARIABLE                 custom_mixed_value
[default] LOWER_VARIABLE                 custom_lower_value
[default]

We still have the environment variables named all in upper case.

Important

As mentioned the issue was originally identified with Terraform - when invoking Terraform it was not invoked via Powershell, but directly as a cli (e.g. terraform plan) but using PowerShell helps to demonstrate the changes in the variable format

Version

3.43.2

Operating system

Windows 11

Experiments Enabled

No response

Example Taskfile

# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
output: prefixed

env:
  UPPER_VARIABLE:
    sh: echo "${UPPER_VARIABLE:-default_upper}"
  lower_variable:
    sh: echo "${lower_variable:-default_lower}"
  MIXED_variable:
    sh: echo "${MIXED_variable:-default_mixed}"

tasks:
  default:
    silent: true
    cmds:
      - echo "upper - $UPPER_VARIABLE"
      - echo "lower - $lower_variable"
      - echo "mixed - $MIXED_variable"
      - pwsh -c ls env:\*variable

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs triageWaiting to be triaged by a maintainer.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions