siyuan-note/siyuan · error

template source is not UTF-8

Error message

template source is not UTF-8

What it means

readTemplateSource requires template file content to be valid UTF-8 and throws this error otherwise. Template sources are rendered as UTF-8 text end to end, so non-UTF-8 bytes (e.g. legacy encodings) cannot be handled.

Solutions

  1. Re-encode the file to UTF-8 (iconv -f GBK -t UTF-8, or editor 'Save with encoding: UTF-8')
  2. Re-save the template from a text editor with UTF-8 selected
  3. Remove binary content from the file

Example fix

# before: templates/old.md is GBK-encoded
# after
iconv -f GBK -t UTF-8 templates/old.md > templates/old-utf8.md
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(p)
if err != nil || !utf8.Valid(data) { /* re-encode to UTF-8 before calling */ }

Prevention

When it happens

Trigger: ManageTemplateFiles reading a template file encoded in GBK/Shift-JIS/Latin-1 or containing binary bytes.

Common situations: Templates imported from Windows editors saving legacy codepages, files downloaded from sources without encoding metadata, or binary data accidentally saved as .md.

Related errors


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

Appendix: source

Thrown at kernel/model/template_manage.go:163

		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)
		if err != nil {
			return err
		}
		_, err = file.WriteString(content)
		if err == nil {
			err = file.Sync()
		}

View on GitHub (pinned to 9f775e8a12)