nestjs / nest-cli

CLI tool for Nest applications 🍹

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SWC + watch + typecheck doesn't wait for typechecking to finish

noahw3 opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I'm currently converting a nestjs service from the default compiler to swc. I've managed to fix most of the issues in the codebase, but the final behavior that I'm experiencing is that when running in watch mode, swc and the tsc compiler run in parallel. This has a couple of undesirable effects:

  1. If type checking fails, the service will likely still start. This masks the errors, particularly if the service has a bunch of startup logs.
  2. Because the type checking step with plugins writes a metadata.ts file, if the swc and service startup step finishes first (which, given the speed difference between tsc and swc basically always occurs), watch mode will then notice that the metadata file has been written/changed and a loop of watch-driven restarts will occur.

This notably does not occur if not using watch mode: the swc step waits for the type checking to finish.

I dug into the code and it appears like there's a missing await that's the culprit. Before putting up a PR, I wanted to validate that this is not intended behavior.

Minimum reproduction code

n/a

Steps to reproduce

Left the above blank, any nestjs swc project should manifest this behavior.

nest-cli.json

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "builder": "swc",
    "typeCheck": true,
    "plugins": ["@nestjs/swagger"]
  }
}
  1. Run nest build
  2. Notice that it runs tsc, and once tsc finishes runs swc
  3. Run nest build --watch
  4. Notice that swc will run first (or at least in parallel), and likely complete before tsc finishes running

Expected behavior

When using watch mode + type checking, the swc compiler will wait for the tsc step to successfully finish before running.

Package version

10.1.10

NestJS version

10.1.2

Node.js version

18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

That's an expected behavior. If we decided to always wait for type checking, then using swc wouldn't make much sense. It's a tradeoff between speed and correctness - with swc, type-checker is forked and is supposed to run in parallel. If you want to wait for type checking, you can use tsc instead.