Skip to content

[Bug Report]: Argument of type "null | string" is not assignable to parameter of type "string" warning on many modules #2641

Closed

Description

Describe the bug

Several child extensions report the subject error in VS Code mainly on the name property passed to the resource when using a reference to the parent. Here is an example from the resourceGroups nested_roleAssignments.bicep where the 'resourceId' specified in the name property is generating the error. This is just one example.

resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for principalId in principalIds: {
  name: guid(last(split(resourceId, '/')), principalId, roleDefinitionIdOrName)
  properties: {
    description: description
    roleDefinitionId: contains(builtInRoleNames, roleDefinitionIdOrName) ? builtInRoleNames[roleDefinitionIdOrName] : roleDefinitionIdOrName
    principalId: principalId
    principalType: !empty(principalType) ? any(principalType) : null
    condition: !empty(condition) ? condition : null
    conditionVersion: !empty(conditionVersion) && !empty(condition) ? conditionVersion : null
    delegatedManagedIdentityResourceId: !empty(delegatedManagedIdentityResourceId) ? delegatedManagedIdentityResourceId : null
  }
}]

I am not sure if this is a new linter test in the BICEP extension, but there are two possibilities to mitigate this error.

  1. Use the any() function to wrap the value of the referencing value (i.e., 'guid(any(last(split(resourceId, '/'))), principalId, roleDefinitionIdOrName)').
name: guid(any(last(split(resourceId, '/'))), principalId, roleDefinitionIdOrName)
  1. Use a non-null assertion postfix operator (!) just introduced in BICEP (but not documented as of now) that tells the linter that we will never have a null value for this passed variable.
name: guid(last(split(resourceId, '/'))!, principalId, roleDefinitionIdOrName)

For more information see: Azure\BICEP Github Issue #9565 and
Azure\BICEP Github Pull Request #9585.

To reproduce

Open modules\Microsoft.Resources\resourceGroups.bicep\nested_roleAssignments.bicep in Visual Studio Code and navigate to line 229. The error looks like this:

CARMLIssue

Note that this is not the only occurrence.

Code snippet

resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for principalId in principalIds: {
  name: guid(last(split(resourceId, '/')), principalId, roleDefinitionIdOrName)
  properties: {
    description: description
    roleDefinitionId: contains(builtInRoleNames, roleDefinitionIdOrName) ? builtInRoleNames[roleDefinitionIdOrName] : roleDefinitionIdOrName
    principalId: principalId
    principalType: !empty(principalType) ? any(principalType) : null
    condition: !empty(condition) ? condition : null
    conditionVersion: !empty(conditionVersion) && !empty(condition) ? conditionVersion : null
    delegatedManagedIdentityResourceId: !empty(delegatedManagedIdentityResourceId) ? delegatedManagedIdentityResourceId : null
  }
}]

Relevant log output

No response

