grafana/k6 · error

could not marshal YAML: %w

Error message

could not marshal YAML: %w

What it means

yamlPrint (used to render YAML output for status/stats views in internal/cmd/ui.go) marshals its value with yaml.Marshal before printing. A marshal failure means the data contains a type YAML cannot represent (non-string map keys, channels, funcs, cycles). Because k6 feeds it fixed internal structures, this error in a stock binary indicates an internal invariant break rather than a user mistake.

Source

Thrown at internal/cmd/ui.go:400

			// Default ticker-based progress bar resizing
			if gs.Stdout.IsTTY && !terminalSizeUnknown && winch == nil {
				tw, _, err := term.GetSize(stdoutFD)
				if tw > 0 && err == nil {
					termWidth = tw
				}
			}
		}
		renderProgressBars(true)
		gs.OutMutex.Lock()
		printProgressBars()
		gs.OutMutex.Unlock()
	}
}

func yamlPrint(w io.Writer, v any) error {
	data, err := yaml.Marshal(v)
	if err != nil {
		return fmt.Errorf("could not marshal YAML: %w", err)
	}
	_, err = fmt.Fprint(w, string(data))
	if err != nil {
		return fmt.Errorf("could flush the data to the output: %w", err)
	}
	return nil
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. For stock k6, report a bug at https://github.com/grafana/k6/issues with the exact command and 'k6 version' output
  2. For custom builds, keep the printed struct limited to strings, numbers, bools, slices, and nested maps
  3. Work around by using a different output form for that data (e.g. capture the JSON equivalent)
Defensive patterns

Strategy: fallback

Try / catch

out=$(k6 status 2>&1)
if [ $? -ne 0 ] && echo "$out" | grep -q 'could not marshal YAML'; then
  echo "YAML rendering failed — likely an internal bug; falling back" >&2
  k6 version  # gather build info and report upstream
fi

Prevention

When it happens

Trigger: Calling the code path through yamlPrint with a value yaml.Marshal cannot encode; practically only reachable in custom/modified k6 builds that put unsupported types into the printed struct.

Common situations: Custom k6 forks adding non-serializable fields to status or stats data; extremely rare in stock binaries.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/02cee4171bc3cfa2. Report an issue: GitHub.