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.

Self-injection causes the app to get stuck in the initializing process without error

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When an Injectable tries to inject itself in the constructor, the initializing process hangs without showing any error.

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

@Injectable()
export class AppService {
  constructor(private readonly appService: AppService) {}
  getHello(): string {
    return 'Hello World!';
  }
}

App’s logs:

[12:49:52 PM] File change detected. Starting incremental compilation...
api_1       | 
api_1       | [12:49:53 PM] Found 0 errors. Watching for file changes.
api_1       | 
api_1       | For help, see: https://nodejs.org/en/docs/inspector
api_1       | [Nest] 182  - 01/11/2022, 12:49:54 PM     LOG [NestFactory] Starting Nest application...
api_1       | [Nest] 182  - 01/11/2022, 12:49:54 PM     LOG [InstanceLoader] PrismaModule dependencies initialized +119ms
api_1       | [Nest] 182  - 01/11/2022, 12:49:54 PM     LOG [InstanceLoader] UsersModule dependencies initialized +2ms

Minimum reproduction code

https://codesandbox.io/s/friendly-lalande-ni9oq

Steps to reproduce

No response

Expected behavior

Nest should throw an error describing that the provider cannot inject itself.

Package

Other package

No response

NestJS version

8.1.9

Packages versions

{
  "name": "server",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug 0.0.0.0:9229 --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^8.0.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^8.0.0",
    "@prisma/client": "^3.7.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "27.0.2",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.2.5",
    "prettier": "^2.3.2",
    "prisma": "^3.7.0",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

v16.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jmcdo29commented, Jan 11, 2022

I was able to replicate this. I’ll try to figure out where the exit is happening so that it can be properly reported

1reaction
micaleviskcommented, Feb 23, 2022

for reference: using another token but the same class will lead to this issue as well.

class AppService {
  constructor(@Inject('Foo') appService: any) {}
}

@Module({
  providers: [
   { provide: 'Foo', useClass: AppService }
  ],
})
class AppModule {}
Read more comments on GitHub >

github_iconTop Results From Across the Web

App startup time - Android Developers
Your app may waste time during startup if your Application subclasses perform initializations that don't need to be done yet. Some ...
Read more >
HTTP Error 500.30 - ANCM In-Process Start Failure
Go to Azure Portal > your App Service > under development tools open console. We can run the application through this console and...
Read more >
ASP.NET Core IIS InProcess Hosting Issue in .NET Core 3.1
NET Core 3.1 application where InProcess hosting was not working. ... The cause of a process startup failure can usually be determined from ......
Read more >
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
NoClassDefFoundError due to the failure of static initialization is quite ... to troubleshoot the root cause of NoClassDefFoundError in Java application.
Read more >
3 Known Issues and Workarounds - Oracle Help Center
Creating Managed Server Domain from a Template Causes Error ... the Managed Server will fail to boot because it does not have the...
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