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

Import of variable object fails with BCP338 #13997

Closed
johndikeibm opened this issue May 2, 2024 · 6 comments · Fixed by #14070
Closed

Import of variable object fails with BCP338 #13997

johndikeibm opened this issue May 2, 2024 · 6 comments · Fixed by #14070
Assignees

Comments

@johndikeibm
Copy link

Bicep version
Bicep CLI version 0.26.170 (e9abaf1)

Describe the bug

  1. I expect that when I import an object, I am able to access the items of that object using dot notation and render the value of that item.
  2. I expect that while using dot notation to render the value of an object's item (as described in 1. above) , I can assign that value (using dot notation) to an element of another object in an array or list.

To Reproduce

bicepConfig.json

{
  // See https://aka.ms/bicep/config for more information on Bicep configuration options
  // Press CTRL+SPACE/CMD+SPACE at any location to see Intellisense suggestions
  "analyzers": {
    "core": {
      "rules": {
        "no-unused-params": {
          "level": "warning"
        }
      }
    },
  "experimentalFeaturesEnabled": {
    "compileTimeImports": true,
    "userDefinedFunctions": true,
   "extensibility": true
  }
  }
}

module.bicep

@export()
var locationOptions = {
location1: 'WestUs3'
location2: 'EastUs1'
}

mainbicep.bicepparam

import {locationOptions} from 'module.bicep'

// Failed to evaluate parameter "vmList": Unhandled exception during evaluating template language function 'variables' is not handled.bicep(BCP338)
param vmList = [
{
name: 'vm1'
location: locationOptions.location1
}
]

Additional context
image

Same screenshot while hovering over locationOptions.location1
image

@jeskew
Copy link
Contributor

jeskew commented May 6, 2024

@johndikeibm Is this something that happens repeatedly for you? I haven't been able to reproduce with the snippets included in the description, but I can tell from the screenshots the snippet is taken from a larger file.

