siyuan-note/siyuan · error

template source must use the .md extension

Error message

template source must use the .md extension

What it means

ManageTemplateFiles validates that any non-directory template path ends with the .md extension (case-insensitive) before performing read/write/move operations. The templates feature only manages Markdown source files, so a path with another extension (or none) is rejected to keep the template store consistent. This check runs before the action switch, so it applies to read, write, and move alike.

Solutions

  1. Append the .md extension to the requested path before calling ManageTemplateFiles (use path.Ext to check first)
  2. For 'move', set Target to a path ending in .md — the target extension is checked separately
  3. If the entry is a directory (template package), pass the directory path; the check only applies to files
  4. Verify with strings.EqualFold(path.Ext(p), ".md") client-side before issuing the request

Example fix

// before
req := TemplateFileRequest{Action: "write", Path: "my-template", Content: content}
// after
req := TemplateFileRequest{Action: "write", Path: "my-template.md", Content: content}
Defensive patterns

Strategy: validation

Validate before calling

func validTemplatePath(p string) bool { return strings.EqualFold(path.Ext(p), ".md") }
if !validTemplatePath(req.Path) { req.Path += ".md" }

Type guard

func isMarkdownPath(p string) bool { return strings.EqualFold(path.Ext(p), ".md") }

Prevention

When it happens

Trigger: Calling ManageTemplateFiles with Action 'read'/'write'/'move' and a Path like 'foo.txt', 'foo', or 'foo.MD.bak' that either does not exist or exists as a regular file. Passing an already-existing file with a non-.md extension also triggers it since the check applies whenever info is nil or not a directory.

Common situations: A caller builds the template path from a document title that contains a period or an extension (e.g. 'Notes.md.txt'); an API client omits the extension when creating a new template; code that previously saved arbitrary files into templates/ now fails after the extension gate was added.

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/1e52c4a9345f005b. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_manage.go:257

	if err = checkTemplateFilePath(root, request.Path); err != nil {
		return nil, err
	}
	abs := filepath.Join(root.Name(), filepath.FromSlash(request.Path))
	filelock.Lock(abs)
	defer filelock.Unlock(abs)
	info, statErr := root.Stat(request.Path)
	if request.Action == "mkdir" {
		if err = validateNewTemplateName(request.Path); err != nil {
			return nil, err
		}
		return nil, root.Mkdir(request.Path, 0755)
	}
	if statErr != nil && !(request.Action == "write" && request.Revision == "" && errors.Is(statErr, os.ErrNotExist)) {
		return nil, statErr
	}
	if info == nil || !info.IsDir() {
		if !strings.EqualFold(path.Ext(request.Path), ".md") {
			return nil, errors.New("template source must use the .md extension")
		}
	}
	if request.Action == "read" {
		if info.IsDir() {
			revision, readErr := templateFileRevision(root, request.Path)
			return map[string]string{"content": "", "revision": revision}, readErr
		}
		content, readErr := readTemplateSource(root, request.Path)
		return map[string]string{"content": content, "revision": fmt.Sprintf("%x", sha256.Sum256([]byte(content))), "path": filepath.Join(util.DataDir, "templates", filepath.FromSlash(request.Path))}, readErr
	}
	if info != nil {
		revision, revisionErr := templateFileRevision(root, request.Path)
		if revisionErr != nil {
			return nil, revisionErr
		}
		if request.Revision == "" || request.Revision != revision {
			return nil, errors.New("template changed; reload it before saving, moving or deleting")
		}

View on GitHub (pinned to 9f775e8a12)