Support outputs with the same name as a variable
Is your feature request related to a problem? Please describe.
I often have templates where I determine the name of a resource as a variable, and need to pass this back to the calling script as an output. In JSON ARM templates I can use the same name for both the variable and the output (e.g. functionAppName for both). However, in Bicep, the two share a global namespace and I can't use the same name for both. I end up having to create suboptimal variable names to deal with this (e.g. the variable is named functionAppNameVar while the output remains as functionAppName).
It makes perfect sense for parameters, variables, and resources to share a namespace, since they can all be referred to within the Bicep template. However, outputs can only be set and can't be read from, so to me it seems like they could live in their own namespace.
Describe the solution you'd like Please consider either:
- Supporting having outputs with the same names as variables; or
- For this scenario, somehow allow outputs to be used within the main template, so the output itself becomes the variable (eliminating the need for duplicate names).
Thanks @johndowns. I'm going to close this as a duplicate of #148 and we can continue the discussion there
Actually going to leave it open. Not totally clear to me if #148 is the same solution as what is asked for here, or if they are different approaches to solve the same problem
Added the revisit label to ensure it is addressed as part of the solution for #148
I am finding myself in a similar position when converting existing ARM templates, we tend to have a similar set of input parameters for a template and will frequently output variables created within the template and also directly output a parameter for consistency. Whilst this may seem somewhat weird it allows for clean chaining of template execution from initial parameter input through multiple templates via output parameters:
"outputs": {
"clientPrefix": {
"type": "string",
"value": "[parameters('clientPrefix')]"
},
"deployEnvironment": {
"type": "string",
"value": "[parameters('deployEnvironment')]"
}
}
We had a discussion about this last week and we agree. We're going to work on this for 0.3.
We had a discussion about this issue within our team and these are the options we are considering.
Current syntax
var _foo = 42
output foo int = _foo
The main issue here is that placing outputs in the same namespace as other symbolic names (params, vars, resources, modules) adds an additional burden ot the user by forcing them to come up with a 2nd unique identifier.
Option 1
We could simply put outputs in a separate namespace like this:
var foo = 42
output foo int = foo
This syntax is confusing because it looks like the 2nd line has a circular dependency even though it actually does not. It also deviates from our existing syntax by introducing new uniqueness rules for some identifiers.
Option 2
We could allow outputs to be referenced just like variables (codegen would generate both):
// variable AND output
output foo string = 42
This is strange conceptually as "outputs" don't imply "variables". We also cannot easily invert the relationship and make all variables outputs because creating an output has to be opt-in (and requires additional type information to be provided as well)
Option 3
We could add some sort of modifier to variable or output syntax like these two alternatives:
var out foo string = 42
out var foo string = 42
The consensus here was that the syntax looks really weird.
Option 4
We could use decorators to turn a variable into an output.
@out string
var foo = 42
This syntax makes it harder to find what the contract of the template is. Today outputs have their own keyword, a decorator feels like a secondary construct w.r.t. keywords.
Option 5
We could play with the keyword order like this:
var foo = 42 output int
This breaks down when object values are involved:
var bar = {
a: b
} output object
Option 6
We could introduce a new keyword as and specify the name of the output using a string token instead of an identifier token to make it obvious that it is not an identifier in the local language scope.
Alternative A
output foo as string 'foo'
Alternative B
output 'foo' as string foo
I don't actually remember what we said about this one, so hopefully someone can chime in 😊.
Option 7
We could borrow from the TypeScript export syntax to have something like the following:
var foo = 123
resource vnet ...
output foo as int
output vnet.name as vnetName string
output { a: true } as vnetName object
The one big difference is that the text immediately following the output keyword is an expression rather than an identifier name. The main downside of this approach is that it's a departure from our current style.
My personal preference right now is Option 7 but with an export keyword instead of output to make it sound more imperative and make it clearer that it's an expression being exported rather than an identifer being created.
The as part could be optional. We could borrow the default identifier rules from C#'s named tuples. We can use the last identifier in an expression as the default name. For example, export myRes.properties.uri as string would be equivalent to export myRes.properties.uri as uri string.
If we're open to breaking changes for this, I'm also in favor of option 7.
I'd also suggest switching type and identifier around so that the name becomes an optional suffix e.g. the grammar can be represented as:
'output' expression 'as' type identifier?
output foo as int
output vnet.name as string vnetName
output { a: true } as object vnet
If we're trying to avoid breaking changes, then my preference would be option 2 followed by option 5.
W.r.t output vs export - I'm more keen on output. It is familiar to anyone using ARM templates and (to me at least) implies evaluation. export in JS/TS is usually used for sharing class/function/constant functionality (more like marking a class as public in C#), and doesn't usually cause any actual evaluation.
I think breaking changes are on the table because this is a fundamental feature of the language and I'd rather get it right while we still can change it.
I think you convinced me on the output over export, so I'm fine with that. (output is a verb too.)
One minor concern I have about flipping the order of identifier and type is that it's the opposite from every other place in the language, but it's worth calling out.
I would lean towards Option 7 also, I would also prefer the verb 'output' as it maintains an association with how ARM templates themselves describe these (as outputs)
One minor concern I have about flipping the order of identifier and type is that it's the opposite from every other place in the language, but it's worth calling out.
Yeah I think that's a fair concern. My motivation for flipping identifier and type is that it flows more naturally as you're typing - after you've typed 'as', it's unambiguous that the next identifier will be a type. I'd expect that to also benefit completions.
You can stop getting notifications by unwatching the repo:

OK, if I understand this then where today I would put
output vnetId string = vnet.id
output privatelinkId string = privatelink.id
the proposed, variant of option 7 syntax, would be
output vnet.id as string vnetId
output privatelink.id as string privatelinkIid
which I'm ok with (I prefer seeing output) but crucially the last parameter is optional so if I just used
output myStringParam as string
I could reference it as blah.outputs.myStringParam which is something we can't do today because of the namespace restrictions. Happy with the goal but don't know what's involved in the implementation, leave that to you guys :)
I like the idea of having the outputs declared at the end. . . i.e. in this case being separate from initial the variable declaration.
So I would vote for 7, since that is clear and concise.
Looking forward to this change, since having to declare a new output (variable) name, i.e. duplicating a variable, seemed odd.
One question... if we already know the variable name and type, couldn't you just use the following ?
param app string {
default: 'HUB'
allowed: [
'HUB'
'ADF'
]
}
var enviro = '${environment}${deploymentid}'
output enviro
output app
Also can we assume output of a parameter would work the same as variables?
or what we are recommending in 7 below
param app string {
default: 'HUB'
allowed: [
'HUB'
'ADF'
]
}
var enviro = '${environment}${deploymentid}'
output enviro as string
output app as string
@brwilkinson Unfortunately, there are cases where we actually don't know the type. Some examples include:
- array indexers whose index is an expression whose value can't be determined at compile time (expression type is inferred as
any) - discriminated object types when we don't know the value of the discriminator property
To add to marcin's comment, even if we did know the type, there is an opportunity for a subtle breaking change. If you change enviro to:
var enviro = 7
Then your output type will change from string to int even though the output declaration did not change. By requiring type, on the output, this would be flagged in the compiler.
thanks @majastrz @alex-frankel sounds fair.
I like this change, as 7 ... i was only yesterday that i was cursing about having to create new output token names :)
was that a yes on the output for param would behave the same as var ?
I like the proposed change. It will be nice to not worry about name collision.
was that a yes on the output for param would behave the same as var ?
With option 7, the tokens after the output keyword would be an expression, which could be the identifier of a parameter. So yeah, something like that would work:
param myParam string
output myParam as string
Since I opened this issue I probably should comment! I like option 7 too and definitely prefer output over exports. It's a really nice bonus that it will work for parameters too.