There were a couple of bugs fixed in earlier releases that had similar error messages to the one reported. VS Code may be using an older version of Bicep (it has its own copy and doesn't use the same one as the az CLI). Could you verify that VS Code is using 0.26.170? This is reported on the extension page in VS Code:

image

@johndikeibm
Copy link
Author

johndikeibm commented May 7, 2024

@jeskew No, it's not something that happens frequently because this is the first time I am using imports.
I attempted to define a variable object in a module.
Then import that variable to be used in a param file as a way to provide pre-defined options to developers.

// sampleModule.bicep
@export()
var subscriptions = {
prod: 'subscriptions/13452556764478374367/'
dev: 'subscrptions/453465765756756776877/'
}

That way I could do

import {subscrption} from sampleModule.bicep
var someNewVar = subscriptions.prod  //This works.

// The problem occurs when I try to reference the object using dot-notation inside of a List [ ] or array.
// The second screenshot shows that the value of the object property can be seen in the pop up.
// Still it shows the error message and lints the line with an error.
param resourceList  = [
{
name: 'resource-1'
sub: subscriptions.prod
}
{
name: 'resource-2'
sub: subscriptions.dev
}
{
name: 'resource-3'
sub: subscriptions.prod
}
]

image

@jeskew
Copy link
Contributor

jeskew commented May 7, 2024

@johndikeibm Sorry for the miscommunication, by "is this something that happens repeatedly for you?" I meant, "does this happen every time you view the file?" (vs an error that only happens sporadically or unpredictably).

Could you share a bit more about your environment (OS and architecture)? I am not seeing the same error with the new snippets either, although I can only easily test on Windows and Linux x64. There shouldn't be any differences across environments, but that info might help repro the error:

Windows:
13997_windows

Linux:
13997_wsl

@stephaniezyen stephaniezyen added Needs: Author Feedback Awaiting feedback from the author of the issue and removed Needs: Triage 🔍 labels May 8, 2024
@johndikeibm
Copy link
Author

johndikeibm commented May 8, 2024

@jeskew I attempted to make a gif-like video, like you did, but couldn't. The video I recorded was too large for github. How did you make yours?

This is a Windows machine.
Here's a reproduction of the bug alongside screenshots.

Error Message in main.bicepparam
image

[Directory Structure)
bicepRepo (folder)
| bicepConfig.json
|
+---AppService
| main.bicep
| main.bicepparam
|
---modules
appService.bicep

bicepConfig.json

{
    // See https://aka.ms/bicep/config for more information on Bicep configuration options
    // Press CTRL+SPACE/CMD+SPACE at any location to see Intellisense suggestions
    "analyzers": {
      "core": {
        "rules": {
          "no-unused-params": {
            "level": "warning"
          }
        }
      },
    "experimentalFeaturesEnabled": {
      "compileTimeImports": true,
      "userDefinedFunctions": true,
     "extensibility": true
    }
    }
  }

AppService/main.bicep

targetScope = 'subscription'
param date string = utcNow()
param appServiceList array

param location string

module appService '../modules/appService.bicep'= [ for appServiceItem in appServiceList: {
name: '${appServiceItem.name}-${date}'
scope: resourceGroup(appServiceItem.resourceGroup)
params: {
  appServiceName: appServiceItem.name
  location: location
  kind: appServiceItem.?kind
  plan: appServiceItem.plan
  siteConfig: appServiceItem.?stackConfig
}
}]

AppService/main.bicepparam

using 'main.bicep'

import  { appStacks } from '../modules/appService.bicep'

param location = 'someLocation'

var aspItems = {
 prod: '/subscriptions/245633435/'
 dev: '/subscriptions/234567832/'
}

param appServiceList = [
{  
  name: 'resource-1'
  plan: aspItems.prod
  resourceGroup: 'resource-group-1'
  stackConfig: appStacks.aspDotNet4
}
{
  name: 'resource-2'
  plan: aspItems.dev
  resourceGroup: 'resource-group-2'
  stackConfig: appStacks.dotNet8
}
{
  name: 'resource-3'
  plan: aspItems.prod
  resourceGroup: 'resource-group-3'
  stackConfig: appStacks.dotNet8
}
]

modules/appserbice.bicep

param appServiceName string
param location string
param plan string


@description('App service kind defaults to Windows Web app')
param kind string = 'app'

@description('Optional: Application sepcifica configuration')
param siteConfig object?

@export()
var appStacks = {
  dotNet8: {
    metadata:[
    {
      name: 'CURRENT_STACK'
      value: 'dotnet'
    }
    ]
    netFrameworkVersion: 'v8.0'
 }
  aspDotNet4: {
    metadata: [
      {
        name: 'CURRENT_STACK'
        value: 'dotnet'
      }
    ]
    netFrameworkVersion: 'v4.8'
  }
  dotNet8Func: {
    netFrameworkVersion: 'v8.0'
    appSettings: [
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'dotnet-isolated'
        }
      ]
  }
}

resource appService 'Microsoft.Web/sites@2020-06-01' = {
  name: appServiceName
  location: location
  kind: kind
  properties: siteConfig
}

[Screenshots]
image

image

image

image

image

@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 May 8, 2024
@jeskew
Copy link
Contributor

jeskew commented May 8, 2024

Thanks for the thorough repro! This is a bug in how .bicepparam files handle variable imports from .bicep files; basically, if the .bicep file has any required parameters, you will see that error. You can work around this by moving the variable into a .bicep file without any declared parameters, but this is definitely something that should be fixed.

I use ScreenToGif for the screenshots, but Window's built-in screenshot function (win + shift + s) can also take videos.

@jeskew jeskew added bug Something isn't working compile-time-imports labels May 8, 2024
@johndikeibm
Copy link
Author

Thank you very much. I did just that and it fixed the error. Snipping tool is how I got the screenshots to you. Sadly, recording a 30 second video provided a file too large for GitHub uploads. That's how I settled for screenshots.
I'll use ScreenToGif moving forward, seems pretty handy and provides output files with low file sizes.

I look forward to the official fix for this bug. In the meantime, I'll use this workaround.
Thank you very much for your help.

jeskew added a commit that referenced this issue May 11, 2024
Resolves #13997 
Resolves #14069 

This PR updates the `ParameterAssignmentEvaluator` to only evaluate the
portions of imported templates that are used. This will allow imports
from Bicep templates that have required parameters, which is not
supported today.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/14070)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants