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

terraform lexer errors on heredoc #2162

Closed
MichaHoffmann opened this issue Jun 15, 2022 · 6 comments
Closed

terraform lexer errors on heredoc #2162

MichaHoffmann opened this issue Jun 15, 2022 · 6 comments

Comments

@MichaHoffmann
Copy link

MichaHoffmann commented Jun 15, 2022

Terraform lexer has problems when heredoc templates start with "<<" instead of "<<-", to prove i applied the following patch:

index 1d73734d..ab0ba809 100644
--- a/tests/snippets/terraform/test_heredoc.txt
+++ b/tests/snippets/terraform/test_heredoc.txt
@@ -1,6 +1,6 @@
 ---input---
 resource "local_file" "heredoc" {
-  content = <<-DOC
+  content = <<DOC
     heredoc content
     DOC
 }

And the parser showed alot of errors in the tests:

========================================== FAILURES ===========================================
__________________________ tests/snippets/terraform/test_heredoc.txt __________________________
The tokens produced by the "terraform" lexer differ from the expected ones in the file "tests/snippets/terraform/test_heredoc.txt".
Run `pytest tests/snippets --update-goldens` to update it.

    'resource'    Keyword.Reserved
    ' '           Text.Whitespace
    '"local_file"' Name.Class
    ' '           Text.Whitespace
    '"heredoc"'   Name.Variable
    ' '           Text.Whitespace
    '{'           Punctuation
    '\n'          Text.Whitespace

    '  '          Text.Whitespace
    'content'     Name.Attribute
    ' '           Text.Whitespace
    '='           Operator
    ' '           Text.Whitespace
  - '<<-'         Operator
  ?    -
  + '<<'          Operator
  ?     +
    'DOC'         Literal.String.Delimiter
    '\n'          Literal.String.Heredoc

  - '    heredoc content\n' Literal.String.Heredoc
  + '    heredoc content\n' Error

  - '    DOC\n'   Literal.String.Delimiter
  + '    DOC\n'   Error
  +
  + '}\n'         Error
  +
  + '    '        Text.Whitespace
  + 'h'           Error
  + 'e'           Error
  + 'r'           Error
  + 'e'           Error
  + 'd'           Error
  + 'o'           Error
  + 'c'           Error
  + ' '           Text.Whitespace
  + 'c'           Error
  + 'o'           Error
  + 'n'           Error
  + 't'           Error
  + 'e'           Error
  + 'n'           Error
  + 't'           Error
  + '\n'          Text.Whitespace
  +
  + '    '        Text.Whitespace
  + 'D'           Error
  + 'O'           Error
  + 'C'           Error
  + '\n'          Text.Whitespace

    '}'           Punctuation
    '\n'          Text.Whitespace
=================================== short test summary info ===================================
FAILED tests/snippets/terraform/test_heredoc.txt::

According to the spec both are valid introductions to heredoc templates.

@birkenfeld
Copy link
Member

From the spec you linked:

The heredoc template expression type is introduced by either << or <<-, followed by an identifier. The template expression ends when the given identifier subsequently appears again on a line of its own.

If a heredoc template is introduced with the <<- symbol, any literal string at the start of each line is analyzed to find the minimum number of leading spaces, and then that number of prefix spaces is removed from all line-leading literal strings. The final closing marker may also have an arbitrary number of spaces preceding it on its line.

This reads to me as if your changed test is not valid, since the final delimiter DOC appears not at the start of a line, which is not allowed in heredocs starting with <<.

@mhoffm-aiven
Copy link

The terraform tool accepts that style without problems though:

[michael.hoffmann@fedora ~]$ terraform fmt test.tf 
test.tf
[michael.hoffmann@fedora ~]$ cat test.tf 
resource "foo" "bar" {
  x = <<EOF
    testing
  EOF
}

@mhoffm-aiven
Copy link

Ah testing with DOC without leading whitespace works though! I get what you say

@birkenfeld
Copy link
Member

Hm, but if terraform itself is happy with the leading whitespace we shouldn't highlight it as error.

@birkenfeld
Copy link
Member

Fixed!

@mhoffm-aiven
Copy link

Thanks @birkenfeld !

laysauchoa added a commit to aiven/devportal that referenced this issue Jul 6, 2022
Add an updated version of pygments.
This helps to not depend on the sphinix
version or other package to be updated to
use pygments on v2.12.0

Previous versions have issues supporint
terraform highlight.

This avoid this error:

```
WARNING: Could not lex literal_block as "terraform". Highlighting skipped.
```

You can check out more info:
pygments/pygments#2162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants