{"record":{"id":"c4870f6d74de547f","repo":"siyuan-note/siyuan","slug":"target-document-not-found-in-notebook-s-s","errorCode":null,"errorMessage":"target document not found in notebook %s: %s","messagePattern":"target document not found in notebook (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/import.go","lineNumber":1500,"sourceCode":"\tif _, err := os.Stat(localPath); err != nil {\n\t\treturn err\n\t}\n\t_, _, err := resolveMarkdownImportTarget(boxID, toPath)\n\treturn err\n}\n\nfunc resolveMarkdownImportTarget(boxID, toPath string) (baseHPath, baseTargetPath string, err error) {\n\ttoPath = path.Clean(\"/\" + strings.TrimPrefix(toPath, \"/\"))\n\tif toPath != \"/\" && !strings.HasSuffix(toPath, \".sy\") {\n\t\ttoPath += \".sy\"\n\t}\n\ttoPath = normalizeBoxDocTarget(boxID, toPath)\n\tif toPath == \"/\" {\n\t\treturn \"/\", \"/\", nil\n\t}\n\tblock := treenode.GetBlockTreeRootByPath(boxID, toPath)\n\tif block == nil || block.ID == \"\" {\n\t\treturn \"\", \"\", fmt.Errorf(\"target document not found in notebook %s: %s\", boxID, toPath)\n\t}\n\treturn block.HPath, strings.TrimSuffix(block.Path, \".sy\"), nil\n}\n\nfunc importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool) (err error) {\n\tbox, err := getOpenedBox(boxID)\n\tif nil != err {\n\t\treturn err\n\t}\n\tutil.PushEndlessProgress(Conf.Language(73))\n\tdefer func() {\n\t\tutil.PushClearProgress()\n\n\t\tif e := recover(); nil != e {\n\t\t\tstack := debug.Stack()\n\t\t\tmsg := fmt.Sprintf(\"PANIC RECOVERED: %v\\n\\t%s\\n\", e, stack)\n\t\t\tlogging.LogErrorf(\"import from local path failed: %s\", msg)\n\t\t\terr = errors.New(\"import from local path failed, please check kernel log for details\")","sourceCodeStart":1482,"sourceCodeEnd":1518,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/import.go#L1482-L1518","documentation":"resolveMarkdownImportTarget maps the user-supplied target path (toPath) for a Markdown import to an existing document in the target notebook via the block tree. If no document root matches the normalized path, the import cannot determine the parent document and returns this error. Empty toPath ('/') is allowed (import to notebook root); anything else must point at an existing .sy document.","triggerScenarios":"Calling ImportFromLocalPath / ImportFromLocalPathSkipRoot / ValidateImportFromLocalPath with a toPath that names a document that does not exist in boxID — typically a stale or typoed hpath-derived path, or a path belonging to another notebook.","commonSituations":"Frontend passing a toPath captured before the target document was deleted or renamed; cross-notebook drag-drop with a path from the wrong notebook; API callers constructing toPath by hand without .sy normalization; stale block-tree index after reindexing.","solutions":["Verify the target document exists in the notebook and pass its current path (from the doc tree) as toPath","Pass \"/\" as toPath to import to the notebook root instead of under a specific document","Refresh/reindex the notebook if the block tree is stale (rebuild index), then retry","If calling the API, obtain toPath from a document lookup (e.g. filetree/doc listing) rather than hardcoding it"],"exampleFix":"// before: stale/foreign path\ncurl -d '{\"notebook\":\"20240102150405-abc\",\"localPath\":\"/tmp/notes\",\"toPath\":\"/20240102150405-deleted-doc.sy\"}' /api/import/importFromPath\n// after: import to notebook root, or a verified existing doc path\ncurl -d '{\"notebook\":\"20240102150405-abc\",\"localPath\":\"/tmp/notes\",\"toPath\":\"/\"}' /api/import/importFromPath","handlingStrategy":"validation","validationCode":"// Caller-side (before /api/import/importFromPath): confirm the target doc exists\n// via the block-tree/filetree API; pass \"/\" to import at notebook root\nconst target = \"/20240102150405-existing-doc.sy\"\nconst ok = (await fetchPost(\"/api/filetree/getDoc\", {id: docID})).code === 0\n// or simply use toPath: \"/\" when unsure","typeGuard":null,"tryCatchPattern":"try {\n  await fetchPost(\"/api/import/importFromPath\", {notebook, localPath, toPath});\n} catch (e) {\n  if (String(e.msg).startsWith(\"target document not found\")) {\n    // fall back to notebook root or re-resolve toPath from the doc tree\n    toPath = \"/\";\n  }\n}","preventionTips":["Resolve toPath from the live document tree at call time, never cache it across renames/deletes","Use \"/\" when importing to the notebook root is acceptable","Never build toPath by hand for a different notebook than boxID","After bulk deletes or reindex, re-fetch target paths before importing"],"tags":["import","markdown","not-found","path"],"backgroundTag":"entity-not-found","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"}