bicep icon indicating copy to clipboard operation
bicep copied to clipboard

readEnvironmentVariable() function without default value throws error in IDE (BCP338)

Open johnlokerse opened this issue 9 months ago • 6 comments

Hello Bicep team,

Bicep version Bicep CLI version 0.26.170 (e9abaf16a5)

Describe the bug

When using the function readEnvironmentVariable() without using a default value it throws a BCP338 error which says Failed to evaluate parameter "test": Environment variable does not exist, and no default value set.

This is confusing because the environment variable exists on my machine and the deployment is valid and completes successfully (as confirmed by the reproduction steps), yet the error appears even though a default value is not required.

To Reproduce Steps to reproduce the behavior:

  1. Create a parameter in a Bicep template (param parTest string)
  2. Give the input parameter a value in the Bicepparam file (param parTest = readEnvironmentVariable('testEnvironmentVariable'))

Results in an error in the Bicepparam file in VSCode: image

Bicep template & parameter file used to reproduce the steps:

Bicepparam

using 'test.bicep'
param parTest = readEnvironmentVariable('testEnvironmentVariable')

Bicep template

param parTest string
output outTestEnvironmentVariableNoDefault string = parTest

Output with environment variable set (without default) using PowerShell ($env:testEnvironmentVariable = "Hello World!"): image

johnlokerse avatar May 02 '24 18:05 johnlokerse

@HadwaAbdelhalem can you look at this please?

stephaniezyen avatar May 08 '24 19:05 stephaniezyen

@HadwaAbdelhalem Let me know if I can be of any help 😃

johnlokerse avatar May 12 '24 10:05 johnlokerse

Hi @johnlokerse, if you use the below cmd to set the env variable, it sets it for the power shell process only where you ran it from. and it will not be available on the user or machine level so that for VS code the variable is not there and the error is right. But if you run Bicep cmd to deploy from that shell the tool is able to use the value you set and that's why the deployment works as expected.

$env:testEnvironmentVariable = "Hello World!"

can you try to set the variable using the below power shell cmd instead and you might need to restart VS code after wards as well. but this will persist the env variable on the user level and have it available for vscode.

[System.Environment]::SetEnvironmentVariable('testEnvironmentVariable','Hello World!', 'User')

HadwaAbdelhalem avatar May 24 '24 21:05 HadwaAbdelhalem

Hey @HadwaAbdelhalem, thank you for your research and reply 👍

image

That works! It is clear to me now how it works on a local computer. I am on a Mac, so it does not know PowerShell environment variables and had to use export (Mac equivalent) to make it work.

Is this something that could be added to the documentation of the readEnvironmentVariable function (https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-parameters-file#readenvironmentvariable)?

johnlokerse avatar Jun 10 '24 14:06 johnlokerse

@johnlokerse glad that works! I'm assigning the documentation tag on this to add more information on the readEnvironmentVariable function based off of Hadwa's comment - @mumian can you look at this when you have the time?

stephaniezyen avatar Jun 10 '24 18:06 stephaniezyen

I'm wondering if this can be made into overridable in bicepconfig.json? Often the place where I'm developing templates is not the place they're deploying (e.g. I know that our deployment pipeline is going to present this environment variable at deploy time). I can add defaults to many things, but it's often cumbersome or undesirable to either add all the defaults and/or add all the items to the environment.

e.g. often we set a VM's admin password to a throwaway randomly generated string given we're using the AADLoginFor{Windows|Linux} VM extension.

I don't really want to load an env var called "adminPassword" or similar just to make the linting error go away, and certainly don't want to put a default in the parameters file.

Leaving this as a linter error then just means other "real" errors get overlooked. It would be nice to be able to tell the linter "I know this value doesn't exist now, but when I actually deploy this file from somewhere else, I promise it will either be there or I'm willing to accept the missing value will induce a failure."

ManagedUx avatar Jun 11 '24 23:06 ManagedUx

PR created - https://github.com/MicrosoftDocs/azure-docs-pr/pull/284244

mumian avatar Aug 09 '24 20:08 mumian

Great improvement on the readEnvironmentVariable documentation. Adding the link to the docs for future reference: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-parameters-file#readenvironmentvariable

johnlokerse avatar Aug 20 '24 11:08 johnlokerse