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

Can't run commitlint on project with tsconfig.json #3256

Open
1 of 4 tasks
baby-gnu opened this issue Jun 27, 2022 · 19 comments
Open
1 of 4 tasks

Can't run commitlint on project with tsconfig.json #3256

baby-gnu opened this issue Jun 27, 2022 · 19 comments

Comments

@baby-gnu
Copy link

When running commitlint on a typeScript project, I have an error because commitlint use the project tsconfig.json.

Expected Behavior

commitlint should not use the project typeScript configuration.

Current Behavior

commitlint produce the following traceback:

/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:843
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
error TS6053: File '@loopback/build/config/tsconfig.common.json' not found.

    at createTSError (/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:843:12)
    at reportTSError (/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:847:19)
    at createFromPreloadedConfig (/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:858:36)
    at create (/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:613:10)
    at register (/usr/local/lib/node_modules/commitlint/node_modules/ts-node/src/index.ts:580:15)
    at TypeScriptLoader (/usr/local/lib/node_modules/commitlint/node_modules/cosmiconfig-typescript-loader/dist/loader.js:7:51)
    at loadConfig (/usr/local/lib/node_modules/commitlint/node_modules/@commitlint/load/src/utils/load-config.ts:35:27)
    at load (/usr/local/lib/node_modules/commitlint/node_modules/@commitlint/load/src/load.ts:25:33)
    at main (/usr/local/lib/node_modules/commitlint/node_modules/@commitlint/cli/src/cli.ts:199:27)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  diagnosticCodes: [ 6053 ]
}

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Steps to Reproduce (for bugs)

  1. clone the repository https://gitlab.mim-libre.fr/alphabet/laboite-blog-api.git
  2. execute commitlint --from HEAD~2 --to HEAD
commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'body-max-line-length': [2, 'always', 120],
    'footer-max-line-length': [2, 'always', 120],
    'header-max-length': [2, 'always', 72],
  },
};

Context

We upgraded our commitlint container for our CI and now all typeScript project are failling.

Your Environment

Executable Version
commitlint --version 16.3.0
git --version 2.36.1
node --version 18.4.0
@baby-gnu
Copy link
Author

I tested with latest @commitlint/cli@17.0.3 to be sure and I have the same problem.

Regards.

@escapedcat
Copy link
Member

escapedcat commented Jun 27, 2022

Pity, looks like it's somehow picking this up?: https://gitlab.mim-libre.fr/alphabet/laboite-blog-api/-/blob/dev/tsconfig.json#L3
Never had an issue with other projects using tsconfig.

Not sure when we can get to this.

@baby-gnu
Copy link
Author

Pity, looks like it's somehow picking this up?: https://gitlab.mim-libre.fr/alphabet/laboite-blog-api/-/blob/dev/tsconfig.json#L3 Never had an issue with other projects using tsconfig.

It looks like yes, I'm not sure it's a good idea for commitlint to load the project configuration but I don't know how to prevent that.

@baby-gnu
Copy link
Author

It looks like yes, I'm not sure it's a good idea for commitlint to load the project configuration but I don't know how to prevent that.

I managed to work around this issue by cd /tmp and execute commitlint --cwd /my/sources

@stefanfrede

This comment was marked as duplicate.

@silvia-YQY

This comment was marked as duplicate.

@jinmingpang

This comment was marked as duplicate.

jrolfs added a commit to hoverinc/hover-javascript that referenced this issue Feb 27, 2023
…onfiguration

**@commitlint/cli** added support for TypeScript-based configurations via `ts-node`, and that has caused a good deal of grief:

conventional-changelog/commitlint#3420
conventional-changelog/commitlint#3351
conventional-changelog/commitlint#3218
conventional-changelog/commitlint#3256

hoverinc/web-react#1792

Both because it's a heavy-ass dependency and because it requires configuration to
work properly. It seems like project-local configuration can affect it's invocation
of ts-node, which happens even JavaScript configuration files now?... lame.

Anyways, this ensures we avoid any type-checking that happens in it's ts-node
invocation until we get a better solution upstream (or contribute one). Ideally,
installing ts-node would be opt-in etc.
@soullivaneuh
Copy link

soullivaneuh commented Mar 8, 2023

I got a similar issue running commitlint on a GitLab CI job.

Here is the Dockerfile of my commitlint command:

