{"record":{"id":"907ecd7c574d5a4e","repo":"siyuan-note/siyuan","slug":"id-is-required-907ecd","errorCode":null,"errorMessage":"--id is required","messagePattern":"--id is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/cli/cmd/ref.go","lineNumber":41,"sourceCode":"\t\"text/tabwriter\"\n\n\t\"github.com/siyuan-note/siyuan/kernel/model\"\n\n\t\"github.com/spf13/cobra\"\n)\n\nvar refCmd = &cobra.Command{\n\tUse:   \"ref\",\n\tShort: \"Backlinks and references\",\n}\n\nvar refBacklinksCmd = &cobra.Command{\n\tUse:   \"backlinks --id <id>\",\n\tShort: \"Get backlinks for a block\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tid, _ := cmd.Flags().GetString(\"id\")\n\t\tif id == \"\" {\n\t\t\treturn fmt.Errorf(\"--id is required\")\n\t\t}\n\n\t\tkeyword, _ := cmd.Flags().GetString(\"keyword\")\n\t\tsortMode, _ := cmd.Flags().GetInt(\"sort\")\n\n\t\t_, backlinks, _, count, _ := model.GetBacklink2(id, keyword, \"\", sortMode, 0, model.Conf.Editor.BacklinkContainChildren)\n\n\t\tswitch outputFormat {\n\t\tcase \"json\":\n\t\t\tdata, _ := json.MarshalIndent(backlinks, \"\", \"  \")\n\t\t\tfmt.Println(string(data))\n\t\tdefault:\n\t\t\tprintPathTable(backlinks, false)\n\t\t}\n\n\t\tfmt.Printf(\"\\n%d backlink(s)\\n\", count)\n\t\treturn nil\n\t},","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/cli/cmd/ref.go#L23-L59","documentation":"Argument validation in the cobra refBacklinksCmd RunE: fires when the --id flag is empty. Querying backlinks requires the target block ID passed to model.GetBacklink2; running `ref backlinks` without --id is the faulting usage.","triggerScenarios":"Running `ref backlinks` without `--id`, or with `--id \"\"`.","commonSituations":"Scripts where the block id variable was never set; omitting the flag while only passing optional `--keyword`/`--sort`.","solutions":["Re-run with `--id <block-id>`","Copy the block id from the SiYuan editor (right-click, copy block id)","Guard the id variable in scripts before invoking"],"exampleFix":"// before\nsiyuan ref backlinks --keyword \"todo\"\n// after\nsiyuan ref backlinks --id 20240101120000-abcdefg --keyword \"todo\"","handlingStrategy":"validation","validationCode":"[ -n \"$BLOCK_ID\" ] || { echo \"--id is required\"; exit 1; }\nsiyuan ref backlinks --id \"$BLOCK_ID\"","typeGuard":"const requireBlockId = (v) => (typeof v === \"string\" && v.length > 0) ? v : null;","tryCatchPattern":"try { run(`siyuan ref backlinks --id ${blockId}`); } catch (e) { console.error(\"backlinks failed:\", e.message); }","preventionTips":["Copy block ids from the editor's context menu","Validate id variables before CLI invocation","Combine with --keyword/--sort only after --id is present"],"tags":["cli","flag-validation","backlinks"],"backgroundTag":"missing-cli-argument","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}