siyuan-note/siyuan · error

reserved template file name

Error message

reserved template file name

What it means

validateNewTemplateName rejects new template file names whose base is a Windows reserved device name: CON, PRN, AUX, NUL, or COM1-9/LPT1-9. Creating such a file would be unreachable or dangerous on Windows, so the kernel blocks it up front for cross-platform safety.

Solutions

  1. Choose a different file name that is not a Windows device name
  2. Prefix or suffix the name (e.g. 'con-template.md') before calling ManageTemplateFiles
  3. Validate user-supplied names against the reserved list in the client before submitting

Example fix

// before
ManageTemplateFiles(newName: "con.md", ...)
// after
ManageTemplateFiles(newName: "con-notes.md", ...)
Defensive patterns

Strategy: validation

Validate before calling

var reserved = map[string]bool{"CON":true,"PRN":true,"AUX":true,"NUL":true}
func isReservedName(name string) bool {
    d := strings.ToUpper(strings.SplitN(name, ".", 2)[0])
    if reserved[d] { return true }
    if len(d) == 4 && (strings.HasPrefix(d,"COM")||strings.HasPrefix(d,"LPT")) && d[3]>='1' && d[3]<='9' { return true }
    return false
}

Prevention

When it happens

Trigger: ManageTemplateFiles receiving a new template name like 'con.md', 'lpt1.txt', 'Com3.md' — the check upper-cases the text before the first dot, so any extension does not help.

Common situations: Users on macOS/Linux choosing device-like names that are legal there but break on Windows, batch imports of files named after devices, or scripts generating template names from unfiltered input.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/template_manage.go:76

		return nil
	}
	if p == "" || !fs.ValidPath(p) || strings.ContainsAny(p, "\\:\x00") {
		return errors.New("invalid template path")
	}
	for _, part := range strings.Split(p, "/") {
		if strings.HasPrefix(part, ".") {
			return errors.New("hidden template paths are reserved")
		}
	}
	return nil
}

// 新名称保持跨平台可用,已有父目录沿用原名。
func validateNewTemplateName(p string) error {
	part := path.Base(p)
	device := strings.ToUpper(strings.SplitN(part, ".", 2)[0])
	if device == "CON" || device == "PRN" || device == "AUX" || device == "NUL" || (len(device) == 4 && (strings.HasPrefix(device, "COM") || strings.HasPrefix(device, "LPT")) && device[3] >= '1' && device[3] <= '9') {
		return errors.New("reserved template file name")
	}
	if strings.HasPrefix(part, ".") || strings.TrimSpace(part) != part || strings.HasSuffix(part, ".") || strings.ContainsAny(part, "\\:<>\"|?*") || strings.ContainsFunc(part, unicode.IsControl) {
		return errors.New("invalid template path component")
	}
	return nil
}

// 清单与目录共同标识模板包,保留目录身份以维持搜索和集市更新。
func isManagedTemplatePackage(root *os.Root, p string) bool {
	_, err := root.Lstat(path.Join(p, "template.json"))
	return err == nil
}

func openTemplateRoot() (*os.Root, error) {
	if err := os.MkdirAll(filepath.Join(util.DataDir, "templates"), 0755); err != nil {
		return nil, err
	}
	return os.OpenRoot(filepath.Join(util.DataDir, "templates"))

View on GitHub (pinned to 9f775e8a12)