siyuan-note/siyuan · error

sort target document [%s] not found

Error message

sort target document [%s] not found

What it means

While applying a custom sibling order, the kernel searches the ordered list of sibling document IDs for the target document (targetID) that defines the insertion position. If the target cannot be found in the parent's child list, the sort is aborted and this error is returned. It indicates the requested sort target no longer exists in that parent (it was removed, moved, or the ID is wrong).

Source

Thrown at kernel/model/file.go:3100

	}
	insertIndex := 0
	if "after" == position {
		insertIndex = len(orderedIDs)
	}
	if "" != targetID {
		insertIndex = -1
		for i, currentID := range orderedIDs {
			if currentID == targetID {
				insertIndex = i
				if "after" == position {
					insertIndex++
				}
				break
			}
		}
		if 0 > insertIndex {
			fileTreeSortLock.Unlock()
			return fmt.Errorf("sort target document [%s] not found", targetID)
		}
	}
	orderedIDs = append(orderedIDs, "")
	copy(orderedIDs[insertIndex+1:], orderedIDs[insertIndex:])
	orderedIDs[insertIndex] = id
	sortIDs := make(map[string]int, len(orderedIDs))
	for i, orderedID := range orderedIDs {
		sortIDs[orderedID] = i + 1
	}
	maps.Copy(fullSortIDs, sortIDs)
	if err = writeSortConfMap(confPath, fullSortIDs); nil != err {
		fileTreeSortLock.Unlock()
		return err
	}
	fileTreeSortLock.Unlock()
	pushFiletreeSortChanged(sortIDs)
	return nil
}

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Refresh the doc tree (GET /api/filetree/listDocsByPath) and retry with a current target document ID
  2. Confirm the target document still exists: POST /api/filetree/getDoc with the ID
  3. Check sync/conflict history — if the target was deleted, remove it from the pending sort request
  4. Re-open the notebook so the in-memory tree and block index are rebuilt
  5. Retry after other devices finish syncing so IDs are no longer stale

Example fix

// before (stale targetID from cached UI state)
await fetchPost('/api/filetree/moveDocs', {targetID: staleTargetID, ...});
// after (verify target exists first)
const resp = await fetchPost('/api/filetree/getDocInfo', {id: targetID});
if (!resp.data) {
  const tree = await fetchPost('/api/filetree/listDocsByPath', {notebook: boxID, path: parentPath});
  targetID = tree.data.files[0].id; // pick a live target
}
await fetchPost('/api/filetree/moveDocs', {targetID, ...});
Defensive patterns

Strategy: validation

Validate before calling

async function targetExists(boxId, parentPath, targetId) {
  const r = await fetchPost('/api/filetree/listDocsByPath', {notebook: boxId, path: parentPath});
  return r.data.files.some(f => f.id === targetId);
}

Type guard

function isBlockId(v) { return typeof v === 'string' && /^[0-9]{14}-[a-z0-9]{7}$/.test(v); }

Try / catch

try {
  await sortDocs(parentId, dragIds, targetId);
} catch (e) {
  if (String(e).includes('sort target document')) {
    await refreshFiletree(); // reload tree and retry with a live target
  }
}

Prevention

When it happens

Trigger: Calling the sibling-order placement API (placeDocInSiblingOrder) with a targetID of a document that is not a current child of the parent path: target was deleted or moved to another notebook/folder before the sort call, the client passed a stale block ID (e.g. cached drag target), or a truncated/invalid ID was supplied.

Common situations: Stale frontend state after another session or a sync operation removed/moved the drag target; API/script callers using an old doc ID after reorganizing the tree; concurrent deletion racing a sort request.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/4255b57f199fe9e0. Report an issue: GitHub.