From 3d59c8b54dbc0af957328d1cff8ff9cf6a70b602 Mon Sep 17 00:00:00 2001 From: cv-dote <74953312+cv-dote@users.noreply.github.com> Date: Thu, 30 Apr 2026 19:06:07 +0100 Subject: [PATCH] Fix CRLF line endings causing extra blank lines between comments --- cft/format/crlf_test.go | 49 +++++++++++++++++++++++++++++++++++++++++ cft/parse/parse.go | 1 + 2 files changed, 50 insertions(+) create mode 100644 cft/format/crlf_test.go diff --git a/cft/format/crlf_test.go b/cft/format/crlf_test.go new file mode 100644 index 00000000..23f38757 --- /dev/null +++ b/cft/format/crlf_test.go @@ -0,0 +1,49 @@ +package format_test + +import ( + "testing" + + "github.com/aws-cloudformation/rain/cft/format" + "github.com/aws-cloudformation/rain/cft/parse" +) + +func TestCRLFComments(t *testing.T) { + // Issue #479: Windows CRLF line endings cause extra blank lines between comments + // Build input with CRLF line endings + input := "AWSTemplateFormatVersion: 2010-09-09\r\n" + + "\r\n" + + "# Comment line 1\r\n" + + "# Comment line 2\r\n" + + "# Comment line 3\r\n" + + "\r\n" + + "Description: Hello World\r\n" + + // Same input with LF line endings for comparison + inputLF := "AWSTemplateFormatVersion: 2010-09-09\n" + + "\n" + + "# Comment line 1\n" + + "# Comment line 2\n" + + "# Comment line 3\n" + + "\n" + + "Description: Hello World\n" + + tmplCRLF, err := parse.String(input) + if err != nil { + t.Fatalf("Failed to parse CRLF input: %v", err) + } + + tmplLF, err := parse.String(inputLF) + if err != nil { + t.Fatalf("Failed to parse LF input: %v", err) + } + + outputCRLF := format.String(tmplCRLF, format.Options{}) + outputLF := format.String(tmplLF, format.Options{}) + + t.Logf("CRLF output:\n---\n%s\n---", outputCRLF) + t.Logf("LF output:\n---\n%s\n---", outputLF) + + if outputCRLF != outputLF { + t.Errorf("CRLF and LF outputs differ.\nCRLF output:\n%s\nLF output:\n%s", outputCRLF, outputLF) + } +} diff --git a/cft/parse/parse.go b/cft/parse/parse.go index 162ec581..9fa4b2f6 100644 --- a/cft/parse/parse.go +++ b/cft/parse/parse.go @@ -58,6 +58,7 @@ func Map(input map[string]interface{}) (*cft.Template, error) { // String returns a cft.Template parsed from a string func String(input string) (*cft.Template, error) { + input = strings.ReplaceAll(input, "\r", "") var n yaml.Node err := yaml.Unmarshal([]byte(input), &n) if err != nil {