how to get bicep AST #17613
-
Hello colleagues, The approach to building infrastructures using bicep I typically follow is, sort of "config-based". I make "config" files which hold full configuration of a target setup, and then reference them. In simple cases it is one-to-one mapping between a "config" file and a core bicep module, like this flowchart TD
A[config.bicep] -->|included in| B(main.bicep)
B -->D[Module A]
B -->E[Module B]
B -->F[Module C]
However in some cases, "configuration" becomes more complex: flowchart TD
A1[shared-config.bicep]
A2[shared-functions.bicep]
A1 --> |included in| G1(regionA-config.bicep)
A1 --> |included in| G2(regionB-config.bicep)
A2 --> |included in| G1(regionA-config.bicep)
A2 --> |included in| G2(regionB-config.bicep)
G1 --> |via bicepparam| B(main.bicep)
G2 --> |via bicepparam| B(main.bicep)
B -->D[Module A]
B -->E[Module B]
B -->F[Module C]
One example of this could be a set of "naming conventions" defined once for all "regional templates". Another example is a set of resources, which should be shared across multiple environments. However, this creates a bit of a problem, because as this becomes more complex, it is harder to keep all these things in mind. So you have to go back to refresh your mind and find dependencies defined between configuration objects. So here is the question. I believe if I could build abstract syntax tree (AST) on top of those configuration files. I'd hope to see dependencies between configuration object themselves. Not between configuration files, but rather objects, defined in those files and, which is more valuable, parameters, values of those objects. Say, I have a common storage account object, defined in the I know that there is a visualizer built-in into VSCode extension, but it displays cross-module dependencies, but I'm looking for cross-object ones, I guess. Next level of the same scenario is to find cross-resource dependencies, or, perhaps, better to say a resource-level topology ... But this is, I guess needs some experimenting with AST. So, my main question is:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
When I wrote bicep-docs I used Tree Sitter to generate the AST for the file. Extending the documentation to map dependencies (and diagram them) is something I'd love to add to it 🙂 |
Beta Was this translation helpful? Give feedback.
-
We publish a NuGet package that you can use for this: https://www.nuget.org/packages/Azure.Bicep.Core Here's an example of writing out the syntax tree: https://github.com/anthony-c-martin/samples/blob/main/csharp/bicep-access-syntax-tree/Program.cs - you can try this out here. For undertanding dependencies between declarations in a file, you're probably going to be more interested in the symbols, which you can also access from the semantic model. See here for an example where we build a symbol graph from the semantic model, and use it to check for cycles: bicep/src/Bicep.Core/TypeSystem/CyclicCheckVisitor.cs Lines 28 to 32 in 2f55436 |
Beta Was this translation helpful? Give feedback.
We publish a NuGet package that you can use for this: https://www.nuget.org/packages/Azure.Bicep.Core
Here's an example of writing out the syntax tree: https://github.com/anthony-c-martin/samples/blob/main/csharp/bicep-access-syntax-tree/Program.cs - you can try this out here.
For undertanding dependencies between declarations in a file, you're probably going to be more interested in the symbols, which you can also access from the semantic model. See here for an example where we build a symbol graph from the semantic model, and use it to check for cycles:
bicep/src/Bicep.Core/TypeSystem/CyclicCheckVisitor.cs
Lines 28 to 32 in 2f55436