mikefarah/yq · error

bad file '%v': %w

Error message

bad file '%v': %w

What it means

The stream evaluator decodes documents one at a time from each input file. Any decode error other than io.EOF is wrapped as `bad file '<filename>': <cause>` so the user knows which file failed to parse. This does not indicate a yq bug, but malformed input for the selected decoder.

Source

Thrown at pkg/yqlib/stream_evaluator.go:88

	return nil
}

func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer, decoder Decoder) (uint, error) {
	filename = resolveFilename(filename)

	var currentIndex uint
	err := decoder.Init(reader)
	if err != nil {
		return 0, err
	}
	for {
		candidateNode, errorReading := decoder.Decode()

		if errors.Is(errorReading, io.EOF) {
			s.fileIndex = s.fileIndex + 1
			return currentIndex, nil
		} else if errorReading != nil {
			return currentIndex, fmt.Errorf("bad file '%v': %w", filename, errorReading)
		}
		candidateNode.document = currentIndex
		candidateNode.filename = filename
		candidateNode.fileIndex = s.fileIndex

		inputList := list.New()
		inputList.PushBack(candidateNode)

		result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
		if errorParsing != nil {
			return currentIndex, errorParsing
		}
		err := printer.PrintResults(result.MatchingNodes)

		if err != nil {
			return currentIndex, err
		}
		currentIndex = currentIndex + 1

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Validate the offending file with a format-specific parser (e.g. `xmllint --noout`, `jq . file`) and fix the syntax error
  2. Ensure the input format flag (`-p`/`--input-format`) matches the actual file contents
  3. Check file encoding — strip BOM or convert to UTF-8 if the decoder chokes on it
  4. Re-download or restore the corrupted file

Example fix

// before
yq -p=json eval '.a' data.xml
// after
yq -p=xml eval '.a' data.xml
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate the file parses with the expected decoder before evaluation
data, _ := os.ReadFile(filename)
if json.Valid(data) && inputFormat == "xml" {
	return fmt.Errorf("%s looks like JSON but input-format is xml", filename)
}

Try / catch

results, err := evaluator.EvaluateFiles(filenames, ...)
if err != nil {
	var badFileErr error
	if strings.HasPrefix(err.Error(), "bad file '") {
		// extract filename, log, and skip or fail per file
	}
	return err
}

Prevention

When it happens

Trigger: Running `yq eval-files` (or a multi-file evaluation) where one of the input files is not valid for the chosen input format — e.g. malformed XML/JSON/CSV passed to the XML/JSON decoder — so decoder.Decode() returns a non-EOF error.

Common situations: Passing a file with the wrong extension/format flag (JSON content parsed as XML), truncated downloads, files with BOM or encoding issues, or empty/corrupt files in a glob-expanded list.

Related errors


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