siyuan-note/siyuan · error

invalid template source file

Error message

invalid template source file

What it means

readTemplateSource rejects files that are not regular files or that exceed maxTemplateSourceSize, returning this single combined error. It guarantees the kernel only reads bounded, plain-file template content.

Solutions

  1. Replace the symlink/special file with a real regular file
  2. Reduce the file size below maxTemplateSourceSize
  3. Remove the offending entry from the templates directory

Example fix

# before: templates/x.md -> elsewhere.md (symlink)
# after
cp -L elsewhere.md templates/x.md  # regular file under the size cap
Defensive patterns

Strategy: validation

Validate before calling

info, err := os.Lstat(p)
if err != nil || !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize { /* refuse before calling */ }

Prevention

When it happens

Trigger: ManageTemplateFiles reading a template that is a symlink or special file, or whose Stat-reported size is over maxTemplateSourceSize.

Common situations: Symlinked template files, oversized templates from bulk imports, device/FIFO files present in the templates directory.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/template_manage.go:156

		})
	} else {
		if info.Size() > maxTemplateSourceSize {
			return "", errors.New("template source is too large")
		}
		var content []byte
		content, err = root.ReadFile(p)
		h.Write(content)
	}
	return fmt.Sprintf("%x", h.Sum(nil)), err
}

func readTemplateSource(root *os.Root, p string) (string, error) {
	info, err := root.Stat(p)
	if err != nil {
		return "", err
	}
	if !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize {
		return "", errors.New("invalid template source file")
	}
	content, err := root.ReadFile(p)
	if err != nil {
		return "", err
	}
	if !utf8.Valid(content) {
		return "", errors.New("template source is not UTF-8")
	}
	return string(content), nil
}

// 同目录临时文件写入完成后替换,写入失败时保留原模板。
func writeTemplateSource(root *os.Root, p, content string, create bool) error {
	if !utf8.ValidString(content) {
		return errors.New("invalid template source")
	}
	if create {
		file, err := root.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)

View on GitHub (pinned to 9f775e8a12)