Skip to content

command/views/json: Never generate invalid diagnostic snippet offsets#29048

Merged
apparentlymart merged 1 commit intomainfrom
b-validate-diags-crash
Jun 28, 2021
Merged

command/views/json: Never generate invalid diagnostic snippet offsets#29048
apparentlymart merged 1 commit intomainfrom
b-validate-diags-crash

Conversation

@apparentlymart
Copy link
Copy Markdown
Contributor

Because our snippet generator is trying to select whole lines to include in the snippet, it has some edge cases for odd situations where the relevant source range starts or ends directly at a newline, which were previously causing this logic to return out-of-bounds offsets into the code snippet string.

Newlines at the bounds of source ranges are inevitably a bit of an edge case when thinking about the line/column representation of positions rather than byte offsets, because newlines don't really exist as far as line/column representation is concerned: they are the boundary between the lines, not part of the lines themselves, and they therefore don't really have a real "column". Therefore we somewhat arbitrarily just decide to shift the range over to the start of the following line instead when presenting it, so we can still show something even if it's not exactly what the parser was gesturing at.

For completeness here I also added some similar logic to the human-oriented diagnostic formatter, which consumes the result of the JSON diagnostic builder. That's not really needed with the additional checks in the JSON diagnostic builder, but it's nice to reinforce that this code can't panic (in this way, at least) even if its input isn't valid.

This fixes #29041, making it no longer crash.

Arguably the source range of the error it is trying to return is also a bit off, but syntax errors are often like that because the HCL parser is just making a best guess about what the author's intention was. In any case if we want to improve that we'd be doing it over in HCL rather than in Terraform, so I've focused just on avoiding a crash here.

Because our snippet generator is trying to select whole lines to include
in the snippet, it has some edge cases for odd situations where the
relevant source range starts or ends directly at a newline, which were
previously causing this logic to return out-of-bounds offsets into the
code snippet string.

Although arguably it'd be better for the original diagnostics to report
more reasonable source ranges, it's better for us to report a
slightly-inaccurate snippet than to crash altogether, and so we'll extend
our existing range checks to check both bounds of the string and thus
avoid downstreams having to deal with out-of-bounds indices.

For completeness here I also added some similar logic to the
human-oriented diagnostic formatter, which consumes the result of the
JSON diagnostic builder. That's not really needed with the additional
checks in the JSON diagnostic builder, but it's nice to reinforce that
this code can't panic (in this way, at least) even if its input isn't
valid.
@apparentlymart apparentlymart requested a review from a team June 28, 2021 18:33
@apparentlymart apparentlymart self-assigned this Jun 28, 2021
@apparentlymart apparentlymart merged commit 70bc432 into main Jun 28, 2021
@apparentlymart apparentlymart deleted the b-validate-diags-crash branch June 28, 2021 20:42
@github-actions
Copy link
Copy Markdown
Contributor

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 contributions.
If you have 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'terraform validate' crashes, apparently caused by curly brace in a wrong place

2 participants