{"record":{"id":"a94091d754f41528","repo":"siyuan-note/siyuan","slug":"cannot-write-a-template-directory","errorCode":null,"errorMessage":"cannot write a template directory","messagePattern":"cannot write a template directory","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":288,"sourceCode":"\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}\n\t}\n\tswitch request.Action {\n\tcase \"write\":\n\t\tif info == nil {\n\t\t\tif err = validateNewTemplateName(request.Path); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t}\n\t\tif len(request.Content) > maxTemplateSourceSize {\n\t\t\treturn nil, errors.New(\"template source is too large\")\n\t\t}\n\t\tif info != nil && info.IsDir() {\n\t\t\treturn nil, errors.New(\"cannot write a template directory\")\n\t\t}\n\t\terr = writeTemplateSource(root, request.Path, request.Content, info == nil)\n\t\treturn map[string]string{\"revision\": fmt.Sprintf(\"%x\", sha256.Sum256([]byte(request.Content)))}, err\n\tcase \"move\":\n\t\tif info.IsDir() && isManagedTemplatePackage(root, request.Path) {\n\t\t\treturn nil, errors.New(\"template packages cannot be renamed or moved\")\n\t\t}\n\t\tif err = checkTemplateFilePath(root, request.Target); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif err = validateNewTemplateName(request.Target); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif !info.IsDir() && !strings.EqualFold(path.Ext(request.Target), \".md\") {\n\t\t\treturn nil, errors.New(\"template source must use the .md extension\")\n\t\t}\n\t\tif _, err = root.Stat(request.Target); !errors.Is(err, os.ErrNotExist) {\n\t\t\treturn nil, errors.New(\"template destination already exists or is inaccessible\")","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L270-L306","documentation":"A 'write' action targets a path that currently exists as a directory (a template package folder). Directories are structural containers for template packages and cannot have file content written to them, so the operation is rejected. Directories are created explicitly with the 'mkdir' action instead.","triggerScenarios":"Calling ManageTemplateFiles with Action \"write\" where root.Stat(request.Path) returns a FileInfo with IsDir()==true — i.e. the path names an existing template package directory.","commonSituations":"A caller computed the template path from user input that happens to match an existing package folder name (e.g. 'my-pack'); code that lists templates and reuses entry paths for writes without checking IsDir; a path collision after a sync brought in a directory with the intended file name.","solutions":["Check IsDir on the entry (from Action \"list\") before writing and choose a different, file path with .md extension","Write inside the directory instead (e.g. Path: \"my-pack/template.md\") if that is the intent","Remove the directory first (Action \"remove\") if it is obsolete and should be replaced by a file","Use Action \"mkdir\" to create directories — never \"write\""],"exampleFix":"// before\nwrite(TemplateFileRequest{Action: \"write\", Path: entry.Path, Content: c}) // entry.Path may be a dir\n// after\nif !entry.IsDir { write(TemplateFileRequest{Action: \"write\", Path: entry.Path, Content: c}) }","handlingStrategy":"type-guard","validationCode":"entries, _ := manage(TemplateFileRequest{Action: \"list\"})\n// consult IsDir before writing","typeGuard":"func isFileEntry(e TemplateFileEntry) bool { return !e.IsDir }","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"cannot write a template directory\") {\n    // pick a file path inside the directory or choose another name\n}","preventionTips":["Look up IsDir from list output before any write","Never derive a write target from a directory entry path","Use mkdir for directories, write for files only"],"tags":["type-mismatch","templates","filesystem"],"backgroundTag":"incompatible-source-type","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"}