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

BCP338 when referencing variables from functions #16172

Open
carbage opened this issue Jan 22, 2025 · 1 comment
Open

BCP338 when referencing variables from functions #16172

carbage opened this issue Jan 22, 2025 · 1 comment
Assignees

Comments

@carbage
Copy link

carbage commented Jan 22, 2025

Bicep version
Bicep CLI version 0.29.47

Describe the bug
Chaining variables causes BCP338 - removing subscriptionId from the diagnosticSettings object fixes the error.
Full error for this example:
Failed to evaluate parameter "keyVaults": Unhandled exception during evaluating template language function 'variables' is not handled.

To Reproduce
common.bicep

@export()
func diagnosticSettings(env string) object => {
  eventHub: {
    namespace: {
      subscriptionId: subscriptions.default[env]
      resourceGroupName: 'diag-rg'
      name: 'diag-ehn'
    }
    name: 'metrics'
  }
  metrics: [
    {
      category: 'AllMetrics'
    }
  ]
}

@export()
var subscriptions = {
  default: {
    'dev': 'xxx'
    'uat': 'xxx'
    'prd': 'xxx'
  }
}

key-vault.bicepparam

using 'br/modules:key-vault:latest'

import { diagnosticSettings } from './common.bicep'

var location = 'uksouth'
var locationSlug = substring(location, 0, 3)

var env = readEnvironmentVariable('ENVIRONMENT', 'dev')
var diagSettings = diagnosticSettings(env)

param keyVaults = {
  'key-vault': {
    resourceGroupName: 'resource-group'
    diagnosticSettings: diagSettings
  }
}

Additional context
I've noticed a similar issue, for example, when using loadJsonContent('./some/json.json') in place of imports though have not reproduced for this specific issue

@github-project-automation github-project-automation bot moved this to Todo in Bicep Jan 22, 2025
@carbage carbage changed the title BCP338 when chaining variables BCP338 when referencing variables from functions Jan 22, 2025
@carbage
Copy link
Author

carbage commented Jan 22, 2025

FWIW, this can be resolved in this scenario by replacing subscriptions with a function returning object

@export()
function subscriptions() object => {
  default: {
    'dev': 'xxx'
    'uat': 'xxx'
    'prd': 'xxx'
  }
}

The inverse, referencing functions from exported variables also works, though the case in question would be fairly beneficial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants