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

When using a parameter with an imported user-defined type array, template compilation fails #12052

Closed
GABRIELNGBTUC opened this issue Oct 5, 2023 · 2 comments · Fixed by #12055
Assignees
Labels
bug Something isn't working Needs: Triage 🔍

Comments

@GABRIELNGBTUC
Copy link

GABRIELNGBTUC commented Oct 5, 2023

Bicep version
Bicep CLI version 0.22.6 (d62b94d)
VS code extension was directly installed from the provided .vsix in the release tab from GitHub

Describe the bug
When upgrading to the 0.22.6, any template that has a parameter type CustomType[] where CustomType is imported now fails to compile

To Reproduce
main.bicep:

import {Foo} from 'types.bicep'

param foo Foo[] = [
  {
    bar: true
  }
  {
    bar: false
  }
]

types.bicep:

@export()
type Foo = {
  bar: bool
}

In 0.21.1, az bicep build -f main.bicep compiles to the following

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.21.1.54444",
      "templateHash": "3487146124344436412"
    }
  },
  "definitions": {
    "Foo": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "bool"
        }
      },
      "metadata": {
        "__bicep_imported_from!": {
          "sourceTemplate": "types.bicep"
        }
      }
    }
  },
  "parameters": {
    "foo": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Foo"
      },
      "defaultValue": [
        {
          "bar": true
        },
        {
          "bar": false
        }
      ]
    }
  },
  "resources": {}
}

In 0.22.6, running the same commands fails with the following error:

WARNING: The following experimental Bicep features have been enabled: Compile-time imports. Experimental features should be enabled for testing purposes only, as there are no guarantees about the quality or stability of these features. Do not enable these settings for any production usage, or your production environment may be subject to breaking.
Unhandled exception. System.ArgumentException: Invalid type expression encountered.
   at Bicep.Core.Emit.TemplateWriter.TypePropertiesForTypeExpression(TypeExpression typeExpression)
   at Bicep.Core.Emit.TemplateWriter.GetTypePropertiesForArrayType(ArrayTypeExpression expression)
   at Bicep.Core.Emit.TemplateWriter.TypePropertiesForTypeExpression(TypeExpression typeExpression)
   at Bicep.Core.Emit.TemplateWriter.<>c__DisplayClass27_0.<EmitParameter>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WriteObjectWithPosition(IPositionable sourcePosition, Action propertiesFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObject(Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.<>c__DisplayClass32_0.<EmitObjectProperty>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WritePropertyWithPosition(IPositionable keyPosition, String name, Action valueFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitProperty(String propertyName, Action writeValueFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObjectProperty(String propertyName, Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.TemplateWriter.EmitParameter(ExpressionEmitter emitter, DeclaredParameterExpression parameter)
   at Bicep.Core.Emit.TemplateWriter.<>c__DisplayClass23_0.<EmitParametersIfPresent>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WriteObjectWithPosition(IPositionable sourcePosition, Action propertiesFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObject(Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.<>c__DisplayClass32_0.<EmitObjectProperty>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WritePropertyWithPosition(IPositionable keyPosition, String name, Action valueFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitProperty(String propertyName, Action writeValueFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObjectProperty(String propertyName, Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.TemplateWriter.EmitParametersIfPresent(ExpressionEmitter emitter, ImmutableArray`1 parameters)
   at Bicep.Core.Emit.TemplateWriter.GenerateTemplateWithoutHash(PositionTrackingJsonTextWriter jsonWriter)
   at Bicep.Core.Emit.TemplateWriter.Write(SourceAwareJsonTextWriter writer)
   at Bicep.Core.Emit.TemplateEmitter.<>c__DisplayClass10_0.<Emit>b__0()
   at Bicep.Core.Emit.TemplateEmitter.EmitOrFail(Func`1 write)
   at Bicep.Core.Emit.TemplateEmitter.Emit(TextWriter textWriter)
   at Bicep.Core.Emit.TemplateEmitter.Emit(Stream stream)
   at Bicep.Cli.Services.CompilationWriter.ToStream(SemanticModel model, Stream stream)
   at Bicep.Cli.Services.CompilationWriter.ToFile(SemanticModel model, String outputPath)
   at Bicep.Cli.Services.CompilationWriter.ToFile(Compilation compilation, String outputPath)
   at Bicep.Cli.Commands.BuildCommand.RunAsync(BuildArguments args)
   at Bicep.Cli.Program.RunAsync(String[] args)
   at Bicep.Cli.Program.Main(String[] args)
   at Bicep.Cli.Program.<Main>(String[] args)
@GABRIELNGBTUC GABRIELNGBTUC changed the title When using a parameter with a user-defined type array, template compilation fails When using a parameter with a user-defined type array and compile time imports, template compilation fails Oct 5, 2023
@GABRIELNGBTUC GABRIELNGBTUC changed the title When using a parameter with a user-defined type array and compile time imports, template compilation fails When using a parameter with an imported user-defined type array, template compilation fails Oct 5, 2023
@jeskew jeskew self-assigned this Oct 5, 2023
@jeskew jeskew added the bug Something isn't working label Oct 5, 2023
@GABRIELNGBTUC
Copy link
Author

I started compiling more templates where non array custom types are defined and they are failing as well.

The sample modified to the following is also failing:

import {Foo} from 'types.bicep'

param foo Foo = {
  bar: false
}

v0.21.1:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.21.1.54444",
      "templateHash": "13878968526056156201"
    }
  },
  "definitions": {
    "Foo": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "bool"
        }
      },
      "metadata": {
        "__bicep_imported_from!": {
          "sourceTemplate": "types.bicep"
        }
      }
    }
  },
  "parameters": {
    "foo": {
      "$ref": "#/definitions/Foo",
      "defaultValue": {
        "bar": false
      }
    }
  },
  "resources": []
}

v0.22.6:

WARNING: The following experimental Bicep features have been enabled: User-defined functions, Compile-time imports. Experimental features should be enabled for testing purposes only, as there are no guarantees about the quality or stability of these features. Do not enable these settings for any production usage, or your production environment may be subject to breaking.
Unhandled exception. System.ArgumentException: Invalid type expression encountered.
   at Bicep.Core.Emit.TemplateWriter.TypePropertiesForTypeExpression(TypeExpression typeExpression)
   at Bicep.Core.Emit.TemplateWriter.<>c__DisplayClass61_0.<EmitOutput>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WriteObjectWithPosition(IPositionable sourcePosition, Action propertiesFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObject(Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.<>c__DisplayClass32_0.<EmitObjectProperty>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WritePropertyWithPosition(IPositionable keyPosition, String name, Action valueFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitProperty(String propertyName, Action writeValueFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObjectProperty(String propertyName, Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.TemplateWriter.EmitOutput(ExpressionEmitter emitter, DeclaredOutputExpression output)
   at Bicep.Core.Emit.TemplateWriter.<>c__DisplayClass60_0.<EmitOutputsIfPresent>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WriteObjectWithPosition(IPositionable sourcePosition, Action propertiesFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObject(Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.<>c__DisplayClass32_0.<EmitObjectProperty>b__0()
   at Bicep.Core.Emit.PositionTrackingJsonTextWriter.WritePropertyWithPosition(IPositionable keyPosition, String name, Action valueFunc)
   at Bicep.Core.Emit.ExpressionEmitter.EmitProperty(String propertyName, Action writeValueFunc, IPositionable position)
   at Bicep.Core.Emit.ExpressionEmitter.EmitObjectProperty(String propertyName, Action writePropertiesFunc, IPositionable position)
   at Bicep.Core.Emit.TemplateWriter.EmitOutputsIfPresent(ExpressionEmitter emitter, ImmutableArray`1 outputs)
   at Bicep.Core.Emit.TemplateWriter.GenerateTemplateWithoutHash(PositionTrackingJsonTextWriter jsonWriter)
   at Bicep.Core.Emit.TemplateWriter.Write(SourceAwareJsonTextWriter writer)
   at Bicep.Core.Emit.TemplateEmitter.<>c__DisplayClass10_0.<Emit>b__0()
   at Bicep.Core.Emit.TemplateEmitter.EmitOrFail(Func`1 write)
   at Bicep.Core.Emit.TemplateEmitter.Emit(TextWriter textWriter)
   at Bicep.Core.Emit.TemplateEmitter.Emit(Stream stream)
   at Bicep.Cli.Services.CompilationWriter.ToStream(SemanticModel model, Stream stream)
   at Bicep.Cli.Services.CompilationWriter.ToFile(SemanticModel model, String outputPath)
   at Bicep.Cli.Services.CompilationWriter.ToFile(Compilation compilation, String outputPath)
   at Bicep.Cli.Commands.BuildCommand.RunAsync(BuildArguments args)
   at Bicep.Cli.Program.RunAsync(String[] args)
   at Bicep.Cli.Program.Main(String[] args)
   at Bicep.Cli.Program.<Main>(String[] args)

If the Foo type is defined in the main.bicep the template compiles successfully.

@jeskew
Copy link
Contributor

jeskew commented Oct 5, 2023

Thanks for reporting, @GABRIELNGBTUC! This reflected a gap in the test coverage for type imports that should be addressed by the linked PR.

If this bug is blocking you from executing needed deployments, there is a workaround you can use. The underlying issue will cause the error you encountered when an imported type is used in a param or output statement, but imported types in type statements are handled. You can therefore use an extra type statement to unblock your deployment:

import {Foo} from 'types.bicep'

type myFoo = Foo // <-- creating an alias for use in param and output statements avoids the issue
param foo myFoo = {
  bar: false
}

jeskew added a commit that referenced this issue Oct 6, 2023
…ses (#12055)

Resolves #12052 

A recent PR (#12048) updated TemplateWriter to preprocess a template's
references to imported symbols. This new step uses an
`ExpressionRewriteVisitor`, which had not been kept up to date when new
properties were added various kinds of IR expressions.

The PR also adds a few more lines to the `Imports_LF` baseline scenario
to exercise additional behavior. The scenario had originally been
written to validate the mechanics of importing types and variables but
didn't cover the mechanics of *using* imported symbols in a template.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/12055)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Needs: Triage 🔍
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants