{"record":{"id":"1ec43a09f141161e","repo":"siyuan-note/siyuan","slug":"template-changed-reload-it-before-saving-moving","errorCode":null,"errorMessage":"template changed; reload it before saving, moving or deleting","messagePattern":"template changed; reload it before saving, moving or deleting","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":274,"sourceCode":"\t\tif !strings.EqualFold(path.Ext(request.Path), \".md\") {\n\t\t\treturn nil, errors.New(\"template source must use the .md extension\")\n\t\t}\n\t}\n\tif request.Action == \"read\" {\n\t\tif info.IsDir() {\n\t\t\trevision, readErr := templateFileRevision(root, request.Path)\n\t\t\treturn map[string]string{\"content\": \"\", \"revision\": revision}, readErr\n\t\t}\n\t\tcontent, readErr := readTemplateSource(root, request.Path)\n\t\treturn map[string]string{\"content\": content, \"revision\": fmt.Sprintf(\"%x\", sha256.Sum256([]byte(content))), \"path\": filepath.Join(util.DataDir, \"templates\", filepath.FromSlash(request.Path))}, readErr\n\t}\n\tif info != nil {\n\t\trevision, revisionErr := templateFileRevision(root, request.Path)\n\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\":","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L256-L292","documentation":"ManageTemplateFiles implements optimistic concurrency: when the target template already exists, its current revision (a content hash) is compared with the Revision supplied in the request. If the client sends an empty Revision or a stale one, the write/move/remove is rejected so a user does not silently overwrite or delete changes made elsewhere. The caller must re-read the template to get the fresh revision before retrying.","triggerScenarios":"Calling ManageTemplateFiles with Action 'write', 'move', or 'remove' on an existing path while request.Revision is empty or differs from templateFileRevision(root, path) — i.e. the file's hash changed since the client last read it.","commonSituations":"Two windows or devices edit the same template and the second save carries an outdated hash; a sync operation updated the template between the client's read and write; a caller never fetched the revision (sent Revision: \"\") for an existing file; clocks/sync reordered saves.","solutions":["Re-read the template (Action \"read\") to obtain the current revision, then resend the request with that Revision value","If you intend to create a new file, ensure the path does not exist — new files with Revision==\"\" are allowed (stat must return ErrNotExist)","Detect the conflict in UI: show 'template changed' and let the user merge/reload before saving","Retry only after confirming with the user that overwriting the newer external change is acceptable"],"exampleFix":"// before\nmanage(TemplateFileRequest{Action: \"write\", Path: \"t.md\", Content: c, Revision: oldRev})\n// after\nread := manage(TemplateFileRequest{Action: \"read\", Path: \"t.md\"})\nmanage(TemplateFileRequest{Action: \"write\", Path: \"t.md\", Content: c, Revision: read[\"revision\"]})","handlingStrategy":"retry","validationCode":"// read current revision first\nres, _ := manage(TemplateFileRequest{Action: \"read\", Path: p})\nrev := res.(map[string]string)[\"revision\"]","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"template changed\") {\n    fresh, _ := manage(TemplateFileRequest{Action: \"read\", Path: p})\n    req.Revision = fresh.(map[string]string)[\"revision\"]\n    // retry after user confirms merge/overwrite\n}","preventionTips":["Always carry the revision from the last read into write/move/remove","Refresh the editor state on sync/push events that may change templates","Surface revision conflicts in UI instead of auto-overwriting","Never cache template revisions across long-running sessions"],"tags":["concurrency","optimistic-locking","conflict"],"backgroundTag":"invalid-state-transition","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"}