flipped-aurora/gin-vue-admin · error

gorm 注入目标不能为空

Error message

gorm 注入目标不能为空

What it means

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.

Source

Thrown at server/utils/ast/package_initialize_gorm.go:91

								i--
							}
						}
					}
				}
			}
		}
		return true
	})

	if packageNameNum == 1 {
		_ = NewImport(a.ImportPath).Rollback(file)
	}
	return nil
}

func (a *PackageInitializeGorm) Injection(file *ast.File) error {
	if file == nil {
		return fmt.Errorf("gorm 注入目标不能为空")
	}
	bizModelDecl := FindFunction(file, "bizModel")
	if bizModelDecl == nil || bizModelDecl.Body == nil {
		return fmt.Errorf("gorm 注入目标缺少 bizModel 函数")
	}
	if a.Business != "" {
		if err := a.addDbVar(bizModelDecl.Body); err != nil {
			return err
		}
	}
	found := false
	// 寻找目标结构
	ast.Inspect(file, func(n ast.Node) bool {
		// 总调用的db变量根据business来决定
		varDB := a.Business + "Db"

		if a.Business == "" {
			varDB = "db"

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Always check the error returned by Parse before calling Injection, and only pass the non-nil *ast.File
  2. If Parse failed, fix the underlying file/read problem first, then re-run the pipeline
  3. In tests, construct the *ast.File via the package's Parse helper rather than passing nil

Example fix

// before
file, _ := pkg.Parse("", writer)
_ = pkg.Injection(file) // file may be nil
// after
file, err := pkg.Parse("", writer)
if err != nil {
    return err
}
return pkg.Injection(file)
Defensive patterns

Strategy: type-guard

Validate before calling

file, err := pkg.Parse("", writer)
if err != nil {
    return err
}
if file == nil {
    return fmt.Errorf("parse returned nil file")
}

Type guard

func hasParsedFile(f *ast.File) bool { return f != nil }

Try / catch

if err := pkg.Injection(file); err != nil {
    if strings.Contains(err.Error(), "注入目标不能为空") {
        return fmt.Errorf("pipeline bug: Parse result not passed through: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/86ff2faf29e21ecd. Report an issue: GitHub.