{"record":{"id":"682f488075d6ce2a","repo":"grafana/k6","slug":"open-failed-reason-unable-to-access-the-file-s","errorCode":null,"errorMessage":"open() failed; reason: unable to access the file system","messagePattern":"open\\(\\) failed; reason: unable to access the file system","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/experimental/fs/module.go","lineNumber":121,"sourceCode":"\nfunc (mi *ModuleInstance) openImpl(path string) (*File, error) {\n\tinitEnv := mi.vu.InitEnv()\n\t// Strip file scheme if available as we should support only this scheme\n\tpath = strings.TrimPrefix(path, \"file://\")\n\n\t// We resolve the path relative to the entrypoint script, as opposed to\n\t// the current working directory (the k6 command is called from).\n\t//\n\t// This is done on purpose, although it diverges in some respect with\n\t// how files are handled in different k6 contexts, so that we cater to\n\t// and intuitive user experience.\n\t//\n\t// See #2781 and #2674.\n\tpath = fsext.Abs(initEnv.CWD.Path, path)\n\n\tfs, ok := initEnv.FileSystems[\"file\"]\n\tif !ok {\n\t\treturn nil, errors.New(\"open() failed; reason: unable to access the file system\")\n\t}\n\n\tif exists, err := fsext.Exists(fs, path); err != nil {\n\t\treturn nil, fmt.Errorf(\"open() failed, unable to verify if %q exists; reason: %w\", path, err)\n\t} else if !exists {\n\t\treturn nil, newFsError(NotFoundError, fmt.Sprintf(\"no such file or directory %q\", path))\n\t}\n\n\tif isDir, err := fsext.IsDir(fs, path); err != nil {\n\t\treturn nil, fmt.Errorf(\"open() failed, unable to verify if %q is a directory; reason: %w\", path, err)\n\t} else if isDir {\n\t\treturn nil, newFsError(\n\t\t\tInvalidResourceError,\n\t\t\tfmt.Sprintf(\"cannot open %q: opening a directory is not supported\", path),\n\t\t)\n\t}\n\n\tdata, err := mi.cache.open(path, fs)","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/experimental/fs/module.go#L103-L139","documentation":"The experimental fs module's open() resolves the path against the script entrypoint and then requires a registered 'file' filesystem in the init environment (initEnv.FileSystems[\"file\"]). When the execution context provides no such filesystem handle, opening any file is impossible and this error is returned.","triggerScenarios":"Calling fs.open() in an execution context where the 'file' filesystem was never attached: embedded/test harnesses building a VU without FileSystems, or execution environments that do not expose a local/bundled filesystem to the script.","commonSituations":"Running scripts in environments where local file access is not wired up; extension or module unit tests that forget to set InitEnv.FileSystems = map[string]fsext.Fs{\"file\": ...}; mismatched k6 versions where the runner does not register the file fs.","solutions":["Run the script with a k6 version/build that registers the local filesystem (standard k6 run does)","In tests or embedded runners, populate the init environment: InitEnv.FileSystems[\"file\"] = fsext.NewOsFs() (or afero fs)","Verify you call fs.open() in the init context; other fs failures produce different messages"],"exampleFix":"// Go (test harness) before: FileSystems empty -> JS fs.open() fails\n// after\nruntime.VU.InitEnvField.FileSystems = map[string]fsext.Fs{\n    \"file\": fsext.NewOsFs(),\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let file;\ntry {\n  file = fs.open(path);\n} catch (e) {\n  if (String(e.message).includes('unable to access the file system')) {\n    throw new Error(`fs unavailable in this execution context; cannot open ${path}`);\n  }\n  throw e;\n}","preventionTips":["Confirm the runner registers the 'file' filesystem (standard k6 run does; custom/embedded runners must set InitEnv.FileSystems)","In Go tests, always set InitEnvField.FileSystems = map[string]fsext.Fs{\"file\": ...}","Call fs.open() only in the init context -- later phases produce different failures"],"tags":["fs","filesystem","environment","init-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}