Skip to content

Conversation

@DrBushyTop
Copy link

@DrBushyTop DrBushyTop commented Dec 8, 2025

Description

Adds a quick fix for BCP057 ("The name does not exist in the current context") that offers to create a parameter or variable declaration for undefined symbols. This makes using snippets without included parameters / variables much easier. This was requested in and fixes #12536

When determining the type for a new parameter, the fix first checks usage context (e.g., bool if used in a condition, int if used in arithmetic), then tries resource-derived input types for complex properties, then user-defined type aliases, and finally falls back to TypeStringifier for the inferred type.

Variables try to assign some reasonable defaults when created, inferred from types. However, for objects if there are more than 5 properties, the variable defaults to an empty object to not make too many assumptions which the user might need.

New declarations are inserted after the last existing declaration of the same kind, or at the start of the referencing statement.

Type inference requires semantic model APIs (GetDeclaredType, GetDeclaredTypeAssignment, contextual analysis) that aren't available during diagnostic emission, so this was implemented as an LSP code action rather than attached directly to the diagnostic.

Example Usage

Please check the included tests for further use cases. Here's a video showing some in the IDE.

UndefinedTypesQuickFix.webm

NOTE: Parameters passed through module context pass the resourceInput values through, but defined user types are not explicitly imported.

For example a module:

param enc resourceInput<'Microsoft.Storage/storageAccounts@2025-06-01'>.properties.encryption

param myCustomParam myParamType

type myParamType = {
  name: string
  kind: 'StorageV2' | 'BlobStorage' | 'FileStorage'
}

resource sa 'Microsoft.Storage/storageAccounts@2025-06-01' = {
  name: 'somename'
  location: 'westus'
  sku: {
    name: 'Premium_LRS'
  }
  kind: myCustomParam.kind
  properties: {
    encryption: enc
  }
}

Calling file's codefix produces

param encryption resourceInput<'Microsoft.Storage/storageAccounts@2025-06-01'>.properties.encryption
param myParam myParamType

module storage 'storage-account/base.bicep' = {
  name: 'myModule'
  params: {
    enc: encryption
    myCustomParam: myParam
  }
}

Checklist

Microsoft Reviewers: Open in CodeFlow

@DrBushyTop
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quick fix for adding parameter/variable for missing variable

1 participant