grafana/k6 · error
marshaling hosts option: %w
Error message
marshaling hosts option: %w
What it means
Returned by setFlagsFromK6Options during launch flag preparation when json.Marshal of the k6 options.Hosts value (types.NullHosts) fails. Hosts is a map of host strings, so standard marshaling practically cannot fail; if this appears it indicates a corrupted/programmatic Options value (unsupported type inside Hosts) rather than a normal script configuration mistake.
Source
Thrown at internal/js/modules/k6/browser/chromium/browser_type.go:534
if currHostResolver, ok := flags["host-resolver-rules"]; ok {
hostResolver = append(hostResolver, fmt.Sprintf("%s", currHostResolver))
}
// Add the host resolver rules.
//
// This is done by marshaling the k6 hosts option to JSON and then
// unmarshaling it to a map[string]string. This is done because the
// k6 v0.42 changed Hosts from a map to types.NullHosts and doesn't
// expose the map anymore.
//
// TODO: A better way to do this would be to handle the resolver
// rules by communicating with Chromium (and then using Hosts's
// Match method) instead of passing the rules via the command line
// to Chromium.
var rules map[string]string
b, err := json.Marshal(k6opts.Hosts)
if err != nil {
return fmt.Errorf("marshaling hosts option: %w", err)
}
if err := json.Unmarshal(b, &rules); err != nil {
return fmt.Errorf("unmarshaling hosts option: %w", err)
}
for k, v := range rules {
hostResolver = append(hostResolver, fmt.Sprintf("MAP %s %s", k, v))
}
if len(hostResolver) > 0 {
sort.Strings(hostResolver)
flags["host-resolver-rules"] = strings.Join(hostResolver, ",")
}
return nil
}
// makeLogger makes and returns an extension wide logger.
func makeLogger(ctx context.Context, envLookup env.LookupFunc) (*log.Logger, error) {
var (View on GitHub (pinned to 93accf6570)
Solutions
- Verify options.hosts in the script is a plain object mapping hostnames to 'host:port' strings
- If you use the Go API, ensure Options.Hosts is a types.NullHosts built from string values
- Temporarily remove the hosts option to confirm the rest of the launch works, then re-add entries incrementally
- Report upstream with the exact hosts value if it reproduces on stock k6
Example fix
// before
export const options = { hosts: { 'test.k6.io': null } };
// after
export const options = { hosts: { 'test.k6.io': 'localhost:3000' } }; Defensive patterns
Strategy: validation
Validate before calling
const hosts = { 'test.k6.io': 'localhost:3000' };
if (Object.values(hosts).some(v => typeof v !== 'string')) {
throw new Error('all hosts values must be strings of the form host:port');
}
export const options = { hosts }; Type guard
function isValidHostsMap(h) {
return typeof h === 'object' && h !== null &&
Object.entries(h).every(([, v]) => typeof v === 'string' && /^[^:]+:\d+$/.test(v));
} Prevention
- Keep hosts mappings as 'hostname' -> 'host:port' strings
- Generate hosts config from a single validated source in CI
- Report stock-k6 reproductions upstream — this path is nearly unreachable with normal scripts
When it happens
Trigger: Launching with options.hosts set to a value that marshals abnormally — in practice only via the Go API constructing lib.Options with a hand-built NullHosts containing unsupported data; not reachable from a normal JS script where hosts parse into string maps.
Common situations: Custom k6 builds or extensions injecting lib.Options programmatically; extremely unusual in stock k6 usage — if seen with a plain script, suspect a k6 bug in the options type.
Related errors
- unmarshaling hosts option: %w
- invalid browser command line flag: "%s=%v"
- parsing options from script got error while parsing %q: %w
- finding browser executable: %w
- k6 couldn't detect google chrome or a chromium-supported bro
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/602c3912325a952c.
Report an issue: GitHub.