From de9e778eabefbd2ad814f44f7178dba68fce269f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 8 Dec 2025 08:49:31 +0000 Subject: [PATCH] Handle file-level comments for files with only blocks Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com> --- pkg/yqlib/decoder_hcl.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/decoder_hcl.go b/pkg/yqlib/decoder_hcl.go index a82e8f64..655c0cab 100644 --- a/pkg/yqlib/decoder_hcl.go +++ b/pkg/yqlib/decoder_hcl.go @@ -140,11 +140,23 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) { body := dec.file.Body.(*hclsyntax.Body) sortedAttrs := sortedAttributes(body.Attributes) - // Extract file-level head comments from before the first attribute + // Extract file-level head comments from before the first attribute or block var fileComment string + var firstPos int = -1 + if len(sortedAttrs) > 0 { - firstAttrPos := sortedAttrs[0].Attr.Range().Start.Byte - fileComment = extractHeadComment(dec.fileBytes, firstAttrPos) + firstPos = sortedAttrs[0].Attr.Range().Start.Byte + } + + if len(body.Blocks) > 0 { + blockPos := body.Blocks[0].Range().Start.Byte + if firstPos == -1 || blockPos < firstPos { + firstPos = blockPos + } + } + + if firstPos != -1 { + fileComment = extractHeadComment(dec.fileBytes, firstPos) if fileComment != "" { root.HeadComment = fileComment }