{"record":{"id":"86ff2faf29e21ecd","repo":"flipped-aurora/gin-vue-admin","slug":"gorm","errorCode":null,"errorMessage":"gorm 注入目标不能为空","messagePattern":"gorm 注入目标不能为空","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/package_initialize_gorm.go","lineNumber":91,"sourceCode":"\t\t\t\t\t\t\t\ti--\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true\n\t})\n\n\tif packageNameNum == 1 {\n\t\t_ = NewImport(a.ImportPath).Rollback(file)\n\t}\n\treturn nil\n}\n\nfunc (a *PackageInitializeGorm) Injection(file *ast.File) error {\n\tif file == nil {\n\t\treturn fmt.Errorf(\"gorm 注入目标不能为空\")\n\t}\n\tbizModelDecl := FindFunction(file, \"bizModel\")\n\tif bizModelDecl == nil || bizModelDecl.Body == nil {\n\t\treturn fmt.Errorf(\"gorm 注入目标缺少 bizModel 函数\")\n\t}\n\tif a.Business != \"\" {\n\t\tif err := a.addDbVar(bizModelDecl.Body); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tfound := false\n\t// 寻找目标结构\n\tast.Inspect(file, func(n ast.Node) bool {\n\t\t// 总调用的db变量根据business来决定\n\t\tvarDB := a.Business + \"Db\"\n\n\t\tif a.Business == \"\" {\n\t\t\tvarDB = \"db\"","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/package_initialize_gorm.go#L73-L109","documentation":"PackageInitializeGorm.Injection injects AutoMigrate registration into the package initialize file. The first guard rejects a nil *ast.File, meaning no parsed AST was supplied to inject into. The injector cannot operate without a parsed target file.","triggerScenarios":"Calling Injection(nil) directly; or a prior Parse step failed and its nil *ast.File return value was passed on to Injection without checking err first.","commonSituations":"Codegen pipeline ignores the error from Parse(filename, writer) and continues to Injection; a test harness invokes Injection without setting up the parsed file; refactored code reordered Parse/Injection and dropped the error check.","solutions":["Always check the error returned by Parse before calling Injection, and only pass the non-nil *ast.File","If Parse failed, fix the underlying file/read problem first, then re-run the pipeline","In tests, construct the *ast.File via the package's Parse helper rather than passing nil"],"exampleFix":"// before\nfile, _ := pkg.Parse(\"\", writer)\n_ = pkg.Injection(file) // file may be nil\n// after\nfile, err := pkg.Parse(\"\", writer)\nif err != nil {\n    return err\n}\nreturn pkg.Injection(file)","handlingStrategy":"type-guard","validationCode":"file, err := pkg.Parse(\"\", writer)\nif err != nil {\n    return err\n}\nif file == nil {\n    return fmt.Errorf(\"parse returned nil file\")\n}","typeGuard":"func hasParsedFile(f *ast.File) bool { return f != nil }","tryCatchPattern":"if err := pkg.Injection(file); err != nil {\n    if strings.Contains(err.Error(), \"注入目标不能为空\") {\n        return fmt.Errorf(\"pipeline bug: Parse result not passed through: %w\", err)\n    }\n    return err\n}","preventionTips":["Never ignore the error return of Parse; propagate it before Injection","Keep Parse and Injection calls adjacent in your codegen pipeline","Add a unit test asserting Injection(nil) is never reachable from your pipeline"],"tags":["go","ast","gorm","codegen","nil-check"],"backgroundTag":"nil-ast-target","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}