Skip to content

fix: strip YAML comments before template rendering - #152

Open
Hermsi1337 wants to merge 3 commits into
mainfrom
fix/strip-yaml-comments-before-templating
Open

fix: strip YAML comments before template rendering#152
Hermsi1337 wants to merge 3 commits into
mainfrom
fix/strip-yaml-comments-before-templating

Conversation

@Hermsi1337

Copy link
Copy Markdown
Member

Summary

Fixes #151.

The stack/services/volumes input is rendered as a Go text/template before it is parsed as YAML, so template expressions inside YAML comments were evaluated as well:

  • A commented-out line such as # db_url: {{ .Env.DATABASE_URL }} failed the whole deployment with map has no entry for key "DATABASE_URL" (because of missingkey=error).
  • Broken or partial template syntax inside a comment (# see {{) produced a misleading parse error pointing at the wrong line.

Changes

  • cmd/action/main.go: new stripYamlComments() is called in loadYamlOptional() right before template.Parse. The scanner removes comments while preserving the line count (error messages keep pointing at the correct line) and knows the cases where # is not a comment:
    • inside single- and double-quoted scalars (including multi-line double-quoted scalars),
    • when not preceded by whitespace (image: nginx#latest),
    • inside block scalars (command: | followed by #!/bin/sh).
  • cmd/action/main_test.go: 10 end-to-end regression tests plus a 14-case table test for stripYamlComments, including non-regression tests asserting that templating outside comments still works and that missingkey=error still fires there.
  • README.md: note in the "Environment Variable Parsing" section that comments are stripped before rendering, so template expressions inside comments are inert.
  • .gitattributes (separate commit): enforce LF line endings (* text=auto eol=lf) so Windows checkouts with core.autocrlf stay gofmt-clean.

Testing

  • go test ./...: 49 tests pass (26 new).
  • go vet ./...: clean.
  • golangci-lint (the Makefile's quay.io/mittwald/golangci-lint:0.0.38 image): no findings.

Known limitation

The scanner tracks quote state conservatively: an apostrophe in an unquoted scalar (e.g. description: user's app) opens single-quote state, so comments until the next apostrophe are left in place. In that case behavior simply degrades to the status quo before this PR — legitimate content is never stripped.

🤖 Generated with Claude Code

Hermsi1337 and others added 2 commits August 22, 2026 18:01
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The stack/services/volumes input is rendered as a Go text/template
before it is parsed as YAML, so template expressions inside YAML
comments were evaluated as well. A commented-out line referencing an
unset variable failed the whole deployment due to missingkey=error,
and broken template syntax inside a comment produced misleading parse
errors.

Comments are now removed before templating by a scanner that is aware
of quoted scalars, "#" without preceding whitespace, and block scalars
(including shebangs), so legitimate "#" content is preserved.

Closes #151

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

Template expressions inside YAML comments are rendered and can fail the whole deployment

1 participant