siyuan-note/siyuan · error

failed to inject global context

Error message

failed to inject global context: %v

What it means

EnableSiyuanModule injects the global siyuan.* API surface into the plugin's goja runtime. Any panic during injection (object creation, injector functions like injectPlugin, etc.) is recovered and wrapped into this error.

Solutions

  1. Read the wrapped panic text (%v) to identify which injector panicked
  2. Ensure both p *KernelPlugin and rt *goja.Runtime are non-nil and the runtime is fresh/not interrupted
  3. Check lo.Must0-asserted calls inside the injection path for conditions that can fail
  4. Fail the plugin load on this error rather than reusing the runtime
  5. Review recent changes to siyuan API injectors for panicking code paths

Example fix

// before
rt := goja.New()
rt.Interrupt("timeout")
err := EnableSiyuanModule(p, rt) // panics inside -> wrapped error
// after
rt := goja.New()
err := EnableSiyuanModule(p, rt)
Defensive patterns

Strategy: try-catch

Validate before calling

if p == nil || rt == nil {
    return fmt.Errorf("EnableSiyuanModule requires non-nil plugin and runtime")
}

Try / catch

if err := EnableSiyuanModule(p, rt); err != nil {
    return fmt.Errorf("siyuan module injection failed for %s: %w", p.Name, err)
}

Prevention

When it happens

Trigger: Calling EnableSiyuanModule when one of the inject helpers (injectPlugin, event/network/etc. injections) panics — typically due to a nil runtime, nil KernelPlugin, a lo.Must0 assertion failing inside injection, or a runtime interrupted mid-injection.

Common situations: Plugin bootstrap against a runtime already killed by a script timeout; a nil *KernelPlugin pointer passed in; an injected API registration that hits a nil map or failed assertion inside lo.Must0.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/74e9f072cf23048f. Report an issue: GitHub.

Appendix: source

Thrown at kernel/plugin/sandbox.go:101

	registry := require.NewRegistry()

	registry.Enable(rt)
	registry.RegisterNativeModule(
		console.ModuleName,
		console.RequireWithPrinter(&Printer{name: p.Name}),
	)

	url.Enable(rt)
	buffer.Enable(rt)
	console.Enable(rt)
	return
}

// EnableSiyuanModule injects all siyuan.* APIs into the plugin's goja global context.
func EnableSiyuanModule(p *KernelPlugin, rt *goja.Runtime) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("failed to inject global context: %v", r)
		}
	}()

	siyuan := rt.NewObject()

	lo.Must0(injectPlugin(p, rt, siyuan))
	lo.Must0(injectEvent(p, rt, siyuan))
	lo.Must0(injectLogger(p, rt, siyuan))
	lo.Must0(injectStorage(p, rt, siyuan))
	lo.Must0(injectRpc(p, rt, siyuan))
	lo.Must0(injectAgent(p, rt, siyuan))
	lo.Must0(injectClient(p, rt, siyuan))
	lo.Must0(injectServer(p, rt, siyuan))
	lo.Must0(injectSecretsVars(p, rt, siyuan))

	lo.Must0(ObjectFreeze(rt, siyuan))

	lo.Must0(rt.GlobalObject().Set("siyuan", siyuan))

View on GitHub (pinned to 9f775e8a12)