{"record":{"id":"1e27845ca458fac7","repo":"flipped-aurora/gin-vue-admin","slug":"filepath-s-1e2784","errorCode":null,"errorMessage":"[filepath:%s]注入代码失败","messagePattern":"\\[filepath:(.+?)\\]注入代码失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_template.go","lineNumber":212,"sourceCode":"\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\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}","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_template.go#L194-L230","documentation":"value.Injection performs the actual AST mutation that inserts generated code into the parsed target; any error is wrapped as '[filepath:%s]注入代码失败'. The file parsed fine, but the injector could not locate or modify the expected node (function, struct, route group) to attach the new code.","triggerScenarios":"renderAutoCodeInjections when the target file lacks the structure the injector expects — e.g. missing router-group variable, missing method receiver, or the anchor declaration was renamed — so the AST rewrite fails.","commonSituations":"Heavy manual refactoring of routers/api removed the anchor the injector looks for; a differently-styled scaffold (forked project) where the target declaration differs from gin-vue-admin's expected shape; conflicting partial injections from earlier runs.","solutions":["Compare <filepath> with the stock gin-vue-admin version of that file and restore the expected anchor (router group variable, api method, etc.).","Read the wrapped cause for the precise node that failed; add back the missing declaration or rename your code to match the injector's expectation.","As a workaround, disable the injection entry for that template and add the code manually, keeping the rest of generation."],"exampleFix":"// before: anchor renamed\nvar publicGroup = RouterGroup{}\n// after: expected anchor restored\nvar publicGroup *gin.RouterGroup","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"injected, injections, err := renderAutoCodeInjections(info, asts)\nif err != nil && strings.Contains(err.Error(), \"注入代码失败\") {\n  log.Printf(\"injection skipped, add code manually at %s: %v\", targetPath, err)\n  // fall back to manual insertion instead of aborting the whole generation\n}","preventionTips":["Keep router/api anchor declarations (group variables, receivers) in stock form.","Re-run generation from a clean checkout when injection anchors were refactored.","Document which files the injector touches so refactors preserve expected structure."],"tags":["ast","codegen","injection","ast-rewrite"],"backgroundTag":"code-injection-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}