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

lang/funcs: templatefile requires valid variable names #24184

Merged
merged 2 commits into from
Feb 25, 2020

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Feb 22, 2020

Previously the templatefile function would permit any arbitrary string as a variable name, but due to the HCL template syntax it would be impossible to refer to one that isn't a valid HCL identifier without causing an HCL syntax error.

The HCL syntax errors are correct, but don't really point to the root cause of the problem. Instead, we'll pre-verify that the variable names are valid before we even try to render the template, and given a specialized error message that refers to the vars argument expression as the problematic part, which will hopefully make the resolution path clearer for a user encountering this situation.

invalid template variable name "<invalid>": must start with
a letter, followed by zero or more letters, digits, and
underscores.

The syntax error still remains for situations where all of the variable names are correct but e.g. the user made a typo referring to one, which makes sense because in that case the problem is inside the template.

I've marked this as a breaking change because technically a user could previously have had a templatefile call with an invalid variable name as long as they didn't try to refer to that variable name in the template itself. While that situation seems unlikely, we will still call it out explicitly in the changelog once merged, and we must not backport this change to a Terraform 0.12 minor release.

This closes #23789.

This function has a number of different error cases with hopefully-helpful
error messages for each, so it's good to test we're getting the error
message we were actually expecting in each case.
Previously the templatefile function would permit any arbitrary string as
a variable name, but due to the HCL template syntax it would be impossible
to refer to one that isn't a valid HCL identifier without causing an
HCL syntax error.

The HCL syntax errors are correct, but don't really point to the root
cause of the problem. Instead, we'll pre-verify that the variable names
are valid before we even try to render the template, and given a
specialized error message that refers to the vars argument expression as
the problematic part, which will hopefully make the resolution path
clearer for a user encountering this situation.

The syntax error still remains for situations where all of the variable
names are correct but e.g. the user made a typo referring to one, which
makes sense because in that case the problem _is_ inside the template.
@apparentlymart apparentlymart requested a review from a team February 22, 2020 00:40
@apparentlymart apparentlymart self-assigned this Feb 22, 2020
@ghost ghost added the sdkv1 [PRs only] Marks changes that may potentially need to be ported to the plugi nSDK label Feb 22, 2020
@apparentlymart apparentlymart merged commit 67d95b9 into master Feb 25, 2020
@ghost
Copy link

ghost commented Apr 1, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
@pselle pselle deleted the f-templatefile-validation branch March 4, 2021 20:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking-change bug config sdkv1 [PRs only] Marks changes that may potentially need to be ported to the plugi nSDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keys for substitution in Template file function must begin with letter, not number
2 participants