siyuan-note/siyuan · error

block write failed: empty block ID

Error message

block write failed: empty block ID

What it means

Helper writeBlockWriteResult refuses to print a block-write result when the resulting block ID is empty, treating it as evidence that the write did not produce a usable block. This guards against reporting a successful write with no identifier.

Solutions

  1. Check the error from the underlying block write before calling printBlockWriteResult.
  2. Ensure the write operation returns a non-empty block ID on success; if it doesn't, fail earlier with the real cause.
  3. In tests, pass a non-empty ID (or keep the empty case as an explicit rejection test).

Example fix

// before
err := doBlockWrite(...)
printBlockWriteResult(newID) // newID may be ""
// after
if err := doBlockWrite(...); err != nil {
    return err
}
if newID == "" {
    return fmt.Errorf("block write returned no ID")
}
return printBlockWriteResult(newID)
Defensive patterns

Strategy: try-catch

Validate before calling

if newID == "" {
    return fmt.Errorf("block write returned no ID")
}
err := printBlockWriteResult(newID)

Type guard

func hasBlockID(id string) bool { return id != "" }

Try / catch

if err := printBlockWriteResult(id); err != nil {
    return fmt.Errorf("report block write result: %w", err)
}

Prevention

When it happens

Trigger: Calling printBlockWriteResult (or writeBlockWriteResult directly, as tests do) with id == "" after a block insert/update/append/prepend operation whose returned ID was empty.

Common situations: Upstream write API returned success but no ID (silent failure); caller forgot to check the write error before formatting output; test verifying the empty-ID rejection path.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/9f639d20ebfe36e6. Report an issue: GitHub.

Appendix: source

Thrown at kernel/cli/cmd/block.go:321

			ParentID: parentID,
		}
		if err = model.PerformTxSync(&model.Transaction{DoOperations: []*model.Operation{operation}}); err != nil {
			return err
		}
		if bt := treenode.GetBlockTree(parentID); bt != nil {
			model.AppendPushReloadProtyleEntry(bt.RootID)
		}
		return printBlockWriteResult(operation.ID)
	},
}

func printBlockWriteResult(id string) error {
	return writeBlockWriteResult(os.Stdout, id)
}

func writeBlockWriteResult(output io.Writer, id string) error {
	if id == "" {
		return fmt.Errorf("block write failed: empty block ID")
	}
	if outputFormat == "json" {
		data, err := json.MarshalIndent(map[string]string{"id": id}, "", "  ")
		if err != nil {
			return err
		}
		_, err = fmt.Fprintln(output, string(data))
		return err
	}
	_, err := fmt.Fprintln(output, id)
	return err
}

var blockUpdateCmd = &cobra.Command{
	Use:   "update --id <id> [--data <markdown> | --file <path>]",
	Short: "Update block",
	RunE: func(cmd *cobra.Command, args []string) error {
		id, _ := cmd.Flags().GetString("id")

View on GitHub (pinned to 9f775e8a12)