{"record":{"id":"992bc0b205ec3b96","repo":"flipped-aurora/gin-vue-admin","slug":"gorm-import-w-992bc0","errorCode":null,"errorMessage":"注入插件 gorm import 失败: %w","messagePattern":"注入插件 gorm import 失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/plugin_initialize_gorm.go","lineNumber":119,"sourceCode":"\t\t\ttargetCall = callExpr\n\t\t\treturn false\n\t\t}\n\n\t\treturn true\n\t})\n\n\tif targetCall == nil {\n\t\ttargetCall = a.appendAutoMigrateBlock(file)\n\t}\n\tif targetCall == nil {\n\t\treturn fmt.Errorf(\"插件 gorm 注入目标缺少 Gorm 函数或 AutoMigrate 调用\")\n\t}\n\n\tif a.hasModelArg(targetCall) {\n\t\treturn nil\n\t}\n\tif err := NewImport(a.ImportPath).Injection(file); err != nil {\n\t\treturn fmt.Errorf(\"注入插件 gorm import 失败: %w\", err)\n\t}\n\n\ttargetCall.Args = append(targetCall.Args, &ast.CompositeLit{\n\t\tType: &ast.SelectorExpr{\n\t\t\tX:   &ast.Ident{Name: a.PackageName},\n\t\t\tSel: &ast.Ident{Name: a.StructName},\n\t\t},\n\t})\n\treturn nil\n}\n\nfunc (a *PluginInitializeGorm) 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","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/plugin_initialize_gorm.go#L101-L137","documentation":"This error is produced during AST-based code injection into a plugin's initialize GORM file. Before appending a composite literal referencing the plugin's package, the injector calls NewImport(...).Injection(file) to add the required gorm import; if that import injection fails, the original error is wrapped as \"注入插件 gorm import 失败\". It means the code generator could not modify the plugin's import block, usually because the file's AST structure is unexpected or the import already conflicts.","triggerScenarios":"Running the auto-code plugin initialization (injectPluginInitializeGorm / Injection on plugin_initialize_gorm.go) when the target plugin's initialize gorm file cannot be parsed or its import block cannot be updated — e.g. the plugin's gorm.go has a malformed or non-standard import section, or NewImport(a.ImportPath).Injection(file) returns an error.","commonSituations":"Generating a new plugin whose plugin/xxx/initialize/gorm.go template was hand-edited and lost the standard import block; a file that fails go/parser; duplicate/conflicting import declarations inserted by previous failed runs.","solutions":["Inspect the wrapped cause (%w) in the error message to see the underlying import-injection failure and fix that root problem.","Restore plugin/xxx/initialize/gorm.go to the standard generated template (valid package clause, proper import block) and re-run the code generator.","Verify the file parses with go/parser (gofmt the file) before injection.","If a previous partial injection left a duplicate import, remove it manually and retry."],"exampleFix":"// before (malformed plugin gorm.go)\npackage initialize\nimport (\n  \"gorm.io/gorm\"\n)\n\n// after (standard template, parses and imports cleanly)\npackage initialize\n\nimport (\n  \"github.com/flipped-aurora/gin-vue-admin/server/plugin/xxx/model\"\n  \"go.uber.org/zap\"\n  \"gorm.io/gorm\"\n)","handlingStrategy":"validation","validationCode":"src, err := os.ReadFile(pluginGormFile)\nif err != nil { return err }\nif _, err := parser.ParseFile(fset, pluginGormFile, src, parser.ParseComments); err != nil {\n    return fmt.Errorf(\"plugin gorm file does not parse: %w\", err)\n}\nif !bytes.Contains(src, []byte(\"import (\")) {\n    return errors.New(\"plugin gorm file lacks an import block\")\n}","typeGuard":null,"tryCatchPattern":"if err := injectGormInit(plugin); err != nil {\n    var wrapped error\n    if errors.As(err, &wrapped) {\n        log.Printf(\"plugin gorm injection failed: %+v\", wrapped)\n    }\n    return fmt.Errorf(\"regenerate plugin files and retry: %w\", err)\n}","preventionTips":["Never hand-edit generated plugin gorm.go templates; regenerate instead","Run gofmt on generated files before injection","Check the wrapped cause for the true import failure","Keep generated plugin structure aligned with the official template"],"tags":["ast","code-generation","go","plugin"],"backgroundTag":"ast-import-injection-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}