Skip to content

Nested TRANSIENT providers not isolated in STATIC context when multiple DEFAULT parents inject same chain #16257

@mag123c

Description

@mag123c

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When multiple DEFAULT scoped providers inject the same TRANSIENT → TRANSIENT chain, the nested TRANSIENT instances are shared instead of being isolated.

TestController (DEFAULT) → TransientLogger [1] → NestedTransientService [1]
TestService (DEFAULT) → TransientLogger [2] → NestedTransientService [1] ← Reused!

Each TransientLogger should receive its own NestedTransientService instance, but they share the same instance.

The root cause is the contextId !== STATIC_CONTEXT condition in getEffectiveInquirer()

// packages/core/injector/injector.ts:977-988
private getEffectiveInquirer(...) {
  return dependency?.isTransient &&
    inquirer?.isTransient &&
    parentInquirer &&
    contextId !== STATIC_CONTEXT  // ← This excludes STATIC context
    ? parentInquirer
    : inquirer;
}

Minimum reproduction code

https://github.com/stineslenea/logger-transient-example

Steps to reproduce

  1. Clone https://github.com/stineslenea/logger-transient-example
  2. Update @nestjs/* packages to 11.1.12
  3. Remove line 12 in src/test.service.ts: @Inject(REQUEST) private readonly request: any,
  4. Run npm start
  5. Call GET /test
  6. Check logs: NestedTransientService instance is shared between Controller and Service

Expected behavior

Each TransientLogger should receive its own NestedTransientService instance

TestController (DEFAULT) → TransientLogger [1] → NestedTransientService [1]
TestService (DEFAULT) → TransientLogger [2] → NestedTransientService [2] ← Expected

NestJS version

11.1.12

Packages versions

{
  "@nestjs/common": "11.1.12",
  "@nestjs/core": "11.1.12",
  "@nestjs/platform-express": "11.1.12"
}

Node.js version

22.x

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Related Issues/PRs

This is the missing piece. TRANSIENT isolation in STATIC context.

The fix in #15815 explicitly excludes STATIC context with contextId !== STATIC_CONTEXT.
This condition needs to be revisited to also handle STATIC context isolation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions