siyuan-note/siyuan · error

cannot write a template directory

Error message

cannot write a template directory

What it means

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.

Solutions

  1. Check IsDir on the entry (from Action "list") before writing and choose a different, file path with .md extension
  2. Write inside the directory instead (e.g. Path: "my-pack/template.md") if that is the intent
  3. Remove the directory first (Action "remove") if it is obsolete and should be replaced by a file
  4. Use Action "mkdir" to create directories — never "write"

Example fix

// before
write(TemplateFileRequest{Action: "write", Path: entry.Path, Content: c}) // entry.Path may be a dir
// after
if !entry.IsDir { write(TemplateFileRequest{Action: "write", Path: entry.Path, Content: c}) }
Defensive patterns

Strategy: type-guard

Validate before calling

entries, _ := manage(TemplateFileRequest{Action: "list"})
// consult IsDir before writing

Type guard

func isFileEntry(e TemplateFileEntry) bool { return !e.IsDir }

Try / catch

if err != nil && strings.Contains(err.Error(), "cannot write a template directory") {
    // pick a file path inside the directory or choose another name
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/template_manage.go:288

		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")
		}
	}
	switch request.Action {
	case "write":
		if info == nil {
			if err = validateNewTemplateName(request.Path); err != nil {
				return nil, err
			}
		}
		if len(request.Content) > maxTemplateSourceSize {
			return nil, errors.New("template source is too large")
		}
		if info != nil && info.IsDir() {
			return nil, errors.New("cannot write a template directory")
		}
		err = writeTemplateSource(root, request.Path, request.Content, info == nil)
		return map[string]string{"revision": fmt.Sprintf("%x", sha256.Sum256([]byte(request.Content)))}, err
	case "move":
		if info.IsDir() && isManagedTemplatePackage(root, request.Path) {
			return nil, errors.New("template packages cannot be renamed or moved")
		}
		if err = checkTemplateFilePath(root, request.Target); err != nil {
			return nil, err
		}
		if err = validateNewTemplateName(request.Target); err != nil {
			return nil, err
		}
		if !info.IsDir() && !strings.EqualFold(path.Ext(request.Target), ".md") {
			return nil, errors.New("template source must use the .md extension")
		}
		if _, err = root.Stat(request.Target); !errors.Is(err, os.ErrNotExist) {
			return nil, errors.New("template destination already exists or is inaccessible")

View on GitHub (pinned to 9f775e8a12)