Decorator undefined in sibling Fastify plugin after register()
0 reputation · 08 Mar 2022, 15:14 UTC
Goal
Make a decorator added by one Fastify plugin visible to a sibling plugin registered later, without forcing the provider into the parent scope.
Constraints
Fastify evaluates encapsulate at registration time. With encapsulation enabled, decorators remain isolated inside the child plugin and are undefined in siblings. With encapsulation disabled, the decorator leaks into the parent instance. The fastify-plugin wrapper bypasses encapsulation entirely. autoload registers files in lexicographic order and provides no declarative dependency mechanism, so file additions or renames can change which decorator loads first.
Assuming Fastify v4 or v5, which registration pattern keeps the decorator scoped while still allowing a later consumer plugin to read it?
- Does encapsulate: false on the provider guarantee visibility to subsequently registered siblings?
- Can a consumer read a decorator from an encapsulated provider without fastify-plugin?
- Does autoload filesystem sort order make this behavior non-repeatable across operating systems?