Repository: nestjs/nest

Issue #4872: improve di circular dependency error, possible solutions
Created: 2020-06-02T16:01:07Z
Bug Report
Current behavior
From the docs:

A forward reference allows Nest to reference classes which aren't yet defined using the forwardRef() utility function. For example, if CatsService and CommonService depend on each other, both sides of the relationship can use @Inject() and the forwardRef() utility to resolve the circular dependency. Otherwise Nest won't instantiate them because all of the essential metadata won't be available.

If you set up a circular dependency without forward refs then you get a warning:
[Nest] 30635   - 06/02/2020, 11:57:14 AM   [ExceptionHandler] Nest can't resolve dependencies of the CatsService (?). Please make sure that the argument dependency at index [0] is available in the CommonModule context.

Potential solutions:
- If dependency is a provider, is it part of the current CommonModule?
- If dependency is exported from a separate @Module, is that module imported within CommonModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

Expected behavior
It would be nice to see that there is a circular dependency in potential solutions list pointing to using forward refs (or discouraging circular deps if desired).
Possible Solution

Environment

Nest version: 6.10.12

 
For Tooling issues:
- Node version: v11.15.0
- Platform:  Mac

Others:

Duplicate issue #5037: Improve circular dependency detection between services
Created: 2020-07-06T15:27:53Z
Bug Report
Current behavior

This weekend I was faced with an wrong error message, which made me miss long hours:
[ExceptionHandler] Nest can't resolve dependencies of the PaymentsService (TransactionsService, ?). Please make sure that the argument dependency at index [1] is available in the PaymentsModule context.

Potential solutions:
- If dependency is a provider, is it part of the current PaymentsModule?
- If dependency is exported from a separate @Module, is that module imported within PaymentsModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })
 +79ms
Error: Nest can't resolve dependencies of the PaymentsService (TransactionsService, ?). Please make sure that the argument dependency at index [1] is available in the PaymentsModule context.

Potential solutions:
- If dependency is a provider, is it part of the current PaymentsModule?
- If dependency is exported from a separate @Module, is that module imported within PaymentsModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })
This message is commonly used for not declaring service classes in the modules providers section. And that was where I was looking for the solution. As the project is large, the difficulty has increased.
After many hours, I found the problem: Circular dependency between services. The biggest problem is that for modules Nest is able to identify the circular dependency and immediately we can fix the bug.
Nest cannot create the PaymentsModule instance.
The module at index [1] of the PaymentsModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
Input Code

@Injectable()
export class PaymentsService {
	constructor(
		private transactionsService: TransactionsService,
		private jobsService: JobsService,
	) {}
@Injectable()
export class JobsService extends TypeOrmCrudService<Job> {
	constructor(
		@InjectRepository(Job)
		public repo: Repository<Job>,
		private paymentsService: PaymentsService,
	) {
		super(repo);
	}
Solution
@Injectable()
export class PaymentsService {
	constructor(
		private transactionsService: TransactionsService,
		@Inject(forwardRef(() => JobsService))
		private jobsService: JobsService,
	) {}
Expected behavior
Identify circular dependencies between service class and display the right message to the user
Environment

Nest version: 7.2.0

 
For Tooling issues:
- Node version: 12.13.1 
- Platform:  ArchLinux

Closing comment (2020-07-08T11:57:06Z): Duplicate of #4872