{"record":{"id":"00312a5789d9d634","repo":"grafana/k6","slug":"csv-parser-constructor-must-be-called-in-the-init","errorCode":null,"errorMessage":"csv Parser constructor must be called in the init context","messagePattern":"csv Parser constructor must be called in the init context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/experimental/csv/module.go","lineNumber":155,"sourceCode":"\t\t\tcallback(func() error {\n\t\t\t\treturn reject(rt.NewGoError(err))\n\t\t\t})\n\t\t\treturn\n\t\t}\n\t\tcallback(func() error {\n\t\t\treturn resolve(fn())\n\t\t})\n\t}()\n\n\treturn promise, nil\n}\n\n// NewParser creates a new CSV parser instance.\nfunc (mi *ModuleInstance) NewParser(call sobek.ConstructorCall) *sobek.Object {\n\trt := mi.vu.Runtime()\n\n\tif mi.vu.State() != nil {\n\t\tcommon.Throw(rt, errors.New(\"csv Parser constructor must be called in the init context\"))\n\t}\n\n\tif len(call.Arguments) < 1 || sobek.IsUndefined(call.Argument(0)) {\n\t\tcommon.Throw(rt, fmt.Errorf(\"csv Parser constructor takes at least one non-nil source argument\"))\n\t}\n\n\tfileArg := call.Argument(0)\n\tif common.IsNullish(fileArg) {\n\t\tcommon.Throw(rt, fmt.Errorf(\"csv Parser constructor takes at least one non-nil source argument\"))\n\t}\n\n\t// 1. Make sure the Sobek object is a fs.File (Sobek operation)\n\tvar file fs.File\n\tif err := mi.vu.Runtime().ExportTo(fileArg, &file); err != nil {\n\t\tcommon.Throw(\n\t\t\tmi.vu.Runtime(),\n\t\t\tfmt.Errorf(\"first argument expected to be a fs.File instance, got %T instead\", call.Argument(0)),\n\t\t)","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/experimental/csv/module.go#L137-L173","documentation":"The experimental CSV module (k6/experimental/csv) parses files that were opened in the init context; its Parser constructor therefore refuses to run once a VU state exists. Like SharedArray and grpc client.load, it is an init-only API by design, because file descriptors cannot be shared across VU iterations.","triggerScenarios":"'new CSV.Parser(...)' executed inside export default function(), setup(), or any runtime-called helper - i.e. when mi.vu.State() != nil. Constructing it at module top level works.","commonSituations":"Opening the CSV lazily inside the default function to 'avoid paying parse cost when unused'; wrapping parser creation in a function called per iteration; upgrading scripts from older examples that parsed CSV manually inside the VU with sharedArray.","solutions":["Move 'new CSV.Parser(...)' to the top level of the script, next to the open() call, and export/reference it from the VU function.","If rows must be parsed per-VU conditionally, keep one top-level parser and consume it selectively inside the default function.","Ensure open() for the CSV file is also in the init context - it is a prerequisite of the constructor anyway."],"exampleFix":"// before\nimport CSV from 'k6/experimental/csv';\nexport default function () {\n  const parser = new CSV.Parser(open('./data.csv')); // throws\n}\n\n// after\nimport CSV from 'k6/experimental/csv';\nconst parser = new CSV.Parser(open('./data.csv'), { delimiter: ',' });\nexport default function () { /* parser.readLine() here */ }","handlingStrategy":"validation","validationCode":"// Parser construction belongs at module top level\nconst parser = new CSV.Parser(open('./data.csv'), { delimiter: ',' });\nexport default function () { /* consume parser here */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep open() and new CSV.Parser() adjacent, both at the top of the script.","Remember the init-only trio: SharedArray, open(), CSV.Parser - same placement rule."],"tags":["k6","csv","parser","init-context","experimental"],"backgroundTag":"k6-init-context-violation","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}