grafana/k6 · error

could not open '%s': %w

Error message

could not open '%s': %w

What it means

When the end-of-test summary is exported to files (handleSummary returning a map of paths to data, or --summary-export), k6 opens each path with O_WRONLY|O_CREAT|O_TRUNC via getWriter (only 'stdout' and 'stderr' are special-cased). If the file cannot be created or opened, the error is wrapped as "could not open '<path>'" and consolidated with other summary failures under 'Could not save some summary information:'.

Source

Thrown at internal/cmd/run.go:594

}

func handleSummaryResult(fs fsext.Fs, stdOut, stdErr io.Writer, result map[string]io.Reader) error {
	var errs []error

	getWriter := func(path string) (io.Writer, error) {
		switch path {
		case "stdout":
			return stdOut, nil
		case "stderr":
			return stdErr, nil
		default:
			return fs.OpenFile(path, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_TRUNC, 0o666)
		}
	}

	for path, value := range result {
		if writer, err := getWriter(path); err != nil {
			errs = append(errs, fmt.Errorf("could not open '%s': %w", path, err))
		} else if n, err := io.Copy(writer, value); err != nil {
			errs = append(errs, fmt.Errorf("error saving summary to '%s' after %d bytes: %w", path, n, err))
		}
	}

	return consolidateErrorMessage(errs, "Could not save some summary information:")
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Create the parent directory before the run: mkdir -p out/reports
  2. Use the special keys 'stdout' or 'stderr' in handleSummary when no file output is needed
  3. Verify the target directory exists and is writable by the k6 process user
  4. Treat every non-stdout/stderr key of handleSummary's return map as a file path that must be creatable, and validate them all

Example fix

// before
export function handleSummary(data) {
  return { 'reports/summary.json': JSON.stringify(data) };
}
// after
export function handleSummary(data) {
  return { stdout: textSummary(data), 'reports/summary.json': JSON.stringify(data) };
}
// plus in the shell: mkdir -p reports before k6 run
Defensive patterns

Strategy: validation

Validate before calling

# Pre-create and verify every summary target directory
mkdir -p reports out
[ -w reports ] || { echo 'reports/ is not writable' >&2; exit 2; }
k6 run --summary-export=reports/summary.json script.js

Prevention

When it happens

Trigger: handleSummary returns { 'out/reports/summary.json': JSON.stringify(data) } where out/reports/ does not exist; --summary-export points into a directory the k6 process cannot write; a directory component of the path is actually a regular file.

Common situations: CI containers writing summaries into directories never created in the image; paths on read-only mounts; typos in summary paths; switching summary output from stdout to a file without creating the folder.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/6b3414b9abfd9ca4. Report an issue: GitHub.