{"record":{"id":"e0ee531cf7f67333","repo":"cilium/cilium","slug":"could-not-write-q-w","errorCode":null,"errorMessage":"could not write %q: %w","messagePattern":"could not write %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/kvstore/commands.go","lineNumber":139,"sourceCode":"\n\t\t\t\t\t\toutfmt, _ := s.Flags.GetString(\"output\")\n\t\t\t\t\t\tswitch outfmt {\n\t\t\t\t\t\tcase \"plain\":\n\t\t\t\t\t\t\tfmt.Fprintln(&b, string(v))\n\t\t\t\t\t\tcase \"json\":\n\t\t\t\t\t\t\tif err := json.Indent(&b, v, \"\", \"  \"); err != nil {\n\t\t\t\t\t\t\t\tfmt.Fprintf(&b, \"ERROR: %s\", err)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tfmt.Fprintln(&b)\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\treturn \"\", \"\", fmt.Errorf(\"unexpected output format %q\", outfmt)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif len(args) == 2 {\n\t\t\t\t\terr = os.WriteFile(s.Path(args[1]), b.Bytes(), 0644)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\terr = fmt.Errorf(\"could not write %q: %w\", s.Path(args[1]), err)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tstdout = b.String()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}, nil\n\t\t},\n\t)\n}\n","sourceCodeStart":121,"sourceCodeEnd":149,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/kvstore/commands.go#L121-L149","documentation":"When the kvstore list command is given a second positional argument, it writes the collected key/value output to that local file path (resolved relative to the script state) instead of stdout. This error wraps any os.WriteFile failure — permissions, bad path, directory missing — with the offending path quoted.","triggerScenarios":"Calling the list command with two args where the second is a path that cannot be written: the target directory does not exist, the file exists but is not writable (permissions/ownership), the path is a directory, or the filesystem is read-only.","commonSituations":"Redirecting output to /etc/cilium/... in a container without write access; saving to a relative path after chdir; disk-full or read-only root filesystem in a Kubernetes node context.","solutions":["Verify the target directory exists and is writable (mkdir -p, chmod/chown)","Write to a path the process can access, e.g. under /tmp","Check disk space and whether the filesystem is mounted read-only","Ensure the second argument is a file path, not a directory"],"exampleFix":"// before\nkvstore list prefix /etc/cilium/backup.json\n// after\nmkdir -p /etc/cilium && kvstore list prefix /etc/cilium/backup.json","handlingStrategy":"validation","validationCode":"dest := args[1]\nif st, err := os.Stat(filepath.Dir(dest)); err != nil || !st.IsDir() {\n    return fmt.Errorf(\"directory for %q is not writable\", dest)\n}\nf, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644)\nif err != nil {\n    return fmt.Errorf(\"cannot write %q: %w\", dest, err)\n}\nf.Close()","typeGuard":"func writableFile(path string) bool {\n    if st, err := os.Stat(path); err == nil && st.IsDir() {\n        return false\n    }\n    f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)\n    if err != nil {\n        return false\n    }\n    f.Close()\n    return true\n}","tryCatchPattern":"stdout, stderr, err := runKvstoreList(ctx, prefix, dest)\nif err != nil && strings.Contains(err.Error(), \"could not write\") {\n    // fall back to stdout instead of file\n    stdout, _, err = runKvstoreList(ctx, prefix)\n}","preventionTips":["Pre-write the output directory (mkdir -p) before running the command","Check filesystem writability/space, especially in containers","Point dumps at writable paths like /tmp when the default paths are read-only","Ensure the second argument is a file path, not a directory"],"tags":["filesystem","io"],"backgroundTag":"file-write-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}