siyuan-note/siyuan · error

createDocTree is only available when manually inserting a…

Error message

createDocTree is only available when manually inserting a template in the editor

What it means

The createDocTree action exposed to templates is a privileged collector side effect: it only works while a template is being manually inserted into the editor (render mode editorInsert), where enabled and allowCreation are both true. Any other context, such as preview rendering or other template invocations, refuses document creation to prevent templates from mutating the workspace unexpectedly.

Solutions

  1. Insert the template manually in the editor (template insertion dialog / editorInsert mode) so createDocTree is allowed.
  2. Do not call createDocTree from preview or content-mode renders; restructure the template to only create documents during manual insertion.
  3. If documents must be created programmatically, use the kernel's document-creation API instead of the template action.
  4. For tests, enable the collector's allowCreation flag before invoking create.

Example fix

// before (preview render context)
// template calls createDocTree -> refused
// after
// open the template insertion dialog in the editor and insert manually
// so the collector runs with enabled=true, allowCreation=true
Defensive patterns

Strategy: try-catch

Try / catch

try {
  createDocTree(def);
} catch (e) {
  if (String(e).includes("only available when manually inserting")) {
    // fall back: create documents via the kernel API instead
  }
}

Prevention

When it happens

Trigger: Calling .createDocTree from a template rendered outside manual editor insertion — e.g. preview render, content insertion mode, or an automated/background template render — where collector.enabled or collector.allowCreation is false.

Common situations: Testing templates via preview instead of inserting them in the editor; invoking the template action from a script or plugin render path; the test TestTemplateDocTreeCollectorLimitsDocumentsAcrossCallS exercises create with the guard disabled.

Related errors


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

Appendix: source

Thrown at kernel/model/template_doc_tree.go:218

func templateDocTreeStringField(definition map[string]any, key string) (string, error) {
	value, exists := definition[key]
	if !exists {
		return "", nil
	}
	ret, ok := value.(string)
	if !ok {
		return "", fmt.Errorf("createDocTree document %s must be a string", key)
	}
	ret = strings.TrimSpace(ret)
	if "" == ret {
		return "", fmt.Errorf("createDocTree document %s must not be empty", key)
	}
	return ret, nil
}

func (collector *templateDocTreeCollector) create(def any) (string, error) {
	if !collector.enabled || !collector.allowCreation {
		return "", errors.New("createDocTree is only available when manually inserting a template in the editor")
	}
	nodes, err := parseTemplateDocTreeDefinition(def)
	if nil != err {
		return "", err
	}
	if maxTemplateDocTreeDocs < len(flattenTemplateDocTreeNodes0(collector.nodes))+len(flattenTemplateDocTreeNodes0(nodes)) {
		return "", fmt.Errorf("createDocTree exceeds the maximum document count of %d", maxTemplateDocTreeDocs)
	}
	collector.bindNodes(nodes, collector.rootID, collector.rootPath, collector.rootHPath)
	collector.nodes = append(collector.nodes, nodes...)
	return "", nil
}

func (collector *templateDocTreeCollector) bindNodes(nodes []*TemplateDocTreeNode, parentID, parentPath, parentHPath string) {
	for _, node := range nodes {
		node.ParentID = parentID
		node.path = strings.TrimSuffix(parentPath, ".sy") + "/" + node.ID + ".sy"
		node.HPath = path.Join(parentHPath, node.Title)

View on GitHub (pinned to 9f775e8a12)