{"record":{"id":"3306f68f1196756b","repo":"grafana/k6","slug":"empty-name-provided-to-sharedarray-s-constructor","errorCode":null,"errorMessage":"empty name provided to SharedArray's constructor","messagePattern":"empty name provided to SharedArray's constructor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/data/data.go","lineNumber":83,"sourceCode":"\t\t\t\"SharedArray\": d.sharedArray,\n\t\t},\n\t}\n}\n\nconst asyncFunctionNotSupportedMsg = \"SharedArray constructor does not support async functions as second argument\"\n\n// sharedArray is a constructor returning a shareable read-only array\n// indentified by the name and having their contents be whatever the call returns\nfunc (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {\n\trt := d.vu.Runtime()\n\n\tif d.vu.State() != nil {\n\t\tcommon.Throw(rt, errors.New(\"new SharedArray must be called in the init context\"))\n\t}\n\n\tname := call.Argument(0).String()\n\tif name == \"\" {\n\t\tcommon.Throw(rt, errors.New(\"empty name provided to SharedArray's constructor\"))\n\t}\n\tval := call.Argument(1)\n\n\tif common.IsAsyncFunction(rt, val) {\n\t\tcommon.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))\n\t}\n\n\tfn, ok := sobek.AssertFunction(val)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"a function is expected as the second argument of SharedArray's constructor\"))\n\t}\n\n\tbuilder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }\n\tarray, err := d.shared.loadOrStore(name, builder)\n\tif err != nil {\n\t\tcommon.Throw(rt, err)\n\t}\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/data/data.go#L65-L101","documentation":"The first argument to new SharedArray(name, fn) is the key used to register and share the array across VUs, so k6 requires a non-empty string. call.Argument(0).String() returns '' for an empty string (or a non-string value like a number/object coerced to nothing sensible), and the constructor throws immediately.","triggerScenarios":"'new SharedArray(\"\", fn)', 'new SharedArray(null, fn)', or passing a non-string first argument whose String() conversion yields an empty string.","commonSituations":"Typos and copy-paste where the name placeholder was never filled in; programmatically generating SharedArrays in a loop and passing an empty variable; assuming the name is optional like in plain JS APIs.","solutions":["Pass a descriptive non-empty string as the first argument.","Keep names unique per SharedArray in the script - two SharedArrays with the same name share one registry entry, and an empty name is simply rejected up front.","If generating names dynamically, assert the computed name is non-empty before constructing."],"exampleFix":"// before\nconst arr = new SharedArray('', () => [1, 2, 3]); // throws\n\n// after\nconst arr = new SharedArray('user-ids', () => [1, 2, 3]);","handlingStrategy":"type-guard","validationCode":"function assertSharedArrayName(name) {\n  if (typeof name !== 'string' || name.trim() === '') {\n    throw new Error(`SharedArray name must be a non-empty string, got: ${JSON.stringify(name)}`);\n  }\n  return name;\n}","typeGuard":"const isValidSharedArrayName = (n) => typeof n === 'string' && n.length > 0;","tryCatchPattern":null,"preventionTips":["Generate names from the data file basename when creating arrays in a loop.","Keep a list of SharedArray names at the top of the script to guarantee uniqueness and non-emptiness."],"tags":["k6","sharedarray","validation","constructor"],"backgroundTag":"constructor-argument-validation","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"}