{"record":{"id":"1e52c4a9345f005b","repo":"siyuan-note/siyuan","slug":"template-source-must-use-the-md-extension","errorCode":null,"errorMessage":"template source must use the .md extension","messagePattern":"template source must use the \\.md extension","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":257,"sourceCode":"\tif err = checkTemplateFilePath(root, request.Path); err != nil {\n\t\treturn nil, err\n\t}\n\tabs := filepath.Join(root.Name(), filepath.FromSlash(request.Path))\n\tfilelock.Lock(abs)\n\tdefer filelock.Unlock(abs)\n\tinfo, statErr := root.Stat(request.Path)\n\tif request.Action == \"mkdir\" {\n\t\tif err = validateNewTemplateName(request.Path); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn nil, root.Mkdir(request.Path, 0755)\n\t}\n\tif statErr != nil && !(request.Action == \"write\" && request.Revision == \"\" && errors.Is(statErr, os.ErrNotExist)) {\n\t\treturn nil, statErr\n\t}\n\tif info == nil || !info.IsDir() {\n\t\tif !strings.EqualFold(path.Ext(request.Path), \".md\") {\n\t\t\treturn nil, errors.New(\"template source must use the .md extension\")\n\t\t}\n\t}\n\tif request.Action == \"read\" {\n\t\tif info.IsDir() {\n\t\t\trevision, readErr := templateFileRevision(root, request.Path)\n\t\t\treturn map[string]string{\"content\": \"\", \"revision\": revision}, readErr\n\t\t}\n\t\tcontent, readErr := readTemplateSource(root, request.Path)\n\t\treturn map[string]string{\"content\": content, \"revision\": fmt.Sprintf(\"%x\", sha256.Sum256([]byte(content))), \"path\": filepath.Join(util.DataDir, \"templates\", filepath.FromSlash(request.Path))}, readErr\n\t}\n\tif info != nil {\n\t\trevision, revisionErr := templateFileRevision(root, request.Path)\n\t\tif revisionErr != nil {\n\t\t\treturn nil, revisionErr\n\t\t}\n\t\tif request.Revision == \"\" || request.Revision != revision {\n\t\t\treturn nil, errors.New(\"template changed; reload it before saving, moving or deleting\")\n\t\t}","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L239-L275","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Append the .md extension to the requested path before calling ManageTemplateFiles (use path.Ext to check first)","For 'move', set Target to a path ending in .md — the target extension is checked separately","If the entry is a directory (template package), pass the directory path; the check only applies to files","Verify with strings.EqualFold(path.Ext(p), \".md\") client-side before issuing the request"],"exampleFix":"// before\nreq := TemplateFileRequest{Action: \"write\", Path: \"my-template\", Content: content}\n// after\nreq := TemplateFileRequest{Action: \"write\", Path: \"my-template.md\", Content: content}","handlingStrategy":"validation","validationCode":"func validTemplatePath(p string) bool { return strings.EqualFold(path.Ext(p), \".md\") }\nif !validTemplatePath(req.Path) { req.Path += \".md\" }","typeGuard":"func isMarkdownPath(p string) bool { return strings.EqualFold(path.Ext(p), \".md\") }","tryCatchPattern":null,"preventionTips":["Always construct template paths with the .md extension at a single helper","Check entry.IsDir from list results before file operations","Keep document-title-derived filenames sanitized to end with .md"],"tags":["validation","file-extension","templates"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}