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 output and console show multi-line strings with an additional newline #31657

Open
prashantv opened this issue Aug 17, 2022 · 9 comments
Labels
documentation new new issue not yet triaged

Comments

@prashantv
Copy link

Terraform Version

Terraform v1.2.7
on darwin_arm64

Terraform Configuration Files

locals {
  v1 = "foo\nbar"
}

resource "local_file" "out" {
  content = local.v1
  filename = "out"
}

output "v1" {
  value = local.v1
}

Expected Behavior

When running terraform apply, the output variable is printed to the console correct;ly

Actual Behavior

After running terraform apply, it prints the output variable with an additional newline:

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Outputs:

v1 = <<EOT
foo
bar
EOT

If I take the exact same heredoc and use it for the v1 variable, the output file has a trailing newline (which the value "foo\nbar" does not).

Additional Context

The same issue occurs in the console, it shows an additional newline for multi-line strings

@prashantv prashantv added bug new new issue not yet triaged labels Aug 17, 2022
@crw
Copy link
Collaborator

crw commented Aug 24, 2022

Thanks for the bug report!

@SamrathPalSingh
Copy link

I can see this in the docs:

A heredoc string consists of:

  • An opening sequence consisting of:
  • A heredoc marker (<< or <<- — two less-than signs, with an optional hyphen for indented heredocs)
  • A delimiter word of your own choosing
  • A line break
  • The contents of the string, which can span any number of lines
  • The delimiter word you chose, alone on its own line (with indentation allowed for indented heredocs)

This info is located here.

@crw - so the trailing new line is expected, right ? or am I misunderstanding the issue.

@bgshri
Copy link

bgshri commented Sep 5, 2022

Actual output of the example mentioned in the issue:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

v1 = <<EOT
foo
bar

EOT

Expected :

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

v1 = <<EOT
foo
bar
EOT`

@crw
Copy link
Collaborator

crw commented Sep 9, 2022

@SamrathPalSingh I'm not sure, I think it depends if you think EOT should be registered as a blank line in the string. I'm sure there is prior art in other languages to examine, but in the meantime I'll check with the devs on this particular implementation detail.

@crw
Copy link
Collaborator

crw commented Sep 9, 2022

The definitive answer: it is a part of the HCL implementation/spec that the newline at the end of each line of a HEREDOC is preserved exactly. It is a quirk of our CLI value rendering that we render multi-line strings by pretending it's a HEREDOC, as part of our mission to show values in the UI in a similar way to how users would read them in the configuration. This abstraction leaks a little here because there isn't any way to write a HEREDOC without a trailing newline in HCL.

We could potentially change it to return a single-line quoted string with \n sequences in it instead, which would be a more honest interpretation of the string but would also arguably be less useful for the intended purpose of making it easier for humans to inspect the string.

Given the above, it might make sense to document this behavior so that it is not a surprise. That said, I'm not entirely sure where this documentation would go, since it is less to do with HEREDOC usage and more to do with a quirk of how it is output. In any case, I will flag it as a documentation issue and raise it with the docs team. Thanks for the feedback on this!

@crw crw added documentation and removed bug labels Sep 9, 2022
@prashantv
Copy link
Author

In our own tooling that generates HCL, we're planning to use chomp for multi-line strings that don't end with a newline.

Is it possible to do something similar in the terraform console output? (since the current representation isn't accurate)

@crw
Copy link
Collaborator

crw commented Mar 14, 2023

The choice to render multi-line strings as HEREDOC is a quirk of Terraform CLI, and the rendering has to end with a newline (that may not exist!) to fit the HEREDOC format. Per the previous comment, the choice would seem to be between getting rid of the HEREDOC formatting entirely in favor of a less-human-readable format (e.g. foo\nbar) or keeping it as-is and documenting the quirky output.

@prashantv
Copy link
Author

I understand and agree that HEREDOC is more readable.

My suggestion was to keep using HEREDOC but add a chomp to indicate that there is no new line at the end in the output, e.g.,

v1 = chomp(<<EOT
foo
bar
EOT
)

That way it's correct if copy+pasted, and maintains readability.

I think documentation wouldn't have helped me unless it was inline in the output.

@crw
Copy link
Collaborator

crw commented Mar 15, 2023

That is more clear. Thanks for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants