Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch mode loops endlessly #11038

Closed
3 of 15 tasks
Hareloo opened this issue Feb 3, 2023 · 15 comments
Closed
3 of 15 tasks

Watch mode loops endlessly #11038

Hareloo opened this issue Feb 3, 2023 · 15 comments
Labels
needs triage This issue has not been looked into

Comments

@Hareloo
Copy link

Hareloo commented Feb 3, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Hello!
I am experiencing a very weird bug:
In my personal home computer, I can create a NestJS project using the CLI and running with npm run start:dev with no problem.
In my laptop however, the exact same thing doesn't work - again, I am creating a default project with the NestJS cli but when I start it with npm run start:dev it loops endless with:

10:40:27 AM - File change detected. Starting incremental compilation...
10:40:27 AM - Found 0 errors. Watching for file changes.

I also tried to use tsc --watch and that does work on my laptop as well...
I don't even know where to begin debugging something like this, I'm talking about the default project and I'm using the same node, npm and typescript versions on both computers...
Would appreciate some help if possible

Minimum reproduction code

Default project

Steps to reproduce

No response

Expected behavior

Watch for file changes and only re-compile when those are detected.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

{
  "name": "test-proj",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/*/.ts\" \"test/*/.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --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": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@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": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "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

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@Hareloo Hareloo added the needs triage This issue has not been looked into label Feb 3, 2023
@micalevisk
Copy link
Member

Please provide a minimum reproduction repository. You can start from this minimal project.

why reproductions are required

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

Please provide a minimum reproduction repository. You can start from this minimal project.

why reproductions are required

Well that's part of the problem - you probably wouldn't be able to reproduce this.
It only happens on some of my work laptops...
I just want to know if other people encountered this and how I could possibly find the issue...

@jmcdo29
Copy link
Member

jmcdo29 commented Feb 3, 2023

If I had to guess, this would be a windows specific thing with typescript 4.9 due to how watched files are computed. I've got no clue otherwise though. Especially cause you said tsc --watch (that's all we end up calling) doesn't endlessly run

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

If I had to guess, this would be a windows specific thing with typescript 4.9 due to how watched files are computed. I've got no clue otherwise though. Especially cause you said tsc --watch (that's all we end up calling) doesn't endlessly run

Well there's got to be more to it than tsc --watch because tsc --watch doesn't actually start the project for me, just compiles the files..

@jmcdo29
Copy link
Member

jmcdo29 commented Feb 3, 2023

Well there's got to be more to it than tsc --watch because tsc --watch doesn't actually start the project for me, just compiles the files..

You're right, we call node dist/main after compilation completes. tsc --watch is what would be an issue here though because you are staying in the compilation loop. Unless you've modified the base nest new project to create some file that tsc is aware of on startup which would cause this, the start up of the server shouldn't be causing tsc to retrigger

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

Well there's got to be more to it than tsc --watch because tsc --watch doesn't actually start the project for me, just compiles the files..

You're right, we call node dist/main after compilation completes. tsc --watch is what would be an issue here though because you are staying in the compilation loop. Unless you've modified the base nest new project to create some file that tsc is aware of on startup which would cause this, the start up of the server shouldn't be causing tsc to retrigger

Is there a way (or maybe this already happens) to make nest start --watch print the detected file that changed to the console or a log file or something? Maybe then I would have some clue as to why this is happenning...

@jmcdo29
Copy link
Member

jmcdo29 commented Feb 3, 2023

Is there a way (or maybe this already happens) to make nest start --watch print the detected file that changed to the console or a log file or something? Maybe then I would have some clue as to why this is happenning...

Again, it's typescript that sees the changed file. Nest just starts up the tsc --watch action programmatically. We don't watch for the file ourselves

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

Is there a way (or maybe this already happens) to make nest start --watch print the detected file that changed to the console or a log file or something? Maybe then I would have some clue as to why this is happenning...

Again, it's typescript that sees the changed file. Nest just starts up the tsc --watch action programmatically. We don't watch for the file ourselves

I understand...
Do you have any suggestions how I could possibly debug this problem?

@micalevisk
Copy link
Member

micalevisk commented Feb 3, 2023

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

@Hareloo read this: https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#file-watching-now-uses-file-system-events

Mm interesting... But if it's a version thing how is it possible that it works on one machine but not on the other one even though they are using the same version?

@micalevisk
Copy link
Member

micalevisk commented Feb 3, 2023

yes, OS works like that, you know

@micalevisk
Copy link
Member

I suggest you to play with watchOptions: https://www.typescriptlang.org/docs/handbook/configuring-watch.html

@Hareloo
Copy link
Author

Hareloo commented Feb 3, 2023

I suggest you to play with watchOptions: https://www.typescriptlang.org/docs/handbook/configuring-watch.html

Thank you so much, this solved my problem, I was looking for a solution for weeks now.
All I needed to do is change watchFile under watchOptions to use polling instead of fsEvents and it works like a charm.

@micalevisk
Copy link
Member

micalevisk commented Feb 3, 2023

@Hareloo could you please add a new section at https://docs.nestjs.com/faq/common-errors to guide windows users that are using typescript@^4.9 towards that solution?

That's not the first time I saw people complaining with nest start --watch right after upgrading TS to 4.9 on windows

@duoduoObama
Copy link

duoduoObama commented May 8, 2023

hi @Hareloo ,May I ask what configuration did you modify to solve this problem? I have been troubled for a long time, but I have read your docs and found no solution steps.

Here is my modified tsconfig.json configuration
"watchOptions": { "watchFile": "fixedPollingInterval", "watchDirectory": "useFsEvents", "fallbackPolling": "dynamicPriority", "synchronousWatchDirectory": true, "excludeDirectories": ["**/node_modules", "dist"] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants