grafana/k6 · error
open() can't be used with directories, path: %q
Error message
open() can't be used with directories, path: %q
What it means
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.
Source
Thrown at internal/js/initcontext.go:53
}
func readFile(fileSystem fsext.Fs, filename string) (data []byte, err error) {
defer func() {
if errors.Is(err, fsext.ErrPathNeverRequestedBefore) {
// loading different files per VU is not supported, so all files should are going
// to be used inside the scenario should be opened during the init step (without any conditions)
err = fmt.Errorf(
"open() can't be used with files that weren't previously opened during initialization (__VU==0), path: %q",
filename,
)
}
}()
// Workaround for https://github.com/spf13/fsext/issues/201
if isDir, err := fsext.IsDir(fileSystem, filename); err != nil {
return nil, err
} else if isDir {
return nil, fmt.Errorf("open() can't be used with directories, path: %q", filename)
}
return fsext.ReadFile(fileSystem, filename)
}
// allowOnlyOpenedFiles enables seen only files
func allowOnlyOpenedFiles(fs fsext.Fs) {
alreadyOpenedFS, ok := fs.(fsext.OnlyCachedEnabler)
if !ok {
return
}
alreadyOpenedFS.AllowOnlyCached()
}
func generateSourceMapLoader(logger logrus.FieldLogger, filesystems map[string]fsext.Fs,
) func(path string) ([]byte, error) {
return func(path string) ([]byte, error) {View on GitHub (pinned to 93accf6570)
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
Example fix
// before: path points at a directory
const data = open('./data/');
// after: path points at a file
const data = open('./data/users.csv'); Defensive patterns
Strategy: validation
Prevention
- 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
When it happens
Trigger: 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.
Common situations: Typos in relative paths; assuming k6 can read a folder of fixtures like Node's fs; archives where a directory shadows the intended file.
Related errors
- the "open" function is only available in the init stage (i.e
- open() can't be used with files that weren't previously open
- open() can't be used with an empty filename
- open() failed; reason: unable to access the file system
- serialising archive: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/0bfb9f2b8b578803.
Report an issue: GitHub.