mikefarah/yq · error

no hcl file parsed

Error message

no hcl file parsed

What it means

Decode was called while dec.file is nil, i.e. no HCL document was ever stored by a successful Init. This is a defensive sentinel in the decoder: it fires when Decode runs before Init, or after Init failed and its error was ignored — the input itself is not at fault, the decoder lifecycle is.

Source

Thrown at pkg/yqlib/decoder_hcl.go:134

	file, diags := hclsyntax.ParseConfig(data, "input.hcl", hcl.Pos{Line: 1, Column: 1})
	if diags != nil && diags.HasErrors() {
		return fmt.Errorf("hcl parse error: %w", diags)
	}
	dec.file = file
	dec.fileBytes = data
	dec.readAnything = false
	dec.documentIndex = 0
	return nil
}

func (dec *hclDecoder) Decode() (*CandidateNode, error) {
	if dec.readAnything {
		return nil, io.EOF
	}
	dec.readAnything = true

	if dec.file == nil {
		return nil, fmt.Errorf("no hcl file parsed")
	}

	root := &CandidateNode{Kind: MappingNode}

	// process attributes in declaration order
	body := dec.file.Body.(*hclsyntax.Body)
	firstAttr := true
	for _, attrWithName := range sortedAttributes(body.Attributes) {
		keyNode := createStringScalarNode(attrWithName.Name)
		valNode := convertHclExprToNode(attrWithName.Attr.Expr, dec.fileBytes)

		// Attach comments if any
		attrRange := attrWithName.Attr.Range()
		headComment := extractHeadComment(dec.fileBytes, attrRange.Start.Byte)
		if firstAttr && headComment != "" {
			// For the first attribute, apply its head comment to the root
			root.HeadComment = headComment
			firstAttr = false

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Ensure Decoder.Init is called (and its error checked) before Decode
  2. If Init returned 'hcl parse error', fix the input syntax first
  3. Treat this as an internal API misuse: never swallow the Init error before decoding
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/yqlib/decoder_hcl.go:134 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/cd488a843a662be8. Report an issue: GitHub.