adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.82k stars 637 forks source link

"@swc/core": "1.7.35" breaks dev server #4759

Open dashart-ke opened 3 days ago

dashart-ke commented 3 days ago

Package version

"@swc/core": "1.7.35"

Describe the bug

Hi, as already reported by different users in discord general channel: @swc/core v1.7.35 will break dev server.

After upgrade to 1.7.35 from 1.7.28, decorators like @inject will lead to strange errors.

Sample controller code:

import { inject } from '@adonisjs/core'
import type { HttpContext } from '@adonisjs/core/http'

@inject()
export default class DemoController {
  private readonly CLASS_NAME = 'DemoController';

  constructor() { }

  public async index({ }: HttpContext) {
    return 'Hello World 2'
  }
}

As soon as the controller will be loaded via method call, the dev server with HMR will crash with error.

console output

[20:05:32.571] ERROR (42696): Invalid or unexpected token
    request_id: "rt0bj6kg6cbwiatd3fb6s597"
    x-request-id: "rt0bj6kg6cbwiatd3fb6s597"
    err: {
      "type": "SyntaxError",
      "message": "Invalid or unexpected token",
      "stack":
          SyntaxError: Invalid or unexpected token
              at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:169:18)
              at callTranslator (node:internal/modules/esm/loader:272:14)
              at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:30)
              at async link (node:internal/modules/esm/module_job:78:21)
      "status": 500
    }

As soon as I remove "@inject()" from my controller, no crash occurs.

Workaround: Downgrade of @swc/core to 1.7.28.

Notice It is unclear if it is a bug of @swc/core nor adonisjs. I hope I selected the correct adonisjs repo.

Dev server started with node ace serve --hmr Error occurs without hmr to node ace serve

package.json

{
  "name": "haraka-in-handler",
  "version": "0.0.0",
  "private": true,
  "type": "module",
  "license": "UNLICENSED",
  "scripts": {
    "start": "node bin/server.js",
    "build": "node ace build",
    "dev": "node ace serve --hmr",
    "test": "node ace test",
    "lint": "eslint .",
    "format": "prettier --write .",
    "typecheck": "tsc --noEmit"
  },
  "imports": {
    "#controllers/*": "./app/controllers/*.js",
    "#exceptions/*": "./app/exceptions/*.js",
    "#models/*": "./app/models/*.js",
    "#mails/*": "./app/mails/*.js",
    "#services/*": "./app/services/*.js",
    "#listeners/*": "./app/listeners/*.js",
    "#events/*": "./app/events/*.js",
    "#middleware/*": "./app/middleware/*.js",
    "#validators/*": "./app/validators/*.js",
    "#providers/*": "./providers/*.js",
    "#policies/*": "./app/policies/*.js",
    "#abilities/*": "./app/abilities/*.js",
    "#database/*": "./database/*.js",
    "#start/*": "./start/*.js",
    "#tests/*": "./tests/*.js",
    "#config/*": "./config/*.js"
  },
  "devDependencies": {
    "@adonisjs/assembler": "^7.8.2",
    "@adonisjs/eslint-config": "^1.3.0",
    "@adonisjs/prettier-config": "^1.4.0",
    "@adonisjs/tsconfig": "^1.4.0",
    "@japa/api-client": "^2.0.3",
    "@japa/assert": "^3.0.0",
    "@japa/plugin-adonisjs": "^3.0.1",
    "@japa/runner": "^3.1.4",
    "@swc/core": "^1.7.35",
    "@types/amqplib": "^0.10.5",
    "@types/luxon": "^3.4.2",
    "@types/node": "^22.7.5",
    "eslint": "^8.0.0",
    "hot-hook": "^0.3.1",
    "pino-pretty": "^11.2.2",
    "prettier": "^3.3.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.3"
  },
  "dependencies": {
    "@adonisjs/auth": "^9.2.3",
    "@adonisjs/core": "^6.14.1",
    "@adonisjs/cors": "^2.2.1",
    "@adonisjs/lucid": "^21.3.0",
    "@vinejs/vine": "^2.1.0",
    "amqplib": "^0.10.4",
    "luxon": "^3.5.0",
    "mysql2": "^3.11.3",
    "reflect-metadata": "^0.2.2"
  },
  "hotHook": {
    "boundaries": [
      "./app/controllers/**/*.ts",
      "./app/middleware/*.ts"
    ]
  },
  "eslintConfig": {
    "extends": "@adonisjs/eslint-config/app"
  },
  "prettier": "@adonisjs/prettier-config"
}

Reproduction repo

No response

thetutlage commented 2 days ago

The issue comes directly from SWC and downgrading to version 1.7.26 or 1.7.28 works. We have updated all the starter kits to use version 1.7.26 by default.

However, for existing apps you can run the following command to pin the SWC version.

npm i -D @swc/core@1.7.26 --save-exact
thetutlage commented 18 hours ago

Reported. https://github.com/swc-project/swc/issues/9640

dashart-ke commented 2 hours ago

Thanks a lot for confirmation and opening an issue at swc project!