{"record":{"id":"59ce1684485701f5","repo":"siyuan-note/siyuan","slug":"content-template-s-is-not-a-regular-file","errorCode":null,"errorMessage":"content template [%s] is not a regular file","messagePattern":"content template \\[(.+?)\\] is not a regular file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template.go","lineNumber":1240,"sourceCode":"\ttemplateRoot := filepath.Join(util.DataDir, \"templates\")\n\tabsPath := filepath.Join(templateRoot, cleanPath)\n\tif !gulu.File.IsSubPath(templateRoot, absPath) {\n\t\treturn \"\", errors.New(\"content template path is outside templates directory\")\n\t}\n\tif !filelock.IsExist(absPath) {\n\t\treturn \"\", fmt.Errorf(\"content template [%s] not found\", templatePath)\n\t}\n\trealRoot, err := filepath.EvalSymlinks(templateRoot)\n\tif nil != err {\n\t\treturn \"\", err\n\t}\n\trealPath, err := filepath.EvalSymlinks(absPath)\n\tif nil != err {\n\t\treturn \"\", err\n\t}\n\tinfo, err := os.Stat(realPath)\n\tif nil != err || !info.Mode().IsRegular() {\n\t\treturn \"\", fmt.Errorf(\"content template [%s] is not a regular file\", templatePath)\n\t}\n\tif !gulu.File.IsSubPath(realRoot, realPath) {\n\t\treturn \"\", errors.New(\"content template path is outside templates directory\")\n\t}\n\treturn realPath, nil\n}\n\n// CreateTemplate 在 <data>/templates/ 下创建模板文件。name 不含扩展名，content 为 markdown 文本。\n// overwrite=false 且文件已存在时返回 code=1（与 DocSaveAsTemplate 一致）。\nfunc CreateTemplate(name, content string, overwrite bool) (code int, err error) {\n\tname = util.FilterFileName(name) + \".md\"\n\tname = util.TruncateLenFileName(name)\n\tsavePath := filepath.Join(util.DataDir, \"templates\", name)\n\tif filelock.IsExist(savePath) {\n\t\tif !overwrite {\n\t\t\tcode = 1\n\t\t\treturn\n\t\t}","sourceCodeStart":1222,"sourceCodeEnd":1258,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template.go#L1222-L1258","documentation":"After resolving symlinks, resolveDocContentTemplatePath stats the real path and requires it to be a regular file (os.Stat + Mode().IsRegular()). This same error is also returned when os.Stat itself fails. Directories, devices, fifos, or broken entries under templates/ cannot be used as content templates.","triggerScenarios":"Passing a directory name inside <data>/templates/ as templatePath; the path is a symlink whose target vanished (EvalSymlinks succeeded but Stat failed), or the file was deleted between checks; the entry is a special file.","commonSituations":"Selecting a template subfolder instead of a file; symlinked template directories whose target was unmounted; race where the file is removed while the template is being applied.","solutions":["Point templatePath at a regular .md file, not a directory or special file.","Re-create the symlink or restore the missing target file, then retry.","Check os.Stat manually on <data>/templates/<templatePath> to see whether it exists and what kind of entry it is."],"exampleFix":"// before\ntemplatePath := \"templates-dir\" // a directory\n// after\ntemplatePath := \"templates-dir/index.md\"","handlingStrategy":"validation","validationCode":"const st = fs.statSync(process.env.SIYUAN_DATA + \"/templates/\" + templatePath);\nif (!st.isFile()) throw new Error(\"template path is not a regular file\");","typeGuard":null,"tryCatchPattern":"try {\n  await applyDocContentTemplate(boxID, templatePath);\n} catch (e) {\n  if (String(e).includes(\"not a regular file\")) {\n    // fall back to directory listing or ask user to select a file\n  } else { throw e; }\n}","preventionTips":["Never pass a directory as a template path; always target a concrete .md file","Check for broken symlinks when templates are managed by dotfile tools","Stat the path before the API call when the path comes from dynamic input"],"tags":["file-type","templates","stat"],"backgroundTag":"path-is-not-a-directory","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"}