{"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":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":482,"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":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/bundle.go#L464-L500","documentation":"The init-context open() helper refuses an empty filename before touching the filesystem. open() loads files (json/csv/txt/js/bin, read-only) during initialization so their contents can be used in VU iterations.","triggerScenarios":"`open('')` or `open(pathVar)` where pathVar is the empty string — typically a path built from an unset `__ENV` variable or a destructuring/template-string mistake; fires only in the init context (outside it you get the 'outside init context' error instead).","commonSituations":"`open(__ENV.DATA_FILE)` with DATA_FILE not exported to the k6 process; template literals with a missing variable interpolating to ''.","solutions":["Validate the variable first: `if (!__ENV.DATA_FILE) throw new Error('DATA_FILE is required');`","Export the env var when launching: `DATA_FILE=./users.csv k6 run script.js`","Pass a literal relative or absolute path with a supported extension"],"exampleFix":"// before\nconst data = open(__ENV.DATA_FILE);\n\n// after\nif (!__ENV.DATA_FILE) throw new Error('DATA_FILE is required');\nconst data = open(__ENV.DATA_FILE);","handlingStrategy":"validation","validationCode":"// Only call open() with a concrete, non-empty path\nconst file = __ENV.DATA_FILE;\nif (!file || typeof file !== 'string') throw new Error('DATA_FILE env var must be set to a non-empty path');\nconst data = open(file);","typeGuard":"const isValidOpenPath = (p) => typeof p === 'string' && p.length > 0 && /\\.(js|json|txt|bin|csv)$/.test(p);","tryCatchPattern":null,"preventionTips":["Check `__ENV` vars for existence before using them in open()","Keep data files' extensions among the supported set (.js .json .txt .bin .csv)","Remember open() only works in init context — never inside default/handleSummary"],"tags":["javascript","files","init-context","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}