{"record":{"id":"602c3912325a952c","repo":"grafana/k6","slug":"marshaling-hosts-option-w","errorCode":null,"errorMessage":"marshaling hosts option: %w","messagePattern":"marshaling hosts option: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/chromium/browser_type.go","lineNumber":534,"sourceCode":"\tif currHostResolver, ok := flags[\"host-resolver-rules\"]; ok {\n\t\thostResolver = append(hostResolver, fmt.Sprintf(\"%s\", currHostResolver))\n\t}\n\n\t// Add the host resolver rules.\n\t//\n\t// This is done by marshaling the k6 hosts option to JSON and then\n\t// unmarshaling it to a map[string]string. This is done because the\n\t// k6 v0.42 changed Hosts from a map to types.NullHosts and doesn't\n\t// expose the map anymore.\n\t//\n\t// TODO: A better way to do this would be to handle the resolver\n\t// rules by communicating with Chromium (and then using Hosts's\n\t// Match method) instead of passing the rules via the command line\n\t// to Chromium.\n\tvar rules map[string]string\n\tb, err := json.Marshal(k6opts.Hosts)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshaling hosts option: %w\", err)\n\t}\n\tif err := json.Unmarshal(b, &rules); err != nil {\n\t\treturn fmt.Errorf(\"unmarshaling hosts option: %w\", err)\n\t}\n\tfor k, v := range rules {\n\t\thostResolver = append(hostResolver, fmt.Sprintf(\"MAP %s %s\", k, v))\n\t}\n\tif len(hostResolver) > 0 {\n\t\tsort.Strings(hostResolver)\n\t\tflags[\"host-resolver-rules\"] = strings.Join(hostResolver, \",\")\n\t}\n\n\treturn nil\n}\n\n// makeLogger makes and returns an extension wide logger.\nfunc makeLogger(ctx context.Context, envLookup env.LookupFunc) (*log.Logger, error) {\n\tvar (","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/chromium/browser_type.go#L516-L552","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nexport const options = { hosts: { 'test.k6.io': null } };\n\n// after\nexport const options = { hosts: { 'test.k6.io': 'localhost:3000' } };","handlingStrategy":"validation","validationCode":"const hosts = { 'test.k6.io': 'localhost:3000' };\nif (Object.values(hosts).some(v => typeof v !== 'string')) {\n  throw new Error('all hosts values must be strings of the form host:port');\n}\nexport const options = { hosts };","typeGuard":"function isValidHostsMap(h) {\n  return typeof h === 'object' && h !== null &&\n    Object.entries(h).every(([, v]) => typeof v === 'string' && /^[^:]+:\\d+$/.test(v));\n}","tryCatchPattern":null,"preventionTips":["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"],"tags":["launch","hosts-option","configuration","internal","json"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}