siyuan-note/siyuan · error

new item templates config is nil

Error message

new item templates config is nil

What it means

SetNewItemTemplates is the single entry point that validates and replaces an Attribute View's new-item template list; it refuses a nil config because every subsequent step dereferences config.Templates and config.DefaultTemplateID. The guard is the first line of defense before any allocation.

Source

Thrown at kernel/av/new_item_template.go:40

	"sort"
	"strings"

	"github.com/88250/lute/ast"

	"github.com/siyuan-note/siyuan/kernel/util"
)

// PrunedNewItemTemplateOption 描述创建条目前从模板字段值中移除的无效选项。
type PrunedNewItemTemplateOption struct {
	TemplateID string
	KeyID      string
	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 {

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Pass a non-nil *NewItemTemplatesConfig; to clear templates, pass an empty struct (Templates nil/empty) — the function sets av.NewItemTemplates = nil for you (new_item_template.go:93).
  2. If writing a handler, mirror doSetAttrViewNewItemTemplates: unmarshal into a freshly allocated config.
  3. Add a nil-config check on the caller side and surface a clearer error before reaching the kernel.

Example fix

// before
av.SetNewItemTemplates(nil)
// after
av.SetNewItemTemplates(&av.NewItemTemplatesConfig{})
Defensive patterns

Strategy: type-guard

Type guard

func nonNilNewItemConfig(c *av.NewItemTemplatesConfig) (*av.NewItemTemplatesConfig, error) {
    if c == nil {
        return nil, errors.New("new item templates config is nil")
    }
    return c, nil
}

Try / catch

if err := attrView.SetNewItemTemplates(config); err != nil {
    return &TxErr{code: TxErrHandleAttributeView, id: avID, msg: err.Error()}
}

Prevention

When it happens

Trigger: Direct library call av.SetNewItemTemplates(nil), or a code path that constructs the config lazily and leaves it nil. The HTTP handler doSetAttrViewNewItemTemplates (model/attribute_view.go:5117) always allocates a non-nil *NewItemTemplatesConfig, so the API path does not normally hit this.

Common situations: SDK/plugin code calling SetNewItemTemplates without populating a config; a refactor that forgets to allocate the struct; tests that pass nil inadvertently.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/258982342a32c48c. Report an issue: GitHub.