{"record":{"id":"1d8d928ddb73426e","repo":"grafana/k6","slug":"open-can-t-be-used-with-files-that-weren-t-previ","errorCode":null,"errorMessage":"open() can't be used with files that weren't previously opened during initialization (__VU==0), path: %q","messagePattern":"open\\(\\) can't be used with files that weren't previously opened during initialization \\(__VU==0\\), path: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/initcontext.go","lineNumber":42,"sourceCode":"\tfilename = strings.TrimPrefix(filename, \"file://\")\n\tdata, err := readFile(fs, fsext.Abs(basePWD.Path, filename))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif len(args) > 0 && args[0] == \"b\" {\n\t\tab := rt.NewArrayBuffer(data)\n\t\treturn rt.ToValue(&ab), nil\n\t}\n\treturn rt.ToValue(string(data)), nil\n}\n\nfunc readFile(fileSystem fsext.Fs, filename string) (data []byte, err error) {\n\tdefer func() {\n\t\tif errors.Is(err, fsext.ErrPathNeverRequestedBefore) {\n\t\t\t// loading different files per VU is not supported, so all files should are going\n\t\t\t// to be used inside the scenario should be opened during the init step (without any conditions)\n\t\t\terr = fmt.Errorf(\n\t\t\t\t\"open() can't be used with files that weren't previously opened during initialization (__VU==0), path: %q\",\n\t\t\t\tfilename,\n\t\t\t)\n\t\t}\n\t}()\n\n\t// Workaround for https://github.com/spf13/fsext/issues/201\n\tif isDir, err := fsext.IsDir(fileSystem, filename); err != nil {\n\t\treturn nil, err\n\t} else if isDir {\n\t\treturn nil, fmt.Errorf(\"open() can't be used with directories, path: %q\", filename)\n\t}\n\n\treturn fsext.ReadFile(fileSystem, filename)\n}\n\n// allowOnlyOpenedFiles enables seen only files\nfunc allowOnlyOpenedFiles(fs fsext.Fs) {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/initcontext.go#L24-L60","documentation":"During init, k6 wraps the filesystem so only files actually opened at init are cached; in the VU stage open() is served from that cache (allowOnlyOpenedFiles / fsext.OnlyCachedEnabler in internal/js/initcontext.go). When a VU requests a path the cache has never seen, fsext returns ErrPathNeverRequestedBefore and readFile rewrites it into this error. The rule: every file must be opened unconditionally during initialization (__VU==0) so k6 can snapshot it for all VUs.","triggerScenarios":"Opening a per-VU path such as open(`data_${__VU}.json`) inside the default function; opening file A during init only under a condition (if (__ENV.MODE) open('a.csv')) so a different path is requested later; calling open() in the VU stage on a path never touched at init.","commonSituations":"Sharding test data per VU; environment-dependent fixtures; code that opens a fallback file only on some branches; loading different files per iteration.","solutions":["Open every candidate file unconditionally at the top level during init - even files only some VUs use - and select among the loaded contents later","For structured data, load and parse at init (SharedArray for large datasets) instead of calling open() in the VU","If data is truly per-VU and huge, pre-generate one combined file or split the run into multiple scripts"],"exampleFix":"// before: per-VU path -> file never cached during init\nexport default function () {\n  const data = JSON.parse(open(`./data/${__VU}.json`));\n}\n\n// after: open all files unconditionally at init, then pick per VU\nimport { SharedArray } from 'k6/data';\nconst shards = new SharedArray('shards', () => [\n  JSON.parse(open('./data/1.json')),\n  JSON.parse(open('./data/2.json')),\n]);\nexport default function () {\n  const data = shards[(__VU - 1) % shards.length];\n}","handlingStrategy":"validation","validationCode":"# heuristic: flag dynamic/interpolated paths inside open() - they cannot all be cached at init\ngrep -nP 'open\\(\\s*[`'\"'\"'].*\\$\\{|open\\(.*\\+' script.js && \\\n  echo 'warning: dynamic open() path - open every file unconditionally during init instead'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Open every file the test may use unconditionally at the top level, even conditionally-needed ones","Never build open() paths from __VU or __ITER","Load data at init (SharedArray) instead of opening files inside iterations"],"tags":["open","files","init-stage","per-vu","cache"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}