{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/initcontext.go#L24-L60","documentation":"readFile's deferred guard converts fsext.ErrPathNeverRequestedBefore: open() was called with a path that was NOT opened during initialization (__VU==0). k6 pre-loads all files in the init stage so VU isolation works; opening a new file mid-iteration (often due to path computed per-VU) is rejected.","triggerScenarios":"Thrown at internal/js/initcontext.go:42 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Hoist all open() calls to the top level (init context) of the script","Call open() unconditionally in init for every file a VU may need — not inside per-VU conditionals","Use import.meta.resolve() for correct relative paths in ESM scripts"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}