Skip to content

fix(core): name dynamic host modules in import error messages - #17939

Merged
kamilmysliwiec merged 3 commits into
nestjs:masterfrom
Chaoqi31:fix/dynamic-module-error-name
Sep 30, 2026
Merged

kamilmysliwiec merged 3 commits into
nestjs:masterfrom
Chaoqi31:fix/dynamic-module-error-name

Conversation

@Chaoqi31

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

A module whose imports array contains undefined (usually a circular file import) or an invalid value makes the scanner throw UndefinedModuleException or InvalidModuleException. The message names the module that owns the broken imports array:

Nest cannot create the CatsModule instance.
The module at index [0] of the CatsModule "imports" array is undefined.

When that module is imported in its dynamic form (CatsModule.forRoot(), register(), any DynamicModule), the scanner passes the DynamicModule object as the parent. UNDEFINED_MODULE_MESSAGE and INVALID_MODULE_MESSAGE read parentModule?.name, which the object does not have, so the message becomes:

Nest cannot create the module instance.
The module at index [0] of the module "imports" array is undefined.

The name of the module to fix is gone, and the Scope [...] line lists only its ancestors.

The new specs against the current messages.ts:

 × UNDEFINED_MODULE_MESSAGE > should display the module name when the parent module is dynamic
 × INVALID_MODULE_MESSAGE > should display the module name when the parent module is dynamic
AssertionError: expected 'Nest cannot create the module instanc…' to be 'Nest cannot create the CatsModule ins…'
      Tests  2 failed | 25 passed (27)

Issue Number: N/A

What is the new behavior?

Both messages use the getInstanceName() helper that messages.ts already uses for the scope line. It resolves the class of a DynamicModule and of a forward reference, so the dynamic case prints the same message as the static one. 'module' stays as the fallback when no name can be found.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

npx vitest run packages/core: 1392 passed.

Chaoqi31 and others added 3 commits September 30, 2026 17:19
When a module imported in its dynamic form (`forRoot()`, `register()`)
has an `undefined` or invalid entry in its `imports`, the scanner passes
the `DynamicModule` object as the parent. `UNDEFINED_MODULE_MESSAGE`
and `INVALID_MODULE_MESSAGE` read `parentModule?.name`, which a plain
object does not have, so the message said "Nest cannot create the
module instance" and hid which module has the broken import. Use the
existing `getInstanceName()` helper, which already resolves dynamic
modules and forward references for the scope line.
@kamilmysliwiec
kamilmysliwiec merged commit 3ba6ccf into nestjs:master Sep 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants