-
Notifications
You must be signed in to change notification settings - Fork 601
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: be more graceful when trying to access the value of an invalid expression template #665
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This makes me a little nervous because it creates a situation where this function produces an incomplete result without indicating any errors, which means that an incorrect result could "leak out" into another system and make it fail in a confusing way.
As far as I remember we haven't yet had a situation where the evaluator needs to directly respond to something that was already found to be invalid during the parsing phase, but I had a similar problem in another project and solved it in a slightly different way that perhaps we could emulate here:
Add a new type
hclsyntax.ExprSyntaxError
which implementshclsyntax.Expression
and is defined something like this:The
Value
implementation of this would just immediately return(Placeholder, ParseDiags)
.Change the parser so that in situations like this where expression parsing fails so badly that we can't generate any expression node at all we insert an
*ExprSyntaxError
node instead ofnil
, and have the parser copy its syntax-related diagnostics intoParseDiags
and generate some suitable placeholder value, which could becty.DynamicVal
as a last resort when the parser doesn't have enough information to insert anything more specific.(In some cases we might have enough information to return a better-typed unknown value, but that isn't true here since an invalid function call could've been to any function and therefore could return a value of any type.)
The effect then would be that if a syntax error during parsing causes us to be unable to produce an evaluable expression then we'll arrange so that any attempt to evaluate that incomplete result will just return the same parse error again, including all of the same details the parser would've reported earlier.
This should then allow us to produce incomplete results that don't require so much care when handling, and avoid having to add explicit
nil
checks all over the rest of this package to deal with that special situation.Do you think that seems plausible as an answer here? I wonder if we can find other situations in the parser where we're currently returning
nil
for an expression parse error that could be treated in the same way we add this new expression node type. 🤔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 agree! And I really like your suggestion of introducing a new type
hclsyntax.ExprSyntaxError
– especially since it feels like there are a ton of places that now havenil
in expressions where they weren't formerly expected (not just in this package, also in terraform-schema, hcl-lang).Yes! I'll give implementing this
ExprSyntaxError
a try and will loop you into the new PR 👍edit: PR