{"record":{"id":"37a96f868d6f3bc6","repo":"flipped-aurora/gin-vue-admin","slug":"gorm-import-w","errorCode":null,"errorMessage":"注入 gorm import 失败: %w","messagePattern":"注入 gorm import 失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/package_initialize_gorm.go","lineNumber":143,"sourceCode":"\t\tif !ok || ident.Name != varDB {\n\t\t\treturn true\n\t\t}\n\n\t\tfound = true\n\t\t// 添加结构体参数\n\t\tcallExpr.Args = append(callExpr.Args, &ast.CompositeLit{\n\t\t\tType: &ast.SelectorExpr{\n\t\t\t\tX:   ast.NewIdent(a.PackageName),\n\t\t\t\tSel: ast.NewIdent(a.StructName),\n\t\t\t},\n\t\t})\n\t\treturn false\n\t})\n\tif !found {\n\t\treturn fmt.Errorf(\"bizModel 中未找到 %s.AutoMigrate 调用\", a.autoMigrateDBName())\n\t}\n\tif err := NewImport(a.ImportPath).Injection(file); err != nil {\n\t\treturn fmt.Errorf(\"注入 gorm import 失败: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (a *PackageInitializeGorm) Format(filename string, writer io.Writer, file *ast.File) error {\n\tif filename == \"\" {\n\t\tfilename = a.Path\n\t}\n\treturn a.Base.Format(filename, writer, file)\n}\n\nfunc (a *PackageInitializeGorm) autoMigrateDBName() string {\n\tif a.Business == \"\" {\n\t\treturn \"db\"\n\t}\n\treturn a.Business + \"Db\"\n}\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/package_initialize_gorm.go#L125-L161","documentation":"Once the AutoMigrate call is found and updated, Injection ensures the model's package is imported by delegating to NewImport(a.ImportPath).Injection(file). This error wraps any failure from that import-injection step (%w preserves the cause), e.g. the ImportPath is empty or the import block could not be updated.","triggerScenarios":"PackageInitializeGorm.ImportPath is empty/blank or malformed, or the underlying import injector fails to locate/modify the file's import declaration.","commonSituations":"Struct field ImportPath forgotten when constructing PackageInitializeGorm; typo in the module path; target file has no import block the injector recognizes.","solutions":["Set ImportPath to the full import path of the model package (e.g. \"github.com/yourorg/server/model/xxx\")","Unwrap the cause (%w) to see the underlying import-injection failure and fix it","Ensure the target file has a standard import declaration block the injector can edit","Re-run the injection with a valid ImportPath"],"exampleFix":"// before\ninj := &ast.PackageInitializeGorm{PackageName: \"article\", StructName: \"Article\"}\n// after\ninj := &ast.PackageInitializeGorm{PackageName: \"article\", StructName: \"Article\", ImportPath: \"server/model/article\"}","handlingStrategy":"validation","validationCode":"if inj.ImportPath == \"\" {\n    return fmt.Errorf(\"ImportPath is required\")\n}\n// optionally verify the package resolves:\n// go/packages or go/build.Import(inj.ImportPath, \"\", build.FindOnly)","typeGuard":null,"tryCatchPattern":"if err := inj.Injection(file); err != nil {\n    var inner error\n    if errors.Unwrap(err) != nil { inner = errors.Unwrap(err) }\n    return fmt.Errorf(\"check ImportPath %q: %w (cause: %v)\", inj.ImportPath, err, inner)\n}","preventionTips":["Always populate ImportPath with the model package's full import path","Validate the import path compiles (go/build or gopls check) before codegen","Unwrap the %w cause to distinguish empty-path vs import-block failures"],"tags":["go","ast","gorm","import-injection","codegen"],"backgroundTag":"import-injection-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}