{"record":{"id":"10a25e4d55722c1c","repo":"gastownhall/beads","slug":"failed-to-create-backup-file-w","errorCode":null,"errorMessage":"failed to create backup file: %w","messagePattern":"failed to create backup file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/detect_pollution.go","lineNumber":104,"sourceCode":"\t\t// Only include if score is above threshold\n\t\tif score >= 0.7 {\n\t\t\tresults = append(results, pollutionResult{\n\t\t\t\tissue:   issue,\n\t\t\t\tscore:   score,\n\t\t\t\treasons: reasons,\n\t\t\t})\n\t\t}\n\t}\n\n\treturn results\n}\n\nfunc backupPollutedIssues(polluted []pollutionResult, path string) error {\n\t// Create backup file\n\t// nolint:gosec // G304: path is provided by user as explicit backup location\n\tfile, err := os.Create(path)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create backup file: %w\", err)\n\t}\n\tdefer file.Close()\n\n\t// Write each issue as JSONL\n\tfor _, p := range polluted {\n\t\tdata, err := json.Marshal(p.issue)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to marshal issue %s: %w\", p.issue.ID, err)\n\t\t}\n\n\t\tif _, err := file.WriteString(string(data) + \"\\n\"); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write issue %s: %w\", p.issue.ID, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/detect_pollution.go#L86-L122","documentation":"backupPollutedIssues creates the backup file with os.Create at the user-supplied path before writing polluted issues as JSONL. If the file cannot be created — bad directory, permission denied, path is a directory, or disk errors — this wrapped error is returned and the pollution check aborts without backing up.","triggerScenarios":"Running the pollution check with a --backup (or similar) path whose parent directory does not exist, which lacks write permission, or which is itself a directory, causing os.Create to fail.","commonSituations":"Typo in the backup path (e.g. missing directory); read-only filesystem or restricted $HOME; backing up into a path owned by root; passing a directory instead of a file path.","solutions":["Create the parent directory first (mkdir -p) or correct a typo in the backup path.","Check write permissions on the target directory (ls -ld) and choose a writable location.","Ensure the path points to a file, not an existing directory.","Re-run the pollution check after fixing the path."],"exampleFix":"// before\nbd pollution-check --backup missing-dir/backup.jsonl\n// after\nmkdir -p missing-dir && bd pollution-check --backup missing-dir/backup.jsonl","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(backupPath)\nif st, err := os.Stat(dir); err != nil || !st.IsDir() {\n    return fmt.Errorf(\"backup directory %q does not exist\", dir)\n}\nif err := os.WriteFile(filepath.Join(dir, \".probe\"), nil, 0o644); err != nil {\n    return fmt.Errorf(\"directory %q not writable: %w\", dir, err)\n}\nos.Remove(filepath.Join(dir, \".probe\"))","typeGuard":null,"tryCatchPattern":"if err := backupPollutedIssues(polluted, path); err != nil {\n    var pe *fs.PathError\n    if errors.As(err, &pe) && errors.Is(pe.Err, syscall.ENOENT) {\n        os.MkdirAll(filepath.Dir(path), 0o755)\n        err = backupPollutedIssues(polluted, path)\n    }\n    if err != nil { return err }\n}","preventionTips":["mkdir -p the backup directory before running pollution checks.","Use absolute, simple file paths for --backup arguments.","Verify write permission on the target directory beforehand.","Never point --backup at an existing directory."],"tags":["go","filesystem","backup","permissions"],"backgroundTag":"file-create-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}