siyuan-note/siyuan · error

unmarshal box document metadata failed

Error message

unmarshal box document metadata failed: %w

What it means

readBoxDocID parses the notebook's .siyuan/boxDoc.json metadata file; this error is returned when the file content is not valid JSON or does not decode into the boxDocMeta structure (spec/boxDocID fields). It wraps the underlying unmarshal error. A corrupt or truncated boxDoc.json makes the notebook's document-node identity unreadable.

Solutions

  1. Inspect .siyuan/boxDoc.json for the notebook and fix or remove the invalid content; it is regenerated with {"spec":1,"boxDocID":"<boxID>"}
  2. Delete the corrupt boxDoc.json and let ensureBoxDoc0 rewrite it via writeBoxDocID (the file is optional; absence is tolerated)
  3. Restore the file from backup or resync the workspace

Example fix

// before: corrupt file
"{"spec": 1, "boxDocID": "20240101120000-abcd  // truncated
// after: valid boxDoc.json
{
  "spec": 1,
  "boxDocID": "20240101120000-abcdefg"
}
Defensive patterns

Strategy: try-catch

Validate before calling

data, _ := os.ReadFile(filepath.Join(util.DataDir, boxID, ".siyuan", "boxDoc.json"))
var probe map[string]any
if len(data) > 0 && json.Unmarshal(data, &probe) != nil {
    // corrupt boxDoc.json: delete it so it is regenerated
}

Try / catch

id, err := readBoxDocID(boxID)
if err != nil && strings.Contains(err.Error(), "unmarshal box document metadata failed") {
    // corrupt JSON: remove boxDoc.json and let ensureBoxDoc0 rewrite it
}

Prevention

When it happens

Trigger: ensureBoxDoc0 or TestBoxDocMetadataMatchesBoxID calls readBoxDocID on a notebook whose .siyuan/boxDoc.json contains invalid JSON — e.g. empty file, partial write, BOM, or non-JSON content.

Common situations: Interrupted write leaving a truncated file; user or tooling editing boxDoc.json by hand and breaking syntax; file sync conflict markers written into the file; encoding issues after manual copy.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/box_doc.go:71

func boxDocPath(boxID string) string {
	if "" == boxID {
		return ""
	}
	return "/" + boxID + ".sy"
}

func readBoxDocID(boxID string) (ret string, err error) {
	data, err := filelock.ReadFile(boxDocMetaPath(boxID))
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			err = nil
		}
		return
	}

	meta := &boxDocMeta{}
	if err = gulu.JSON.UnmarshalJSON(data, meta); err != nil {
		return "", fmt.Errorf("unmarshal box document metadata failed: %w", err)
	}
	if boxDocMetaSpec != meta.Spec {
		return "", fmt.Errorf("unsupported box document metadata spec [%d]", meta.Spec)
	}
	if !ast.IsNodeIDPattern(meta.BoxDocID) {
		return "", fmt.Errorf("invalid box document ID [%s]", meta.BoxDocID)
	}
	if boxID != meta.BoxDocID {
		return "", fmt.Errorf("box document ID [%s] does not match box ID [%s]", meta.BoxDocID, boxID)
	}
	return boxID, nil
}

func writeBoxDocID(boxID string) error {
	meta := &boxDocMeta{Spec: boxDocMetaSpec, BoxDocID: boxID}
	data, err := gulu.JSON.MarshalIndentJSON(meta, "", "  ")
	if err != nil {
		return fmt.Errorf("marshal box document metadata failed: %w", err)

View on GitHub (pinned to 9f775e8a12)