-
Notifications
You must be signed in to change notification settings - Fork 458
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
fix(hcl2cdk): suffix variables and locals #845
Conversation
What's the motivation for this change? |
@jsteinich When converting https://github.com/futurice/terraform-examples/tree/master/azure/azure_linux_docker_app_service I found that locals, variables, and outputs can have the same name. Previously we just used the name directly, this resulted in multiple variables with the same name. Now they are suffixed so no more collisions can happen |
Locals are certainly often better as local variables, but I wonder if they should be translated that way or instead stay as |
They will still collide in construct id space. Probably other (if not all) variables that will encounter the same issue. |
@jsteinich Ah I see. We never translated locals into |
I'm sorry, I meant locals and variables sharing the same name. Since locals are just constants there is no construct id involved. |
Probably depends what the generation ends up looking like. As long as a Terraform expression is just passed along, then probably not.
The variable and the output in your example overlap. |
It is passed along, so no problem then 👍
Ah snap, I'll add this to the known issues or is there a nice and easy way around it? |
You could do the same suffix that you are using for the variable name, though there's no guarantee that you won't then collide with a user named resource. |
3ee3222
to
395e0b5
Compare
We have this problem across all resources, I think using the same formula we use to get variable names for scopes makes sense so that they are all properly "namespaced" by their respective resource type |
packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Outdated
Show resolved
Hide resolved
packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Outdated
Show resolved
Hide resolved
395e0b5
to
445ec60
Compare
How feasible would it be to only do the variable qualifying if conflicts are detected? |
38342f5
to
044b217
Compare
@jsteinich I think this is much better, thank you! |
d1d3f38
to
d4b25d9
Compare
: [resource, name].join("_") | ||
); | ||
function validVarName(name: string) { | ||
if (!Number.isNaN(parseInt(name[0], 10))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think just a number was valid in hcl, but I could imagine invalid names coming up, i.e. reserved keywords.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am checking for reserved words in JS now. Rosetta will handle / check for reserved words in other languages afaik.
if (!Number.isNaN(parseInt(proposedName[0], 10))) { | ||
return `d${proposedName}`; | ||
// we only cache one per name | ||
return validVarName(camelCase([resource, name].join("_"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this could still collide, but probably don't need to worry about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How could it collide? There can't be two identical resources with the same name in TF, can there?
a77fcb5
to
544386b
Compare
There are more for other languages but this is a first safeguard
544386b
to
530767d
Compare
I'm going to lock this pull request because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Input
Output Before PR
Output After PR