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

Trailing space on output.template start/end comments results in Error: value of '--output-template' is missing end comment #652

Open
ccampo133 opened this issue Nov 14, 2022 · 9 comments · May be fixed by #658
Labels

Comments

@ccampo133
Copy link

ccampo133 commented Nov 14, 2022

Describe the bug

Specifying the output.template value as a YAML multiline block in .terraform-docs.yml string results in the error Error: value of '--output-template' is missing end comment, despite the end comment clearly existing.

See edit at the bottom for clarification. This appears to be caused by extra whitespace around these comments.

If you specify as a regular quoted string with linebreaks, it works as expected (no error).

How can we reproduce it?

Pretty simple to reproduce. Create the following files:

main.tf:

/**
 * Hello world!
 */
resource "null_resource" "foo" {}

.terraform-docs.yml:

formatter: "markdown"

output:
  file: "README.example.md"
  mode: inject
  # Using a block style string does not appear to work...
  #... however a string with linebreaks does (uncomment to test, and comment 
  # out the block template).
  #template: "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->"
  template: |-
    <!-- BEGIN_TF_DOCS -->
    {{ .Content }}
    <!-- END_TF_DOCS -->  

And you'll see the following error:

$ terraform-docs .
Error: value of '--output-template' is missing end comment

See my example repo: https://github.com/ccampo133/terraform-docs-template-comment-bug

EDIT 2022-12-13:

As identified by @ccq1994 in this comment, it appears to be caused by trailing whitespace after the <!--END_TF_DOCS --> comment. Looking further in the code at https://github.com/terraform-docs/terraform-docs/blob/master/print/config.go#L292-L293, the whitespace of these lines are intentionally trimmed anyway, however this happens after validation is performed. I opened #658 to address this.

Environment information

terraform-docs version v0.16.0 darwin/amd64
MacOS Monterey 12.6

@ccq1994
Copy link

ccq1994 commented Dec 8, 2022

@ccampo133 I found the issue - the docs' example code has trailing spaces following <!-- END_TF_DOCS -->. I'll open a PR.

@ccq1994
Copy link

ccq1994 commented Dec 8, 2022

Upon further inspection, it's not a fault in the documentation as it's written. It's an issue with how the code blocks are rendered on terraform-docs.io. If you copy the contents from the site, it includes a lot of trailing spaces:
image

However, the code itself doesn't actually have trailing spaces. This appears to only be an issue with HTML comment blocks, not anything else.

This has to do with how the site is rendered and I have no clue how to work with that. Hopefully this helps people figure out this problem, at least.

@ccampo133
Copy link
Author

ccampo133 commented Dec 13, 2022

@ccq1994 confirmed that removing the trailing spaces does appear to resolve the issue I reported. Thanks for tracking that down!

I think it would make sense to have the code ignore/trim any whitespace following the comment.

@ccampo133 ccampo133 linked a pull request Dec 13, 2022 that will close this issue
2 tasks
@ccampo133
Copy link
Author

I opened a PR for this here: #658

@nikolaik
Copy link

nikolaik commented Mar 1, 2023

If you intent to write a newline after <!-- END_TF_DOCS --> i.e. the template <!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->\n will the fix in #658 still work?

If the newline is missing, this is something that git clients will tell you about, like explained here: https://stackoverflow.com/questions/5813311/whats-the-significance-of-the-no-newline-at-end-of-file-log

@ccampo133
Copy link
Author

ccampo133 commented Mar 1, 2023

@nikolaik yes, although I'm not sure that your question is relevant to this issue. The template in question here must be passed in the YAML file, under the output.template key - it cannot live in its own file. You can have an arbitrary amount of newlines at the end of the template block and this will work, and so far as I can tell, this has always worked, for example:

.terraform-docs.yaml:

formatter: "markdown"

output:
  file: "README.example.md"
  template: |-
    <!-- BEGIN_TF_DOCS -->
    {{ .Content }}
    <!-- END_TF_DOCS -->

  mode: inject

@ccampo133
Copy link
Author

@EppO @blakegong @khos2ow any chance this issue and corresponding PR could be merged? Or is this project unmaintained at this point?

@ccampo133 ccampo133 changed the title Using YAML block multiline string for output.template results in Error: value of '--output-template' is missing end comment Trailing space on output.template start/end comments results in Error: value of '--output-template' is missing end comment Mar 1, 2023
@RuiSMagalhaes
Copy link

RuiSMagalhaes commented Jul 20, 2023

is the related fix going to be merged ? @EppO @blakegong @khos2ow

@ccampo133
Copy link
Author

@RuiSMagalhaes I'd love to see it merged, but I don't think the project is actively maintained. However the workaround for now is simple - just to remove any extra whitespace from the template manually (see #652 (comment)).

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

Successfully merging a pull request may close this issue.

4 participants