{"record":{"id":"7365e8c6cfdc24ed","repo":"cilium/cilium","slug":"writing-to-file-v-error-w","errorCode":null,"errorMessage":"writing to file %v error: %w","messagePattern":"writing to file (.+?) error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-cli/connectivity/perf/common/metrics.go","lineNumber":195,"sourceCode":"\t\t\t\tdata[identifier+\"th\"] = res\n\t\t\t} else {\n\t\t\t\tmaps.Copy(data[identifier+\"th\"].Data, res.Data)\n\t\t\t}\n\t\t}\n\t}\n\treturn exportSummary(perfData{Version: \"v1\", DataItems: slices.Collect(maps.Values(data))}, reportDir)\n}\n\nfunc exportSummary(content perfData, reportDir string) error {\n\t// this filename needs to be in a specific format for perfdash\n\tfileName := strings.Join([]string{\"NetworkPerformance_benchmark\", time.Now().Format(time.RFC3339)}, \"_\")\n\tfilePath := path.Join(reportDir, strings.Join([]string{fileName, \"json\"}, \".\"))\n\tcontentStr, err := prettyPrintJSON(content)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error formatting summary: %v error: %w\", content, err)\n\t}\n\tif err := os.WriteFile(filePath, []byte(contentStr), 0600); err != nil {\n\t\treturn fmt.Errorf(\"writing to file %v error: %w\", filePath, err)\n\t}\n\treturn nil\n}\n\nfunc prettyPrintJSON(data any) (string, error) {\n\toutput := &bytes.Buffer{}\n\tif err := json.NewEncoder(output).Encode(data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"building encoder error: %w\", err)\n\t}\n\tformatted := &bytes.Buffer{}\n\tif err := json.Indent(formatted, output.Bytes(), \"\", \"  \"); err != nil {\n\t\treturn \"\", fmt.Errorf(\"indenting error: %w\", err)\n\t}\n\treturn formatted.String(), nil\n}\n","sourceCodeStart":177,"sourceCodeEnd":211,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-cli/connectivity/perf/common/metrics.go#L177-L211","documentation":"After successfully formatting the perf summary as JSON, exportSummary writes it to NetworkPerformance_benchmark_<timestamp>.json in the report directory (path required by perfdash). If os.WriteFile fails, the error is wrapped with this message naming the target path. The benchmark ran but its results were not persisted.","triggerScenarios":"os.WriteFile(filePath, ..., 0600) fails — reportDir doesn't exist, is not writable by the test user, disk full, or path length/permission issues — when exporting perf summaries.","commonSituations":"Caller passed a --report-dir that was never created; read-only container filesystem; running tests as non-root in a directory owned by root; disk quota exceeded on CI runners.","solutions":["Check the wrapped syscall error (ENOENT vs EACCES vs ENOSPC) for the exact cause","Create the report directory before running: mkdir -p <reportDir>, or have exportSummary os.MkdirAll(reportDir, 0o755)","Ensure the user running the test has write permission on reportDir and enough disk space","Point --report-dir at a writable volume (e.g. CI artifact directory)"],"exampleFix":"// before\nfunc exportSummary(content perfData, reportDir string) error {\n\tfileName := strings.Join([]string{\"NetworkPerformance_benchmark\", time.Now().Format(time.RFC3339)}, \"_\")\n\tfilePath := path.Join(reportDir, strings.Join([]string{fileName, \"json\"}, \".\"))\n// after\nfunc exportSummary(content perfData, reportDir string) error {\n\tif err := os.MkdirAll(reportDir, 0o755); err != nil {\n\t\treturn fmt.Errorf(\"creating report dir %s: %w\", reportDir, err)\n\t}\n\tfileName := strings.Join([]string{\"NetworkPerformance_benchmark\", time.Now().Format(time.RFC3339)}, \"_\")\n\tfilePath := path.Join(reportDir, strings.Join([]string{fileName, \"json\"}, \".\"))","handlingStrategy":"validation","validationCode":"// before running perf tests\nif err := os.MkdirAll(reportDir, 0o755); err != nil {\n\tlog.Fatal(err)\n}\nif f, err := os.CreateTemp(reportDir, \".wtest\"); err != nil {\n\tlog.Fatalf(\"report dir %s not writable: %v\", reportDir, err)\n} else { f.Close(); os.Remove(f.Name()) }","typeGuard":null,"tryCatchPattern":"if err := exportSummary(content, reportDir); err != nil {\n\tvar pe *fs.PathError\n\tif errors.As(err, &pe) && (errors.Is(pe, os.ErrPermission) || errors.Is(pe, os.ErrNotExist)) {\n\t\tlog.Printf(\"report dir issue (%s): %v — falling back to cwd\", reportDir, err)\n\t\treturn exportSummary(content, \".\")\n\t}\n\treturn err\n}","preventionTips":["Create reportDir (MkdirAll) before the test run","Point --report-dir at a writable volume on CI","Check disk space/quota before long benchmark runs"],"tags":["filesystem","io","performance-test","cilium"],"backgroundTag":"file-write-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}