mikefarah/yq · error

bad file '%v': %w

Error message

bad file '%v': %w

What it means

readDocuments decodes input documents for evaluation (used both by CLI file evaluation and test helpers). A decode error other than io.EOF is reported as `bad file '<filename>': <cause>`, wrapping the underlying parser failure so the user can identify the failing input.

Source

Thrown at pkg/yqlib/utils.go:79

	filename = resolveFilename(filename)
	err := decoder.Init(reader)
	if err != nil {
		return nil, err
	}
	inputList := list.New()
	var currentIndex uint

	for {
		candidateNode, errorReading := decoder.Decode()

		if errors.Is(errorReading, io.EOF) {
			switch reader := reader.(type) {
			case *os.File:
				safelyCloseFile(reader)
			}
			return inputList, nil
		} else if errorReading != nil {
			return nil, fmt.Errorf("bad file '%v': %w", filename, errorReading)
		}
		candidateNode.document = currentIndex
		candidateNode.filename = filename
		candidateNode.fileIndex = fileIndex
		candidateNode.EvaluateTogether = true

		inputList.PushBack(candidateNode)

		currentIndex = currentIndex + 1
	}
}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Run the input through a standalone parser (`yq '.' file` or a YAML/JSON linter) to surface the underlying syntax error and fix it
  2. Confirm the decoder/format matches the data (input-format flag or correct format in test scenarios)
  3. Check for tab characters or inconsistent indentation in YAML and reformat
  4. Inspect the wrapped %w cause in the message — it names the exact parse failure

Example fix

// before
a:
	b: 1
// after
a:
  b: 1
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-parse YAML to catch syntax errors before yq decode
var n yaml.Node
if err := yaml.Unmarshal(data, &n); err != nil {
	return fmt.Errorf("invalid YAML in %s: %w", filename, err)
}

Try / catch

docs, err := readDocuments(reader, filename, ...)
if err != nil && strings.HasPrefix(err.Error(), "bad file '") {
	// surface the wrapped %w cause and fix or skip the file
}

Prevention

When it happens

Trigger: Calling EvaluateFiles (or test helpers like processFormatScenario/assertEncodesTo) with a reader whose contents the decoder cannot parse — invalid YAML/JSON/XML for the configured decoder, or a reader returning a non-EOF error mid-stream.

Common situations: Malformed YAML (bad indentation, tabs, duplicate anchors issues), JSON with trailing commas, XML/CSV content fed to the wrong decoder in tests, or read errors from a failing file handle mid-decode.

Related errors


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