{"record":{"id":"24e2ec59b3567ef9","repo":"dgraph-io/dgraph","slug":"no-zerohooks-configured-ensure-worker-package-is","errorCode":null,"errorMessage":"no ZeroHooks configured - ensure worker package is imported or hooks.Enable() is called","messagePattern":"no ZeroHooks configured - ensure worker package is imported or hooks\\.Enable\\(\\) is called","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"hooks/config.go","lineNumber":120,"sourceCode":"\treturn globalConfig.Load()\n}\n\n// SetDefaultZeroHooks registers the fallback ZeroHooks used when embedded mode is\n// disabled or Config.ZeroHooks is nil. The worker package calls this from its init.\nfunc SetDefaultZeroHooks(h ZeroHooks) {\n\tdefaultZeroHooks.Store(&defaultHooksHolder{hooks: h})\n}\n\n// GetHooks returns the active Zero hooks.\n// If embedded mode is not enabled, it returns the default hooks implementation.\nfunc GetHooks() ZeroHooks {\n\tif cfg := globalConfig.Load(); cfg != nil && cfg.ZeroHooks != nil {\n\t\treturn cfg.ZeroHooks\n\t}\n\tif h := defaultZeroHooks.Load(); h != nil {\n\t\treturn h.hooks\n\t}\n\tpanic(\"no ZeroHooks configured - ensure worker package is imported or hooks.Enable() is called\")\n}\n","sourceCodeStart":102,"sourceCodeEnd":122,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/hooks/config.go#L102-L122","documentation":"GetHooks returns the configured ZeroHooks, which must be registered either by importing the worker package (which sets them via init) or by explicitly calling hooks.Enable()/SetDefaultZeroHooks. If neither global config nor a default is present, it panics rather than returning nil hooks.","triggerScenarios":"Calling GetHooks (directly or via workers like AssignNsIdsOverNetwork) in a binary/test that never imports the worker package and never calls hooks.Enable() or sets a default ZeroHooks.","commonSituations":"Unit tests exercising hook-consuming code without the worker import, trimming imports to break the init-side-effect registration chain, or new entry points wired before hook initialization.","solutions":["Call hooks.Enable() (or hooks.SetDefaultZeroHooks(...)) during startup before any GetHooks call","Import the worker package for its init side effect (blank import: _ \"your/module/worker\")","In tests, set default hooks in TestMain or a setup fixture","Ensure the global config with ZeroHooks is loaded before use"],"exampleFix":"// before\nfunc main() {\n    StartWorker() // panics: no ZeroHooks\n}\n// after\nimport _ \"your/module/worker\"\nfunc main() {\n    hooks.Enable()\n    StartWorker()\n}","handlingStrategy":"try-catch","validationCode":"if cfg := hooks.GlobalConfig(); cfg == nil || cfg.ZeroHooks == nil {\n  if !hooks.DefaultsSet() { hooks.Enable() }\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n  if r := recover(); r != nil {\n    if strings.Contains(fmt.Sprint(r), \"no ZeroHooks configured\") {\n      hooks.Enable()\n      // retry operation\n    } else { panic(r) }\n  }\n}()","preventionTips":["Call hooks.Enable() at process startup before any worker activity","Blank-import the worker package for its init side effects","Initialize default hooks in TestMain for test binaries"],"tags":["go","hooks","initialization","panic"],"backgroundTag":"hooks-not-configured","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}