siyuan-note/siyuan · error

clone new item template failed

Error message

clone new item template failed

What it means

During SetNewItemTemplates, each template is deep-copied via cloneNewItemTemplate so the view never aliases caller data. If cloning fails (cloneNewItemTemplate returns nil, e.g. nil or corrupt inner data), the whole operation aborts with this generic error.

Solutions

  1. Inspect each NewItemTemplate in the config and ensure all required inner fields (name, id, target fields) are populated.
  2. Rebuild the config from a known-good serialization instead of reusing partially constructed objects.
  3. Skip or normalize invalid entries before calling SetNewItemTemplates.
Defensive patterns

Strategy: validation

Validate before calling

for _, t := range cfg.Templates {
	if t == nil || t.ID == "" || t.Name == "" { /* drop or repair invalid entries */ }
}

Try / catch

if err := view.SetNewItemTemplates(cfg); err != nil && err.Error() == "clone new item template failed" {
	// rebuild cfg from a known-good source and retry once
}

Prevention

When it happens

Trigger: Supplying a *NewItemTemplate in config.Templates that cloneNewItemTemplate cannot clone — practically a template entry whose inner structures fail to copy or an entry that yields nil after cloning despite passing the nil check.

Common situations: Constructing templates programmatically with partially initialized fields; deserialized templates from JSON with missing embedded objects.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at kernel/av/new_item_template.go:52

	Values     []string
}

// SetNewItemTemplates 校验并替换数据库的新增条目模板配置。
func (av *AttributeView) SetNewItemTemplates(config *NewItemTemplatesConfig) error {
	if nil == config {
		return errors.New("new item templates config is nil")
	}

	templates := make([]*NewItemTemplate, 0, len(config.Templates))
	templateIDs := map[string]bool{}
	defaultTemplateID := config.DefaultTemplateID
	for _, itemTemplate := range config.Templates {
		if nil == itemTemplate {
			continue
		}
		itemTemplate = cloneNewItemTemplate(itemTemplate)
		if nil == itemTemplate {
			return errors.New("clone new item template failed")
		}
		itemTemplate.Name = strings.TrimSpace(itemTemplate.Name)
		itemTemplate.Icon = strings.TrimSpace(itemTemplate.Icon)
		if filteredIcon, valid := util.FilterIconValue(itemTemplate.Icon); valid {
			itemTemplate.Icon = filteredIcon
		} else {
			// 非法图标值置空,防止存储可执行标记
			// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq
			itemTemplate.Icon = ""
		}
		if "" == itemTemplate.Name {
			return errors.New("new item template name is empty")
		}
		if !ast.IsNodeIDPattern(itemTemplate.ID) {
			return fmt.Errorf("invalid new item template id [%s]", itemTemplate.ID)
		}
		if templateIDs[itemTemplate.ID] {
			return fmt.Errorf("duplicated new item template id [%s]", itemTemplate.ID)

View on GitHub (pinned to 9f775e8a12)