grafana/k6 · error

failed to encode the lib.Options as json

Error message

failed to encode the lib.Options as json: %w

What it means

Raised in optionsAsObject when json.Marshal fails to serialize the consolidated lib.Options struct so it can be mapped into a JS object for execution.scenario.options / test.options. The '%w' carries the marshal error. Since lib.Options is a plain serializable struct, failure indicates an internal inconsistency in derived option values — effectively an internal error rather than a user-script input problem at this call site.

Solutions

  1. Report as a k6 bug, including the options configuration that triggered it
  2. Simplify/reduce options to isolate which setting produces an unmarshalable value
  3. Upgrade k6 — internal marshalling of options has been hardened over versions
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at internal/js/modules/k6/execution/execution.go:299 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/61b3116a0cd59c78. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/execution/execution.go:299

	for p, get := range props {
		err := o.DefineAccessorProperty(p, rt.ToValue(get), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)
		if err != nil {
			return nil, err
		}
	}

	return o, nil
}

// optionsAsObject maps the lib.Options struct that contains the consolidated
// and derived options configuration in a sobek.Object.
//
// When values are not set then the default value returned from JSON is used.
// Most of the lib.Options are Nullable types so they will be null on default.
func optionsAsObject(rt *sobek.Runtime, options lib.Options) (*sobek.Object, error) {
	b, err := json.Marshal(options)
	if err != nil {
		return nil, fmt.Errorf("failed to encode the lib.Options as json: %w", err)
	}

	// Using the native JS parser function guarantees getting
	// the supported types for deep freezing the complex object.
	jsonParse, _ := sobek.AssertFunction(rt.GlobalObject().Get("JSON").ToObject(rt).Get("parse"))
	parsed, err := jsonParse(sobek.Undefined(), rt.ToValue(string(b)))
	if err != nil {
		common.Throw(rt, err)
	}

	obj := parsed.ToObject(rt)

	mustDelete := func(prop string) {
		delErr := obj.Delete(prop)
		if err != nil {
			common.Throw(rt, delErr)
		}
	}

View on GitHub (pinned to 01ffac6f24)