{"record":{"id":"2ce653ec1f5ac393","repo":"golang-migrate/migrate","slug":"expected-instance-of-pkging-pkger","errorCode":null,"errorMessage":"expected instance of pkging.Pkger","messagePattern":"expected instance of pkging\\.Pkger","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"source/pkger/pkger.go","lineNumber":57,"sourceCode":"\t\t\treturn nil, err\n\t\t}\n\t\treturn f.(http.File), nil\n\t})\n\n\tif err := p.Init(fs, u.Path); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to init driver with relative path %q: %w\", u.Path, err)\n\t}\n\n\treturn p, nil\n}\n\n// WithInstance returns a source.Driver that is backed by an instance of\n// pkging.Pkger. The relative location of migrations is indicated by path. The\n// path must exist on the pkging.Pkger instance for the driver to initialize\n// successfully.\nfunc WithInstance(instance pkging.Pkger, path string) (source.Driver, error) {\n\tif instance == nil {\n\t\treturn nil, fmt.Errorf(\"expected instance of pkging.Pkger\")\n\t}\n\n\t// wrap pkger to implement http.FileSystem.\n\tfs := fsFunc(func(name string) (http.File, error) {\n\t\tf, err := instance.Open(name)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn f.(http.File), nil\n\t})\n\n\tvar p Pkger\n\n\tif err := p.Init(fs, path); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to init driver with relative path %q: %w\", path, err)\n\t}\n\n\treturn &p, nil","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/golang-migrate/migrate/blob/01a9643f1475e75bb6d6224ddeaf9d8e2434ca8a/source/pkger/pkger.go#L39-L75","documentation":"source/pkger.WithInstance requires a non-nil pkging.Pkger instance because the driver is backed entirely by that embedded filesystem. Passing nil would panic later when wrapping instance.Open into an http.FileSystem, so the function returns \"expected instance of pkging.Pkger\" immediately.","triggerScenarios":"Calling pkger.WithInstance(nil, \"/migrations\"), typically because a variable holding the pkger instance was never initialized or an interface value is a nil *pkgs.Pkgs typed nil.","commonSituations":"Refactoring code so the pkger instance is conditionally created, dependency-injection wiring that leaves the instance nil, or passing a typed nil interface from another package.","solutions":["Pass a real pkger instance, e.g. pkger.WithInstance(pkger.New(), \"/migrations\").","Check the variable feeding WithInstance for nil before calling it, including typed-nil interface cases.","If you don't need a custom instance, use source/pkger's Open(url) instead of WithInstance."],"exampleFix":"// before\nvar p pkging.Pkger\nsrc, err := pkger.WithInstance(p, \"/migrations\") // panics/errs: nil\n// after\np := pkger.New()\nsrc, err := pkger.WithInstance(p, \"/migrations\")","handlingStrategy":"validation","validationCode":"var p pkging.Pkger = buildPkger()\nif p == nil {\n\treturn errors.New(\"pkger instance must be non-nil before WithInstance\")\n}\nsrc, err := pkger.WithInstance(p, \"/migrations\")","typeGuard":"func isNilInterface(v interface{}) bool {\n\tif v == nil { return true }\n\trv := reflect.ValueOf(v)\n\tswitch rv.Kind() {\n\tcase reflect.Ptr, reflect.Interface, reflect.Map, reflect.Slice:\n\t\treturn rv.IsNil()\n\t}\n\treturn false\n}","tryCatchPattern":"if err != nil {\n\tif err.Error() == \"expected instance of pkging.Pkger\" {\n\t\treturn fmt.Errorf(\"pkger instance not initialized: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Construct the pkger instance immediately before WithInstance, not in a separate failure-prone step.","Never assign to an interface variable from a function that can return nil on error.","Document that WithInstance requires a live pkging.Pkger, not a typed nil."],"tags":["go","migrate","pkger","nil-argument"],"backgroundTag":"nil-argument-rejected","analyzedSha":"01a9643f1475e75bb6d6224ddeaf9d8e2434ca8a","analyzedAt":"2026-09-02T19:38:29.671Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}