Activity

  1. added
    bug
    on Feb 1, 2023
  2. AlexanderSehr commented on Feb 1, 2023

    @AlexanderSehr
    Contributor

    Note: In the ResourceGroup module, this VSCode warning actually shows up as an error

  3. shawntmeyer commented on Feb 1, 2023

    @shawntmeyer
    ContributorAuthor

    My vote is to use the Postfix operator. The BICEP extension actually suggests this fix. See:

    Linter1

    Linter2

  4. AlexanderSehr commented on Feb 1, 2023

    @AlexanderSehr
    Contributor

    Note: I just tested it with the RG module and at least there - it doesn't work.

    If I add the !, the VSCode error goes away - but az bicep build throws an error when compiling:

    ERROR: C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\deploy.bicep(59,38) : Error BCP104: The referenced module has errors.
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(228,10) : Warning BCP179: Unique resource or deployment name is required when looping. The loop item variable "principalId" must be referenced in at least one of the value expressions of the following properties: "name", "scope"
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(229,42) : Error BCP236: Expected a new line or comma character at this location.
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(229,43) : Error BCP009: Expected a literal value, an array, an object, a parenthesized expression, or a function call at this location.
    

    If I remove it again, the VSCode extension error is back - but the compilation works again. Will comment on the Bicep issue

  5. shawntmeyer commented on Feb 1, 2023

    @shawntmeyer
    ContributorAuthor

    Note: I just tested it with the RG module and at least there - it doesn't work.

    If I add the !, the VSCode error goes away - but az bicep build throws an error when compiling:

    ERROR: C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\deploy.bicep(59,38) : Error BCP104: The referenced module has errors.
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(228,10) : Warning BCP179: Unique resource or deployment name is required when looping. The loop item variable "principalId" must be referenced in at least one of the value expressions of the following properties: "name", "scope"
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(229,42) : Error BCP236: Expected a new line or comma character at this location.
    C:\dev\ip\DevOps-Self-Hosted\AlexanderSehr-Fork\CARML0.9\Microsoft.Resources\resourceGroups\.bicep\nested_roleAssignments.bicep(229,43) : Error BCP009: Expected a literal value, an array, an object, a parenthesized expression, or a function call at this location.
    

    If I remove it again, the VSCode extension error is back - but the compilation works again. Will comment on the Bicep issue

    what happens if you try the any() method?

  6. AlexanderSehr commented on Feb 1, 2023

    @AlexanderSehr
    Contributor

    Just upgraded Bicep to 0.14.6 too - there the ! doesn't cause any issues. Any() should work without an issue as it works differently from !.

    This puts us into a tough spot as the GitHub Runners are still operating 0.13.1 which doesn't work with that fix (as per the error message above). So if we upgrade to the fix - then our pipelines will start failing on some of the modules (like ResourceGroup).

  7. shawntmeyer commented on Feb 1, 2023

    @shawntmeyer
    ContributorAuthor

    Just upgraded Bicep to 0.14.6 too - there the ! doesn't cause any issues. Any() should work without an issue as it works differently from !.

    This puts us into a tough spot as the GitHub Runners are still operating 0.13.1 which doesn't work with that fix (as per the error message above). So if we upgrade to the fix - then our pipelines will start failing on some of the modules (like ResourceGroup).

    yeah, the ! is a new implementation (2 weeks ago :))

  8. AlexanderSehr commented on Feb 1, 2023

    @AlexanderSehr
    Contributor

    Right. I think before we can fix this we need to wait for the GitHub runners to update. It's a breaking change otherwise

  9. krbar commented on Feb 1, 2023

    @krbar
    Contributor

    We should keep the ADO build agents in mind, which may also use the 0.13.1

  10. shawntmeyer commented on Feb 1, 2023

    @shawntmeyer
    ContributorAuthor

    Is there a document outlining the release schedule for the agents in both ADO and GitHub?

  11. AlexanderSehr commented on Feb 1, 2023

    @AlexanderSehr
    Contributor

    Not as far as I know.

    An additional note: When running 0.14.6 - the Linter rule will always be a warning until we updated the modules. The one exception I'm aware of is the ResourceGroup that actually throws an error. So it will also throw an error for any customer that uses our module until it is 'updated'.

  12. eriqua commented on Feb 1, 2023

    @eriqua
    Contributor

    We definitely need to prioritize a fix for the error thrown by Bicep release 0.14.6. Luckily it should be easy, let's use the rg resourceId in the guid instead of using last and split functions to calculate the name, that would also be more consistent with the way other rbac deployments are done at resource scope.

    For the bigger picture on how to fix the warning for all rbac at resource scope, we should discuss it more thoroughly. Discuss if we want to support both versions 0.13.1 and 0.14.6, or if going with the latest and update 0.14.6 on both gh runners and ADO agents. Another point would be to add to the CARML release the Bicep version with which that was tested.

  13. eriqua commented on Feb 1, 2023

    @eriqua
    Contributor

    Not as far as I know.

    An additional note: When running 0.14.6 - the Linter rule will always be a warning until we updated the modules. The one exception I'm aware of is the ResourceGroup that actually throws an error. So it will also throw an error for any customer that uses our module until it is 'updated'.

    This should cover the error on RG RBAC #2642

  14. 10 remaining items

  15. shawntmeyer commented on Feb 6, 2023

    @shawntmeyer
    ContributorAuthor

    Hey Shawn Meyer (@shawntmeyer), I hope Erika Gressi (@eriqua) reached out to you. If not: It was an option we discussed but ultimately dropped as it would not be a best practice and overshoot the original issue.

    She did, thank you!

  16. eriqua commented on Feb 6, 2023

    @eriqua
    Contributor

    Team decides to go with
    OPT2: Wait for runners to be updated and introduce the ! operator later

  17. shawntmeyer commented on Feb 8, 2023

    @shawntmeyer
    ContributorAuthor

    Github Runners are now using latest BICEP. Not sure about ADO.

  18. eriqua commented on Feb 8, 2023

    @eriqua
    Contributor

    Hey Shawn Meyer (@shawntmeyer), this unfortunately may not be the case for all runners. From past experience, we noticed not all of them are updated at the same time and it may take a while until all will use the same version. For example I just triggered a workflow and noticed v0.13.1 is still used.

    That's also why issue #2660 was opened

  19. shawntmeyer commented on Feb 8, 2023

    @shawntmeyer
    ContributorAuthor
  20. removed
    blocked
    on Feb 15, 2023
  21. moved this from Low priority to Needs triage in Bug boardon Feb 15, 2023
  22. eriqua commented on Feb 15, 2023

    @eriqua
    Contributor

    GH runners seem to stably have Bicep 0.14.6 installed. Didn't check ADO agents yet. Moving this back to discussion to agree on if we want to create a dependency on #2660 or if proceeding with introducing the ! operator meanwhile.

    Also, an alternative fix which wasn't discussed, would be to force the last(split) function causing the warning to a string, i.e., '$last(split)'. This would work with previous Bicep versions as well.

  23. moved this from Needs triage to Low priority in Bug boardon Feb 16, 2023
  24. moved this from Low priority to Closed in Bug boardon Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[cat] modulesbug

Type

No type

Projects

  • Status
    Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions