{"record":{"id":"8da86c57b8c1b807","repo":"gastownhall/beads","slug":"failed-to-open-file-w","errorCode":null,"errorMessage":"failed to open file: %w","messagePattern":"failed to open file: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/flags.go","lineNumber":198,"sourceCode":"\t\tv, _ := cmd.Flags().GetString(\"design\")\n\t\treturn v, true, nil\n\t}\n\n\treturn \"\", false, nil\n}\n\n// readBodyFile reads the description content from a file.\n// If filePath is \"-\", reads from stdin.\nfunc readBodyFile(filePath string) (string, error) {\n\tvar reader io.Reader\n\n\tif filePath == \"-\" {\n\t\treader = os.Stdin\n\t} else {\n\t\t// #nosec G304 - filePath comes from user flag, validated by caller\n\t\tfile, err := os.Open(filePath)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to open file: %w\", err)\n\t\t}\n\t\tdefer file.Close()\n\t\treader = file\n\t}\n\n\tcontent, err := io.ReadAll(reader)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to read file: %w\", err)\n\t}\n\n\treturn string(content), nil\n}\n\n// textSources names the places a command can take body text from: stdin, a\n// file path, an explicit text flag, then positional args. A non-nil stdin\n// means --stdin was given. flagName names the text flag (e.g. \"--response\")\n// in conflict errors and always accompanies flagText. flagSet marks the text\n// flag as explicitly passed (cobra's Changed), so an empty flag value still","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/flags.go#L180-L216","documentation":"readBodyFile loads body text for command flags (--description-file, --design-file, --reason-file, etc.) from a file path or '-' for stdin. This error wraps os.Open failures: the file could not be opened at all. It is a straightforward file-path/permission problem surfaced when the CLI tries to read user-supplied body content.","triggerScenarios":"Passing --description-file/--design-file/--reason-file with a path that does not exist, is a directory, or lacks read permission; a typo'd path; using '-' when stdin is closed/unavailable (less common; that path bypasses os.Open).","commonSituations":"Shell variable empty or unset so the flag receives a truncated path; running from a different working directory than assumed; file deleted between tab-completion and execution; permissions tightened by sandboxed CI environments.","solutions":["Verify the path exists and is readable: ls -l <path>; correct typos and use an absolute path if unsure","Check that you didn't pass a directory instead of a file","Fix file permissions (chmod/chown) or run from a context/user that can read it","If the text is short or generated, pipe it via stdin with '-' or pass it inline instead of via a file"],"exampleFix":"// before: path from an unset variable\nbd create \"T\" --description-file=\"$NOTES_FILE\"   // failed to open file: open : no such file or directory\n// after: guard the variable before invoking\n[ -n \"$NOTES_FILE\" ] && [ -f \"$NOTES_FILE\" ] && bd create \"T\" --description-file=\"$NOTES_FILE\"","handlingStrategy":"validation","validationCode":"path := filePath\nif path != \"-\" {\n    info, err := os.Stat(path)\n    if err != nil {\n        return fmt.Errorf(\"body file not accessible: %w\", err)\n    }\n    if info.IsDir() {\n        return fmt.Errorf(\"body file is a directory: %s\", path)\n    }\n    f, err := os.Open(path)\n    if err != nil { return err }\n    f.Close()\n}","typeGuard":"func readableFile(path string) bool {\n    if path == \"-\" { return true }\n    f, err := os.Open(path)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"content, err := readBodyFile(flagPath)\nif err != nil {\n    if errors.Is(err, os.ErrNotExist) {\n        return fmt.Errorf(\"--description-file: no such file: %s\", flagPath)\n    }\n    if errors.Is(err, os.ErrPermission) {\n        return fmt.Errorf(\"--description-file: permission denied: %s\", flagPath)\n    }\n    return err\n}","preventionTips":["Quote and verify file-flag paths in shell scripts; fail on unset variables (${VAR:?})","Use absolute paths or run from the expected working directory","Pass a regular file, not a directory","Use '-' with stdin (or inline text) when content is generated rather than stored"],"tags":["go","cli","file-io","flags"],"backgroundTag":"file-not-found","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}