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

commitlint does not find plugin when a relative path is provided #932

Open
1 of 4 tasks
avinashbot opened this issue Feb 4, 2020 · 7 comments
Open
1 of 4 tasks
Labels

Comments

@avinashbot
Copy link

Expected Behavior

Currently, it seems that plugins can only be resolved if they're an npm package inside of the node_modules folder (node resolution logic). If I try to load a plugin using a relative path, I get this error:

Error: Failed to load plugin ./plugins/my-custom-plugin.js: Cannot find module 'commitlint-plugin-./plugins/my-custom-plugin.js'

Relative paths are supported for the extends and parserPreset keys, so making this work would align that behavior with plugins too.

Current Behavior

I would love to be able to write this and just have it work.

{
  plugins: ['./dir/my-custom-plugin.js'],
  ...
}

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Looking at how the parserPreset key is being loaded, it seems like it's just a matter of using resolveFrom before requireing the plugin.

Context

I basically want to have a small custom rule in a single file, but it seems like a hassle to either handle two separate packages or set up lerna or something.

Your Environment

Executable Version
commitlint --version 8.2.0
git --version git version 2.23.0
node --version v12.10.0
@marionebl
Copy link
Contributor

Yes, this should work if we expect plugins to be used.

@marionebl marionebl added the bug label Feb 6, 2020
@marionebl
Copy link
Contributor

marionebl commented Feb 6, 2020

As a workaround you can try passing down an absolute path, e.g.:

// commitlint.config.js
{
  plugins: [require.resolve('./dir/my-custom-plugin.js')],
}

@avinashbot
Copy link
Author

The absolute path doesn't work either, for the same reason (it's assuming that it's a module name and prepending commitlint-plugin- to the path):

Error: Failed to load plugin /home/avinash/test/commitlint/custom-plugin.js: Cannot find module 'commitlint-plugin-/home/avinash/test/commitlint/custom-plugin.js'

@marionebl
Copy link
Contributor

marionebl commented Feb 6, 2020

Ah thanks for pointing this out. I think this creates two bodies of work for us:

  1. resolve from the current config file for plugin paths starting with ./
  2. do not prefix for paths starting with ./ or /

@marionebl marionebl pinned this issue Feb 6, 2020
@mrmartan
Copy link

mrmartan commented Apr 6, 2020

I think I have run into this issue as well, albeit in different use case. I am building a tool to provide "conventional release" services to projects across company and most of them are not based on NPM.

So what I am doing is building a docker image to be used in CI.

package.json:

{
  "name": "conventional-release",
  "version": "0.0.0",
  "license": "MIT",
  "dependencies": {
    "semantic-release": "^17.0.4",
    "@semantic-release/git": "^9.0.0",
    "@semantic-release/changelog": "^5.0.1",
    "@semantic-release/gitlab": "^6.0.3",
    "conventional-changelog-conventionalcommits": "^4.2.3",
    "@commitlint/config-conventional": "^8.3.4",
    "@commitlint/cli": "^8.3.5"
  }
}

Dockerfile:

FROM docker:git

ENV NODE_VERSION 12.15.0
ENV YARN_VERSION 1.19.2

RUN apk add --no-cache nodejs npm yarn

WORKDIR /opt
ENV PATH="/opt/node_modules/.bin:${PATH}"

COPY package.json .
COPY yarn.lock .
RUN yarn --prod && \
    yarn autoclean --init && \
    yarn autoclean --force && \
    rm package.json yarn.lock .yarnclean

Now running this results in

$ npx commitlint --from=$CI_MERGE_REQUEST_TARGET_BRANCH_SHA
 /opt/node_modules/@commitlint/cli/lib/cli.js:124
 	throw err;
 	^
 Error: Cannot find module "@commitlint/config-conventional" from "/builds/dotnet-demo"
     at resolveId (/opt/node_modules/@commitlint/resolve-extends/lib/index.js:97:15)
     at resolveConfig (/opt/node_modules/@commitlint/resolve-extends/lib/index.js:80:24)
     at /opt/node_modules/@commitlint/resolve-extends/lib/index.js:37:24
     at Array.reduce (<anonymous>)
     at loadExtends (/opt/node_modules/@commitlint/resolve-extends/lib/index.js:35:38)
     at resolveExtends (/opt/node_modules/@commitlint/resolve-extends/lib/index.js:22:20)
     at Object.<anonymous> (/opt/node_modules/@commitlint/load/lib/index.js:65:44) {
   code: 'MODULE_NOT_FOUND'
 }

Note that semantic release modules work as expected.

@armano2
Copy link
Contributor

armano2 commented Jan 11, 2021

Ah thanks for pointing this out. I think this creates two bodies of work for us:

  1. resolve from the current config file for plugin paths starting with ./
  2. do not prefix for paths starting with ./ or /

thats not fully true, we should support also windows absolute paths eg. C:\DEV\....

@armano2
Copy link
Contributor

armano2 commented Jan 16, 2021

Currently there is a way to load local plugins by simply require'ing it

// commitlint.config.js
{
  plugins: [require('./dir/my-custom-plugin.js')],
}

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

Successfully merging a pull request may close this issue.

4 participants