{"record":{"id":"3a12608654a5ff9b","repo":"grafana/k6","slug":"open-can-t-be-used-with-an-empty-filename","errorCode":null,"errorMessage":"open() can't be used with an empty filename","messagePattern":"open\\(\\) can't be used with an empty filename","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":484,"sourceCode":"\t\t\tpanic(fmt.Errorf(\"failed to set '%s' global object: %w\", k, err))\n\t\t}\n\t}\n\n\timpl := requireImpl{\n\t\tinInitContext: func() bool { return vu.state == nil },\n\t\tmodSys:        modSys,\n\t}\n\n\tmustSet(\"require\", impl.require)\n\n\tmustSet(\"open\", func(filename string, args ...string) (sobek.Value, error) {\n\t\t// TODO fix in stack traces\n\t\tif vu.state != nil {\n\t\t\treturn nil, fmt.Errorf(cantBeUsedOutsideInitContextMsg, \"open\")\n\t\t}\n\n\t\tif filename == \"\" {\n\t\t\treturn nil, errors.New(\"open() can't be used with an empty filename\")\n\t\t}\n\t\t// This uses the pwd from the requireImpl\n\t\tpwd, err := modSys.CurrentlyRequiredModule()\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif !strings.HasPrefix(filename, \"file://\") && !filepath.IsAbs(filename) {\n\t\t\totherPath, shouldWarn := modSys.ShouldWarnOnParentDirNotMatchingCurrentModuleParentDir(vu, pwd)\n\t\t\tlogger := b.preInitState.Logger\n\t\t\tif shouldWarn {\n\t\t\t\tlogger.Warningf(\"open() was used and is currently relative to '%s', but in the future \"+\n\t\t\t\t\t\"it will be aligned with how `require` and imports work and will be relative to '%s'. This means \"+\n\t\t\t\t\t\"that in the future open will open relative path relative to the module/file it is written in. \"+\n\t\t\t\t\t\"You can future proof this by using `import.meta.resolve()` to get relative paths to the file it \"+\n\t\t\t\t\t\"is written in the current k6 version.\", pwd, otherPath)\n\t\t\t\terr = b.preInitState.Usage.Uint64(\"deprecations/openRelativity\", 1)\n\t\t\t\tif err != nil {\n\t\t\t\t\tlogger.WithError(err).Warn(\"failed reporting usage of deprecated relativity of open()\")","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/bundle.go#L466-L502","documentation":"The init-context `open()` builtin (internal/js/bundle.go:482) reads files at script load time. It rejects an empty filename outright because there is nothing to resolve relative to the module's working directory. (A separate error covers calling open() outside the init context.)","triggerScenarios":"Calling `open('')`, or `open(someVar)` where someVar is an empty string — e.g. a path built from an env var or variable that is unset/empty at init time.","commonSituations":"Parameterized data files via `__ENV.DATA_FILE` when the variable isn't exported to the k6 process; template strings with an empty variable; refactor leftovers calling open with a placeholder ''.","solutions":["Pass a real path relative to the script (or absolute, or a file:// URL).","If the path comes from an env var, ensure it is set: `K6_DATA_FILE=users.csv k6 run script.js` and guard before calling open.","For dynamically chosen files, resolve the name in setup() with http or compute it before calling open() in init context."],"exampleFix":"// before\nconst data = open(__ENV.DATA_FILE ?? '');\n// after\nconst path = __ENV.DATA_FILE;\nif (!path) throw new Error('DATA_FILE env var is required');\nconst data = open(path);","handlingStrategy":"validation","validationCode":"// guard before calling open() in init context\nconst path = __ENV.DATA_FILE;\nif (!path || typeof path !== 'string') {\n  throw new Error('DATA_FILE must be a non-empty environment variable');\n}\nconst data = open(path);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail fast with a clear message when path-bearing env vars are missing instead of passing '' to open().","Keep data files adjacent to the script and reference them with literal relative paths where possible."],"tags":["filesystem","init-context","open","javascript"],"backgroundTag":"empty-path-argument","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}