conventional-changelog / commitlint

📓 Lint commit messages
https://commitlint.js.org
MIT License
16.77k stars 903 forks source link

fix: SyntaxError: Invalid or unexpected token. commit-msg run error. #3588

Open rangersss opened 1 year ago

rangersss commented 1 year ago

Expected Behavior

PS E:\all_project\original-detection> git commit -m 'test commit-msg' → No staged files match any configured task. E:\all_project\original-detection\node_modules.pnpm\@commitlint+read@17.5.1\node_modules\@commitlint\read\lib\read.js:1

SyntaxError: Invalid or unexpected token at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1176:20) at Module._compile (node:internal/modules/cjs/loader:1218:27) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object. (E:\all_project\original-detection\node_modules.pnpm\@commitlint+cli@17.6.1\node_modules\@commitlint\cli\lib\cli.js:16:32) at Module._compile (node:internal/modules/cjs/loader:1254:14)

Node.js v18.16.0 husky - commit-msg hook exited with code 1 (error)

Current Behavior

No response

Affected packages

Possible Solution

No response

Steps to Reproduce

1. pnpm add @commitlint/config-conventional @commitlint/cli husky
2. echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
3. npx husky install
4. npx husky add .husky/commit-msg  'npx --no -- commitlint --edit ${1}'
5. git add .
6. git commit -m 'test pre-commit'

Context

verify commit message

commitlint --version

"@commitlint/cli": "^17.6.1"

git --version

git version 2.37.3.windows.1

node --version

v18.16.0

escapedcat commented 1 year ago

Tried with node v18.15.0 on OSX

echo "test pre-commit" | npx commitlint
⧗   input: test pre-commit
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

Did you search the existing issues already? Might be one of these:

DavidWesley commented 1 year ago

I got the same bug. Any one else solve this bug?

DavidWesley commented 1 year ago
I just figured out what was causing this error in my context.

Problem

After hours of reading documentation for errors in husky, lint-staged and commitlint, none of them worked.

I found that what was preventing the commitlint cli from working was the my tsconfig file.

Configuration file

// tsconfig.json
{
  "extends": [
    "@tsconfig/node12"
  ],
  "compilerOptions": {
    // configs ...
  }
}
Prompt
dwesley@DESKTOP-R4V89G6:~/www/new-beecrowd$ git commit -m "TEST"
→ No staged files match any configured task.
--- COMMIT-MSG HOOK ---
/home/dwesley/www/new-beecrowd/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

TypeError: value.replace is not a function
    at normalizeSlashes (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/util.js:62:18)
    at Object.getExtendsConfigPath (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/ts-internals.js:24:54)
    at readConfig (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/configuration.js:127:64)
    at findAndReadConfig (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/configuration.js:50:84)
    at create (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/index.js:146:69)
    at register (/home/dwesley/www/new-beecrowd/node_modules/ts-node/dist/index.js:127:19)
    at TypeScriptLoader (/home/dwesley/www/new-beecrowd/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js:52:54)
    at loadConfig (/home/dwesley/www/new-beecrowd/node_modules/@commitlint/load/lib/utils/load-config.js:12:75)
    at load (/home/dwesley/www/new-beecrowd/node_modules/@commitlint/load/lib/load.js:19:55)
    at main (/home/dwesley/www/new-beecrowd/node_modules/@commitlint/cli/lib/cli.js:194:45)

Node.js v18.15.0
husky - commit-msg hook exited with code 1 (error)

Solution

By removing this specific setting, everything went back to normal.

Configuration file

// tsconfig.json
{
  "extends": [
    // "@tsconfig/node12"
  ],
  "compilerOptions": {
    // configs ...
  }
}

Prompt

dwesley@DESKTOP-R4V89G6:~/www/new-beecrowd$ git commit -m "TEST"
→ No staged files match any configured task.
--- COMMIT-MSG HOOK ---
⧗   input: TEST
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)

Suggestion

I believe that, somehow, my project settings interfered with the execution of the lib. Ideally, this shouldn't happen. I believe this error is a point that the lib should resolve permanently.

escapedcat commented 1 year ago

@DavidWesley your issue sounds like #3256
@rangersss issue could be something else. Not sure.

rangersss commented 1 year ago

@DavidWesley your issue sounds like #3256 @rangersss issue could be something else. Not sure.

The problem is the npm package

escapedcat commented 1 year ago

Sure, but is it the same as #3256 ?

rangersss commented 1 year ago

Sure, but is it the same as #3256 ?

Yes, because there may be some problem with the package when downloading with pnpm before, and then there will be no problem after re-downloading the package with yarn!

escapedcat commented 1 year ago

Sorry, I do not understand.

3256 is about a conflicting tsconfig.

If your issue is about pnpm vs. yarn this is a different issue.

prisis commented 1 year ago

It looks like a problem with typescript v5 and https://github.com/TypeStrong/ts-node/pull/1958, ts-node is currently not supporting extends as array

escapedcat commented 1 year ago

~Please see if the latest release fixes this: https://github.com/conventional-changelog/commitlint/releases/tag/v17.7.0~

See #3641 This will then hopefully be fixed with a new mayor version. yolo