question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error thrown on `moduleRef.resolve()` is a breaking change

See original GitHub issue

Bug Report

Current behavior

Calling moduleRef.resolve() on non-transient providers throws an error when it did not before 7.1.2.

The relevant commit is:

https://github.com/nestjs/nest/commit/439736f051384492813bed469682a48f3c8935e0

Input Code

import { Injectable } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';

import { MyClass } from './MyClass';

@Injectable()
export class Foo {
  constructor(private readonly moduleRef: ModuleRef) {}

  async build(): Promise<MyClass> {
    return await this.moduleRef.resolve(MyClass);
  }
}

Expected behavior

I understand that resolve should only be called for transient-scoped providers, but before this update, it did not throw an error when not doing so. This is a breaking change introduced in a patch version. I have full coverage in my repos, so I was able to catch this, but for anyone who is relying on Nest not to release breaking changes in minor updates, this will be a problem.

Possible Solution

Revert the change. Re-introduce it in a major version update.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Jun 18, 2020

Since there’s no easy way to dynamically determine the scope of a provider, I’ve reverted this breaking change for now (+ error message was misleading either way 😅 ). Fixed in the latest release!

1reaction
janhartigancommented, Jun 8, 2020

@maximelkin The reasoning behind this makes sense given how Nest’s injector works behind the scenes. The idea is that resolve is intended to be used to force-resolve a dependency chain that has some non-global-scoped providers in it. In the Nest world, these kinds of providers don’t get instantiated until the a request comes in, so it makes sense that you might need an async method to do the work. get is intended to be used for globally-scoped providers.

I think a case could be made for having a method available that does either/or…it’s not always easy to know when a particular provider is request scoped or not (although frequently this is a red flag for abusing the provider scopes).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module reference | NestJS - A progressive Node.js framework
The ModuleRef instance (hereafter we'll refer to it as the module reference) has a get() method. This method retrieves a provider, controller, or...
Read more >
Getting started with continuous integration for Nest.js APIs
Nest.js recommends breaking a large application into multiple modules. It helps to maintain the structure of the application.
Read more >
if httpservice is a provider, is it part of the current roottestmodule
If run with Node version 15.0.1 (latest) or 12.19.0 (latest), the following error is thrown when executing the test: Error: Nest can't resolve...
Read more >
Unit Testing and Integration Testing Nestjs Application
Lets break this down first we are getting TestingModule by calling ... beforeEach(async () => { const module: TestingModule = await Test.
Read more >
History - setuptools 65.6.3.post20221220 documentation
No significant changes. ... #3693: Merge pypa/distutils@3e9d47e with compatibility fix for ... Errors might be raised in future versions of setuptools .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found