{"record":{"id":"c243157db9a942cc","repo":"flipped-aurora/gin-vue-admin","slug":"filepath-s-c24315","errorCode":null,"errorMessage":"[filepath:%s]格式化注入代码失败","messagePattern":"\\[filepath:(.+?)\\]格式化注入代码失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_template.go","lineNumber":215,"sourceCode":"\t\t\t}\n\t\t\tif !info.AutoMigrate {\n\t\t\t\tif keys[1] == utilsAst.TypePackageInitializeGorm || keys[1] == utilsAst.TypePluginInitializeGorm {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tvar builder strings.Builder\n\t\t\tparsed, err := value.Parse(\"\", &builder)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, nil, errors.Wrapf(err, \"[filepath:%s]解析注入目标失败\", keys[0])\n\t\t\t}\n\t\t\tif parsed == nil {\n\t\t\t\treturn nil, nil, errors.Errorf(\"[filepath:%s]解析注入目标为空\", keys[0])\n\t\t\t}\n\t\t\tif err = value.Injection(parsed); err != nil {\n\t\t\t\treturn nil, nil, errors.Wrapf(err, \"[filepath:%s]注入代码失败\", keys[0])\n\t\t\t}\n\t\t\tif err = value.Format(\"\", &builder, parsed); err != nil {\n\t\t\t\treturn nil, nil, errors.Wrapf(err, \"[filepath:%s]格式化注入代码失败\", keys[0])\n\t\t\t}\n\t\t\tcode[keys[0]] = builder\n\t\t\tinjections[keys[1]] = value\n\t\t\tfmt.Println(keys[0], \"注入成功!\")\n\t\t}\n\t}\n\treturn code, injections, nil\n}\n\nfunc (s *autoCodeTemplate) AddFunc(ctx context.Context, info request.AutoFunc) error {\n\tautoPkg := model.SysAutoCodePackage{}\n\terr := global.GVA_DB.WithContext(ctx).First(&autoPkg, \"package_name = ?\", info.Package).Error\n\tif err != nil {\n\t\treturn err\n\t}\n\tif autoPkg.Template != \"package\" {\n\t\tinfo.IsPlugin = true\n\t}","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_template.go#L197-L233","documentation":"After injecting, value.Format renders the modified AST back to formatted Go source; failure is wrapped as '[filepath:%s]格式化注入代码失败'. The injection happened, but go/format could not pretty-print the resulting tree — usually a malformed AST or an issue serializing positions/comments.","triggerScenarios":"renderAutoCodeInjections when the injected snippet produces an AST that go/format rejects — e.g. injected code with invalid tokens, duplicate declarations, or the template emitted text that breaks Go syntax after merge.","commonSituations":"Custom injection templates generating syntactically invalid Go (bad imports, stray characters); injected identifier collides with an existing declaration in the file; format_node receiving comments positioned outside the file set.","solutions":["Read the wrapped format error (it names the offending node/token) and fix the injection template that produced it.","Check for duplicate identifiers — if the generated function/import already exists in <filepath>, remove the old one or skip re-running the injection.","Run gofmt on <filepath> after fixing, and verify the file compiles with go build."],"exampleFix":"// before: injection created duplicate import\nimport \"gin-vue-admin/utils\"\nimport \"gin-vue-admin/utils\"\n// after: single import merged\nimport \"gin-vue-admin/utils\"","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"格式化注入代码失败\") {\n  // read wrapped go/format error, fix the injection template, then:\n  // exec: gofmt -w <filepath> && go build ./...\n  return fmt.Errorf(\"fix injection template output: %w\", err)\n}","preventionTips":["Ensure injection templates emit syntactically valid, non-duplicating Go.","Skip already-injected code (check for existing identifiers before injecting).","Run go build after each generation to catch formatting fallout immediately."],"tags":["ast","codegen","gofmt","injection"],"backgroundTag":"gofmt-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}