FROM node:18.14-alpine
RUN apk add --no-cache git~=2 \
&& npm install --global @commitlint/cli@17.4.4 @commitlint/config-conventional@17.4.4
# Contains only commitlint.config.js
COPY rootfs /
USER node
ENTRYPOINT [ "commitlint", "--strict", "--config", "/commitlint.config.js" ]

The related CI job:

release:commitlint:
  stage: release
  needs: []
  image:
    name: registry.gitlab.com/company/images/commitlint
    entrypoint: ['']
  script:
    - git config --global --add safe.directory ${CI_PROJECT_DIR}
    - git fetch --quiet origin ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
    - npx commitlint --version
    - npx commitlint --strict --config /commitlint.config.js --from=origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  allow_failure:
    exit_codes: 2

Works on any of my project excepts the one using tsonfig.json, ending to a similar error.

My workaround was to add a rm -f tsconfig.json just before the commitlint command.

@stefanfrede
Copy link

stefanfrede commented Mar 9, 2023

I managed to work around this issue by cd /tmp and execute commitlint --cwd /my/sources

@baby-gnu I try to can't wrap my head around how this should work 🤔

@ccamel
Copy link

ccamel commented Apr 26, 2023

🤔 It seems that we suffer from the same problem for the okp4/team-wiki project. The temporary solution was to delete the tsconfig.json file while linting the github action.

@TimeCoder
Copy link

Just check the encoding of index.ts.
In my case, somehow it was UTF-16. Once I changed it to UTF-8 (in Notepad++) - the problem is gone.

@Fierce-Cat
Copy link

Just check the encoding of index.ts. In my case, somehow it was UTF-16. Once I changed it to UTF-8 (in Notepad++) - the problem is gone.

I'm getting the same UTF-16 file after following docs https://commitlint.js.org/#/guides-local-setup?id=install-commitlint with command

# Configure commitlint to use conventional config
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js 

Error log:

D:\dev\Project\node_modules\.pnpm\ts-node@10.9.1_@types+node@18.16.19_typescript@5.1.6\node_modules\ts-node\src\index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
commitlint.config.js(1,1): error TS1490: File appears to be binary.

The problem is solved after changing the file encoding to UTF-8 manually.

@DavidWesley
Copy link

DavidWesley commented Jul 30, 2023

Just check the encoding of index.ts. In my case, somehow it was UTF-16. Once I changed it to UTF-8 (in Notepad++) - the problem is gone.

@TimeCoder Can you be a little more specific? Which index.ts file has this problem? I badly need to resolve this conflict

My error message still is:

/home/dwesley/www/node-config-template/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

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

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

@shlroland
Copy link

Just check the encoding of index.ts. In my case, somehow it was UTF-16. Once I changed it to UTF-8 (in Notepad++) - the problem is gone.

@TimeCoder Can you be a little more specific? Which index.ts file has this problem? I badly need to resolve this conflict

My error message still is:

/home/dwesley/www/node-config-template/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

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

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

When I use the latest array values in my tsconfig/extends, I encounter this error. However, when I change it to a string, the error disappears. Therefore, I believe it's an issue with ts-node not supporting parsing of the latest ts configuration. Perhaps upgrading ts-node version could resolve this problem?

@escapedcat
Copy link
Member

Perhaps upgrading ts-node version could resolve this problem?

According to the lock-file commitlint is on the latest version?:

ts-node@^10.8.1:
  version "10.9.1"

@shlroland
Copy link

Perhaps upgrading ts-node version could resolve this problem?

According to the lock-file commitlint is on the latest version?:

ts-node@^10.8.1:
  version "10.9.1"

Unfortunately, ts-node did not solve this problem.TypeStrong/ts-node#2000
ts-node has not had a new release for a year, and its support for TypeScript 5.0 seems to be quite poor. Perhaps it's time to migrate to other libraries, such as @swc/register or sucrase

@escapedcat
Copy link
Member

Perhaps it's time to migrate to other libraries [...]

Sounds like a good idea. You up for a PR?

@escapedcat
Copy link
Member

Reopening because of #3641
This will then hopefully be fixed with a new mayor version.
yolo

@escapedcat escapedcat reopened this Aug 10, 2023
@brc-dd
Copy link
Contributor

brc-dd commented Oct 14, 2023

Another issue here is why TypeScriptLoader is being init in first place when loading the config does not require it. Most of us are using JS/JSON/YAML based configs. It would probably be better to lazy load that loader here:

const tsLoader = TypeScriptLoader();

All internal loaders of cosmiconfig are already lazily loaded. Refer this commit - cosmiconfig/cosmiconfig@08ebcd5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests