{"record":{"id":"732f8b187b3830fb","repo":"flipped-aurora/gin-vue-admin","slug":"filpath-s-732f8b","errorCode":null,"errorMessage":"[filpath:%s]生成文件失败!","messagePattern":"\\[filpath:(.+?)\\]生成文件失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_template.go","lineNumber":169,"sourceCode":"\treturn preview, nil\n}\n\nfunc (s *autoCodeTemplate) generate(ctx context.Context, info request.AutoCode, entity model.SysAutoCodePackage) (map[string]strings.Builder, map[string]string, map[string]utilsAst.Ast, error) {\n\ttemplates, asts, _, err := AutoCodePackage.templates(ctx, entity, info, false)\n\tif err != nil {\n\t\treturn nil, nil, nil, err\n\t}\n\tcode := make(map[string]strings.Builder)\n\tfor key, create := range templates {\n\t\tvar files *template.Template\n\t\tfiles, err = template.New(filepath.Base(key)).Funcs(autocode.GetTemplateFuncMap()).ParseFiles(key)\n\t\tif err != nil {\n\t\t\treturn nil, nil, nil, errors.Wrapf(err, \"[filpath:%s]读取模版文件失败!\", key)\n\t\t}\n\t\tvar builder strings.Builder\n\t\terr = files.Execute(&builder, info)\n\t\tif err != nil {\n\t\t\treturn nil, nil, nil, errors.Wrapf(err, \"[filpath:%s]生成文件失败!\", create)\n\t\t}\n\t\tcode[create] = builder\n\t} // 生成文件\n\tinjectedCode, injections, err := renderAutoCodeInjections(info, asts)\n\tif err != nil {\n\t\treturn nil, nil, nil, err\n\t}\n\tfor key, builder := range injectedCode {\n\t\tcode[key] = builder\n\t}\n\t// 注入代码\n\treturn code, templates, injections, nil\n}\n\nfunc renderAutoCodeInjections(info request.AutoCode, asts map[string]utilsAst.Ast) (map[string]strings.Builder, map[string]utilsAst.Ast, error) {\n\tcode := make(map[string]strings.Builder, len(asts))\n\tinjections := make(map[string]utilsAst.Ast, len(asts))\n\tfor key, value := range asts {","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_template.go#L151-L187","documentation":"After a template parses successfully, generate executes it against the request.AutoCode data; an execution error is wrapped as '[filpath:%s]生成文件失败!'. This means the template body itself failed while rendering (missing method/field on data, index out of range, custom template function error), not that the file is unreadable.","triggerScenarios":"files.Execute(&builder, info) fails during Create or Preview when: the .tpl calls a field/method absent from request.AutoCode, a template func from GetTemplateFuncMap errors (e.g. unmapped DB type), or data assumptions break (nil maps/slices indexed by the template).","commonSituations":"Custom templates written against an older AutoCode struct after upgrading; unsupported database column type passed into a type-mapping template function; template assumes a non-empty collection the request does not provide.","solutions":["Read the wrapped cause printed with the error — it names the exact template line/executing action; fix that line in the .tpl.","Check any custom template functions used exist in autocode.GetTemplateFuncMap and support the input type (e.g. DB column type mapping) for your field definitions.","If this started after an upgrade, diff your custom .tpl files against the version-matched official templates and update accesses to renamed AutoCode fields."],"exampleFix":"{{/* before: assumes field exists */}}\n{{ .TableName .StructName }}\n{{/* after: guard */}}\n{{ if .StructName }}{{ .TableName .StructName }}{{ end }}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"out, _, _, err := svc.generate(info, templates, asts)\nif err != nil {\n  if strings.Contains(err.Error(), \"生成文件失败\") {\n    // parse wrapped template exec error to locate the failing tpl\n    log.Printf(\"template execution failed, check recent .tpl edits: %v\", err)\n  }\n  return err\n}","preventionTips":["After upgrading gin-vue-admin, diff custom templates against the new official ones.","Test codegen with a minimal struct definition before large generations.","Keep custom template funcs registered in GetTemplateFuncMap typed and nil-safe."],"tags":["template","codegen","template-execution","text-template"],"backgroundTag":"template-execution-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}