siyuan-note/siyuan · warning

marshal box document metadata failed

Error message

marshal box document metadata failed: %w

What it means

writeBoxDocID serializes the boxDocMeta struct to JSON before writing .siyuan/boxDoc.json; this error wraps any failure of gulu.JSON.MarshalIndentJSON. Since the struct contains only an int and a string, marshalling essentially cannot fail — this is a defensive wrap, meaning a hit points to an unexpected serialization-library or environment issue rather than user input.

Solutions

  1. Retry the operation that writes the notebook doc metadata (re-open the notebook / re-run import)
  2. Check the boxed error (%w chain) to identify the underlying serialization failure
  3. Verify the gulu library version matches kernel/go.mod and rebuild; the struct contains no unmarshalable types so failure signals an environment problem
Defensive patterns

Strategy: retry

Try / catch

if err := writeBoxDocID(boxID); err != nil {
    // marshalling a two-field struct should not fail; log wrapped cause and retry
    logging.LogErrorf("write box doc metadata for %s failed: %s", boxID, err)
}

Prevention

When it happens

Trigger: writeBoxDocID (called from ensureBoxDoc0, importSY0, or tests) where MarshalIndentJSON returns a non-nil error on {Spec:1, BoxDocID:boxID}.

Common situations: Effectively only internal/library anomalies; theoretical JSON encoder failure. Practically seen only during unusual environments (memory pressure) or modified gulu behavior.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/box_doc.go:89

		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)
	}
	return filelock.WriteFile(boxDocMetaPath(boxID), data)
}

func IsBoxDocEnabled() bool {
	return nil != Conf && nil != Conf.FileTree && nil != Conf.FileTree.BoxDocEnabled && *Conf.FileTree.BoxDocEnabled
}

func hiddenBoxDocRootIDs() (ret []string) {
	if IsBoxDocEnabled() || nil == Conf {
		return
	}
	for _, box := range Conf.GetOpenedBoxes() {
		ret = append(ret, box.ID)
	}
	return
}

View on GitHub (pinned to 9f775e8a12)