{"record":{"id":"0bfb9f2b8b578803","repo":"grafana/k6","slug":"open-can-t-be-used-with-directories-path-q","errorCode":null,"errorMessage":"open() can't be used with directories, path: %q","messagePattern":"open\\(\\) can't be used with directories, path: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/initcontext.go","lineNumber":53,"sourceCode":"}\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) {\n\talreadyOpenedFS, ok := fs.(fsext.OnlyCachedEnabler)\n\tif !ok {\n\t\treturn\n\t}\n\n\talreadyOpenedFS.AllowOnlyCached()\n}\n\nfunc generateSourceMapLoader(logger logrus.FieldLogger, filesystems map[string]fsext.Fs,\n) func(path string) ([]byte, error) {\n\treturn func(path string) ([]byte, error) {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/initcontext.go#L35-L71","documentation":"readFile in internal/js/initcontext.go stats the requested path (a workaround for spf13/fsext#201) and refuses when it resolves to a directory. k6's open() reads one file fully into memory; there is no directory listing API, so pointing it at a folder is rejected with the offending path in the message.","triggerScenarios":"open('data/') with a trailing slash or any path that resolves to a directory; a path where the filename was forgotten; a joined path that ends at a folder instead of a file.","commonSituations":"Typos in relative paths; assuming k6 can read a folder of fixtures like Node's fs; archives where a directory shadows the intended file.","solutions":["Point open() at the concrete file, e.g. './data/users.csv'","Open each file in the folder explicitly during init","Check for trailing slashes and for './' vs '/' when building relative paths"],"exampleFix":"// before: path points at a directory\nconst data = open('./data/');\n\n// after: path points at a file\nconst data = open('./data/users.csv');","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a concrete file path ending in a filename, not a directory","Watch for trailing slashes and for './' vs absolute paths when composing paths","List the files you plan to open in a comment so path typos are caught in review"],"tags":["open","files","filesystem","path"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}