Kest

Search

Search the documentation

Fundamentals

Circular dependency

Two classes that need each other.

Constructor injection builds a class by first building what it needs. If A needs B and B needs A, neither can be built first. This is a circular dependency.

Loading diagram…

Kest finds it for you: the processor reports it at build time, naming the path:

Circular constructor dependency: OrderService -> InvoiceService -> OrderService. Break it with `by inject()` on one side.

(If it is hidden behind a dynamic provider it is reported at start-up, with the same path.)

First: is the design wrong?

Most cycles are a sign that a third class is hiding. If orders and invoices both need to "calculate the total", move that logic into its own provider that both depend on:

Loading diagram…

No cycle, and each class does one thing. Try this first.

When the cycle is real: three ways to break it

Sometimes two classes really do call each other. Break the cycle on one side with something lazy, resolved the first time it is used instead of when the class is built. Kest has the same tools NestJS has, and the build still checks that the other class exists and is visible, so you keep the build-time safety.

1. ForwardRef<T> (NestJS @Inject(forwardRef(() => B)))

Declare the constructor parameter as ForwardRef<InvoiceService>:

@Injectable
class OrderService(private val invoices: ForwardRef<InvoiceService>) {
    fun total(id: String) = invoices.get().forOrder(id)           // resolved on the first call
}
 
@Injectable
class InvoiceService(private val orders: OrderService)            // a normal dependency: OrderService -> InvoiceService -> OrderService

The processor allows this cycle because one edge is a ForwardRef, and the container hands OrderService a handle that looks up InvoiceService when get() is first called (once; later calls reuse it). You can also use the handle as a property delegate:

@Injectable
class OrderService(private val invoicesRef: ForwardRef<InvoiceService>) {
    private val invoices by invoicesRef                           // reads like an ordinary property
    fun total(id: String) = invoices.forOrder(id)
}

2. by inject() (a property instead of a parameter)

@Injectable
class InvoiceService {
    private val orders: OrderService by inject()          // resolved when first read, not when constructed
    fun forOrder(id: String) = ... orders.find(id) ...
}

Same effect, written as a property of the class that does not take the other in its constructor. Use whichever reads better; ForwardRef keeps the dependency visible in the constructor.

3. The container (NestJS ModuleRef)

Inject Container and ask for the provider when you need it: container.get(OrderService::class) (Module reference). It is the most flexible and the least checked: nothing verifies at build time that the provider exists.

One rule for all three: do not use the other side while constructing (in init or a property initialiser): it may not exist yet. Use it inside a method.

Cycles between modules (NestJS forwardRef in imports)

Two modules that import each other are refused at build time (Circular module import: A -> B -> A). The best fix is to extract what they share into a third module that both import. When the modules genuinely belong together, mark one side with forwardImports:

@Module(imports = [BillingModule::class], providers = [OrderService::class], exports = [OrderService::class])
class OrdersModule
 
@Module(providers = [InvoiceService::class], exports = [InvoiceService::class],
        forwardImports = [OrdersModule::class])             // BillingModule uses OrdersModule's exports, although OrdersModule imports BillingModule
class BillingModule

A forward import behaves like a normal import (the other module's exports are visible, and the module is loaded), but it is not counted when the build looks for circular imports, and the generated code reads it lazily so the two descriptors can refer to each other. Only the module cycle is allowed: the providers inside must not form a constructor cycle (use ForwardRef<T> above for that).

Read more