← Back to PRs

#19996: fix(plugins): bind logger methods to preserve this context (#19988)

by lailoo open 2026-02-18 11:28 View on GitHub →
size: M experienced-contributor
## Summary - **Bug**: Plugin SDK `normalizeLogger` destroys tslog `this`-binding - **Root cause**: `normalizeLogger` in `src/plugins/registry.ts:465-470` destructures logger methods without `.bind()`, losing the `this` context - **Fix**: Add `.bind(logger)` to all logger method assignments Fixes #19988 ## Problem When `normalizeLogger` creates a new logger object by destructuring methods: ```typescript const normalizeLogger = (logger: PluginLogger): PluginLogger => ({ info: logger.info, // ← loses `this` warn: logger.warn, error: logger.error, debug: logger.debug, }); ``` The `this` binding is lost. Loggers like tslog that rely on internal state via `this` will throw or silently fail when `api.logger.*` is called inside plugins. **Before fix (on main branch):** ``` Input: api.logger.info("test") // with tslog logger Output: TypeError: Cannot read properties of undefined ``` ## Changes - `src/plugins/registry.ts` — Add `.bind(logger)` to preserve `this` context - `src/plugins/logger-this-binding.test.ts` — Add regression tests **After fix:** ``` Input: api.logger.info("test") Output: (logs correctly) ``` ## Test plan - [x] New test: simulated `this`-dependent logger class - [x] New test: real tslog `Logger` instance (`new Logger({ type: "hidden" })`) - [x] Both tests fail on main, pass on fix branch - [x] All 108 existing plugin tests pass - [x] Lint passes ## Effect on User Experience **Before:** Plugin authors using tslog see `api.logger.*` calls fail. They must implement workarounds (storing logger object, stderr fallback). **After:** `api.logger.*` works correctly with any logger implementation including tslog.

Most Similar PRs