{"record":{"id":"5f80bb8b51c4d48d","repo":"siyuan-note/siyuan","slug":"failed-to-enable-extend-modules-v","errorCode":null,"errorMessage":"failed to enable extend modules: %v","messagePattern":"failed to enable extend modules: (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/plugin/sandbox.go","lineNumber":79,"sourceCode":"type FunctionResult[T any] struct {\n\tValue T\n\tError error\n}\n\ntype CallResult FunctionResult[goja.Value]\n\nfunc (r *CallResult) TaskResult() *TaskResult {\n\tif r.Error != nil {\n\t\treturn &TaskResult{err: r.Error}\n\t}\n\treturn &TaskResult{value: r.Value.Export()}\n}\n\n// EnableExtendModules registers extended modules (e.g. url, buffer) to the plugin's goja runtime.\nfunc EnableExtendModules(p *KernelPlugin, rt *goja.Runtime) (err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = fmt.Errorf(\"failed to enable extend modules: %v\", r)\n\t\t}\n\t}()\n\n\tregistry := require.NewRegistry()\n\n\tregistry.Enable(rt)\n\tregistry.RegisterNativeModule(\n\t\tconsole.ModuleName,\n\t\tconsole.RequireWithPrinter(&Printer{name: p.Name}),\n\t)\n\n\turl.Enable(rt)\n\tbuffer.Enable(rt)\n\tconsole.Enable(rt)\n\treturn\n}\n\n// EnableSiyuanModule injects all siyuan.* APIs into the plugin's goja global context.","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/plugin/sandbox.go#L61-L97","documentation":"EnableExtendModules registers extended CommonJS modules (url, buffer, etc.) into a plugin's goja runtime. Registration can panic inside goja (bad module definition, runtime in a bad state); the deferred recover converts that panic into this wrapped error so the kernel does not crash.","triggerScenarios":"Calling EnableExtendModules (directly or during plugin loading) when the goja require registry setup panics — e.g. a nil *goja.Runtime, a runtime already interrupted/interrupted by timeout, or a module registrar that panics.","commonSituations":"Loading a plugin against a runtime that was previously interrupted by a script timeout; goja runtime misconfiguration during sandbox bootstrap; panicking custom module registrations added to the registry.","solutions":["Check the wrapped panic message (%v) to find the root cause inside the registry setup","Ensure the goja runtime is freshly created and not interrupted before calling EnableExtendModules","Verify no custom module registration code panics (nil maps, invalid exports)","Catch this error at plugin-load time and fail the plugin load gracefully instead of retrying on a poisoned runtime","Upgrade/review goja usage if the panic stems from a goja-internal bug"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if rt == nil || p == nil {\n    return fmt.Errorf(\"runtime and plugin must be initialized before EnableExtendModules\")\n}","typeGuard":null,"tryCatchPattern":"if err := EnableExtendModules(p, rt); err != nil {\n    log.Errorf(\"plugin %s sandbox init failed: %v\", p.Name, err)\n    return fmt.Errorf(\"plugin load aborted\")\n}","preventionTips":["Create a fresh goja.Runtime per plugin and never reuse interrupted runtimes","Check the wrapped panic text for the root cause before retrying","Fail plugin load cleanly instead of reusing a half-initialized runtime","Keep custom module registrations panic-free"],"tags":["goja","javascript","plugin-sandbox","panic-recovery"],"backgroundTag":"module-init-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}