Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johnlokerse opened this issue May 2, 2024 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation Needs: Triage 🔍
Milestone

Comments

@johnlokerse
Copy link
Contributor

johnlokerse commented May 2, 2024

Hello Bicep team,

Bicep version
Bicep CLI version 0.26.170 (e9abaf1)

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

@stephaniezyen
Copy link
Contributor

@HadwaAbdelhalem can you look at this please?

@stephaniezyen stephaniezyen self-assigned this May 8, 2024
@johnlokerse
Copy link
Contributor Author

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

@HadwaAbdelhalem
Copy link
Contributor

HadwaAbdelhalem commented May 24, 2024

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')

@stephaniezyen stephaniezyen added Needs: Author Feedback Awaiting feedback from the author of the issue and removed Needs: Triage 🔍 labels Jun 4, 2024
@johnlokerse
Copy link
Contributor Author

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)?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Triage 🔍 and removed Needs: Author Feedback Awaiting feedback from the author of the issue labels Jun 10, 2024
@stephaniezyen stephaniezyen added the documentation Improvements or additions to documentation label Jun 10, 2024
@stephaniezyen stephaniezyen added this to the v0.29 milestone Jun 10, 2024
@stephaniezyen
Copy link
Contributor

@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?

@ManagedUx
Copy link

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."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation Needs: Triage 🔍
Projects
Status: Todo
Development

No branches or pull requests

5 participants