{"record":{"id":"1b5d86741ad3c401","repo":"siyuan-note/siyuan","slug":"template-already-exists-use-overwrite-to-replac","errorCode":null,"errorMessage":"template already exists, use --overwrite to replace: %s","messagePattern":"template already exists, use --overwrite to replace: (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/cli/cmd/template.go","lineNumber":152,"sourceCode":"\t\tid, _ := cmd.Flags().GetString(\"id\")\n\t\tname, _ := cmd.Flags().GetString(\"name\")\n\t\tif id == \"\" {\n\t\t\treturn fmt.Errorf(\"--id is required\")\n\t\t}\n\t\tif name == \"\" {\n\t\t\treturn fmt.Errorf(\"--name is required\")\n\t\t}\n\t\toverwrite, _ := cmd.Flags().GetBool(\"overwrite\")\n\t\tif dryRun {\n\t\t\tfmt.Printf(\"[dry-run] Would save document %s as template \\\"%s\\\"\\n\", id, name)\n\t\t\treturn nil\n\t\t}\n\t\tcode, err := model.DocSaveAsTemplate(id, name, overwrite)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif code == 1 {\n\t\t\treturn fmt.Errorf(\"template already exists, use --overwrite to replace: %s\", name)\n\t\t}\n\t\tfmt.Printf(\"%s.md\\n\", name)\n\t\treturn nil\n\t},\n}\n\nvar templateCreateCmd = &cobra.Command{\n\tUse:   \"create --name <name> [--data <markdown> | --file <path>]\",\n\tShort: \"Create a template from markdown content\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tname, _ := cmd.Flags().GetString(\"name\")\n\t\tif name == \"\" {\n\t\t\treturn fmt.Errorf(\"--name is required\")\n\t\t}\n\t\tcontent, err := resolveData(cmd)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/cli/cmd/template.go#L134-L170","documentation":"Thrown by the `template save-as` cobra subcommand when `model.DocSaveAsTemplate` returns code 1, meaning a template file with the given name already exists under `data/templates/` and `--overwrite` was not set. The engine returns a status code (1 = exists) rather than a Go error, so the CLI translates it into an actionable message. The error names the conflicting template name.","triggerScenarios":"Running `siyuan template save-as --id <id> --name <existingName>` without `--overwrite`, where `<name>.md` already exists in `data/templates/`. Re-running a save-as after a previous successful run with the same name.","commonSituations":"Iterating on a template and re-saving; two documents saved as templates with the same name; scripting template generation without idempotency checks.","solutions":["Add `--overwrite` to replace the existing template: `siyuan template save-as --id <id> --name <name> --overwrite`.","Choose a different `--name` to avoid clobbering.","Remove the old template first with `siyuan template remove --path <name>.md` if you prefer not to overwrite."],"exampleFix":"// before\nsiyuan template save-as --id 20240101000000abc123 --name daily\n// after\nsiyuan template save-as --id 20240101000000abc123 --name daily --overwrite","handlingStrategy":"validation","validationCode":"// Check for an existing template file before calling save-as without --overwrite.\nexisting := filepath.Join(util.DataDir, \"templates\", name+\".md\")\nif _, err := os.Stat(existing); err == nil && !overwrite {\n    return fmt.Errorf(\"template already exists: %s — re-run with --overwrite\", name)\n}","typeGuard":null,"tryCatchPattern":"// Distinguish the exists-code-1 path from real errors.\ncode, err := model.DocSaveAsTemplate(id, name, overwrite)\nif err != nil { return err }\nif code == 1 { /* prompt user, then retry with overwrite=true */ }","preventionTips":["Use `--overwrite` when re-saving a template you intend to update.","Pick unique template names in automation to avoid conflicts.","Pre-check `data/templates/<name>.md` existence before scripting save-as."],"tags":["cli","template","conflict","filesystem","overwrite"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}