{"record":{"id":"04347afcfc9f0d5b","repo":"siyuan-note/siyuan","slug":"template-document-tree-output-exceeds-d-bytes","errorCode":null,"errorMessage":"template document tree output exceeds %d bytes","messagePattern":"template document tree output exceeds (.+?) bytes","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree_render.go","lineNumber":45,"sourceCode":"\n\t\"github.com/88250/lute/ast\"\n\t\"github.com/88250/lute/parse\"\n\t\"github.com/siyuan-note/siyuan/kernel/av\"\n\t\"github.com/siyuan-note/siyuan/kernel/sql\"\n\t\"github.com/siyuan-note/siyuan/kernel/treenode\"\n\t\"github.com/siyuan-note/siyuan/kernel/util\"\n)\n\nfunc renderTemplateDocTreeNodes(collector *templateDocTreeCollector, rootTemplate *template.Template,\n\tfuncs template.FuncMap) error {\n\tfor _, node := range flattenTemplateDocTreeNodes0(collector.nodes) {\n\t\tcontent, err := renderTemplateDocTreeNodeContent(collector, rootTemplate, funcs, node)\n\t\tif nil != err {\n\t\t\treturn err\n\t\t}\n\t\tcollector.totalOutput += len(content)\n\t\tif maxTemplateDocTreeOutputSize < collector.totalOutput {\n\t\t\treturn fmt.Errorf(\"template document tree output exceeds %d bytes\", maxTemplateDocTreeOutputSize)\n\t\t}\n\t\ttree, err := renderTemplateDocTreeMarkdown(content, collector.boxID)\n\t\tif nil != err {\n\t\t\treturn err\n\t\t}\n\t\tcollector.buildTree(node, tree)\n\t}\n\treturn nil\n}\n\nfunc renderTemplateDocTreeNodeContent(collector *templateDocTreeCollector, rootTemplate *template.Template,\n\tfuncs template.FuncMap, node *TemplateDocTreeNode) ([]byte, error) {\n\tif \"\" == node.Template && \"\" == node.Define {\n\t\treturn nil, nil\n\t}\n\n\tpreviousAllowCreation := collector.allowCreation\n\tcollector.allowCreation = false","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree_render.go#L27-L63","documentation":"renderTemplateDocTreeNodes accumulates the rendered byte size of every node's content in collector.totalOutput and enforces maxTemplateDocTreeOutputSize across the whole render. When the aggregate output would exceed that limit, rendering is aborted with this error to bound memory use and prevent runaway templates from generating an unbounded number/size of documents.","triggerScenarios":"Rendering a template doc tree whose nodes collectively render more than maxTemplateDocTreeOutputSize bytes — e.g. a root template with many child nodes, loops over large data sets, or templates that inline huge content blocks per node.","commonSituations":"Very large imported data sets fed via .action{range} loops; recursive nesting of child templates each emitting large content; accidentally rendering an entire large collection into the document tree in one pass.","solutions":["Split the template into smaller batches and render/attach multiple smaller plans","Trim the template content: remove large blocks, reduce loop iterations, or paginate the data source","Raise maxTemplateDocTreeOutputSize only if the deployment genuinely needs bigger renders (memory tradeoff)","Check the rendered output size of each node during development and cap data inputs before rendering"],"exampleFix":"// before: one giant plan\nrenderTemplateDocTreeNodes(collector, rootTemplate, funcs) // exceeds limit\n// after: batch nodes\nfor batch := range chunkNodes(collector.nodes, 50) {\n    renderBatch(batch, rootTemplate, funcs)\n}","handlingStrategy":"validation","validationCode":"const maxTemplateDocTreeOutputSize = 10 * 1024 * 1024 // adjust per repo constant\nif estimatedTotalBytes(nodes) > maxTemplateDocTreeOutputSize {\n    return errors.New(\"render would exceed output limit; batch the template\")\n}","typeGuard":null,"tryCatchPattern":"content, err := renderTemplateDocTreeNodes(collector, rootTemplate, funcs)\nif err != nil && strings.Contains(err.Error(), \"output exceeds\") {\n    return retryWithSmallerBatch(ctx, remainingNodes)\n}","preventionTips":["Estimate rendered size from the data source before rendering","Batch large data sets into multiple smaller template plans","Avoid range loops over unbounded collections in templates"],"tags":["go","template","limit","resource"],"backgroundTag":"file-size-limit-exceeded","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"}