conventional-changelog / commitlint

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

[@commitlint/prompt] get error use '@commitlint/prompt' with commitizen #2486

Closed Liaoct closed 2 years ago

Liaoct commented 3 years ago

As title.

Expected Behavior

Current Behavior

i got Could not find prompter method in the provided adapter module: @commitlint/prompt

Affected packages

Possible Solution

Maybe this code as below at commitizen/adapter.js, got a error:

function getPrompter(adapterPath) {
  cov_8emx7ww64.f[7]++;
  // Resolve the adapter path
  let resolvedAdapterPath = (cov_8emx7ww64.s[25]++, resolveAdapterPath(adapterPath)); // Load the adapter

  let adapter = (cov_8emx7ww64.s[26]++, require(resolvedAdapterPath));
  /* istanbul ignore next */

  if (adapter && adapter.prompter && (0, _util.isFunction)(adapter.prompter)) {
    return adapter.prompter;
  } else if (adapter && adapter.default && adapter.default.prompter && (0, _util.isFunction)(adapter.default.prompter)) {
    return adapter.default.prompter;
  } else {
    throw new Error(`Could not find prompter method in the provided adapter module: ${adapterPath}`);
  }
}

Steps to Reproduce (for bugs)

  1. First step
  2. Second step
```js module.exports = { extends: ['@commitlint/config-conventional'] } ```

and package.json

{
  "scripts": {
    "commit": "git-cz"
  },
  "config": {
    "commitizen": {
      "path": "@commitlint/prompt"
    }
  }
}

Context

Your Environment

Executable Version
commitlint --version 12.0.1
git --version 2.25.1
node --version 12.16.1
commitizen --version 4.2.3
escapedcat commented 3 years ago

Hey, did this use to work with an older version or is this a new issue related to v12?

alpavlove commented 3 years ago

I can confirm it works with v11

armano2 commented 3 years ago

@escapedcat @a-pavlov-parc i did some digging and there seem to be an issue in commitizen


v11: code was transpiled with babel and async functions where converted to functions with regenerator v12: code is compiled with typescript and there is no need anymore to do this transformation


https://github.com/commitizen/cz-cli/blob/ba7eeb67c4d3347cc2369ff6538d9d97761cedc8/src/commitizen/adapter.js#L150-L153

function getPrompter (adapterPath) {
  // Resolve the adapter path
  let resolvedAdapterPath = resolveAdapterPath(adapterPath);

  // Load the adapter
  let adapter = require(resolvedAdapterPath);

  /* istanbul ignore next */
  if (adapter && adapter.prompter && isFunction(adapter.prompter)) {
     return adapter.prompter;
  } else if (adapter && adapter.default && adapter.default.prompter && isFunction(adapter.default.prompter)) {
     return adapter.default.prompter;
  } else {
    throw new Error(`Could not find prompter method in the provided adapter module: ${adapterPath}`);
  }
}

adapter.prompter is a async function but isFunction returns false

function isFunction (functionToCheck) {
  if (typeof functionToCheck === "undefined")
  {
    return false;
  } else if (functionToCheck === null) {
    return false;
  } else {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  }
}
Object.prototype.toString.call(async function () {}) // [object AsyncFunction]
Object.prototype.toString.call(function () {}) // [object Function]

this implementation is not correct as it should be

function isFunction (functionToCheck) {
   return typeof functionToCheck === 'function'
}

this issue should be reported to commitizen but as "workaround" we could take this

https://github.com/conventional-changelog/commitlint/blob/6a48e44dbc803bf49b1f9253e650ca1184421914/%40commitlint/prompt/src/index.ts#L14-L17

and replace it with

export function prompter(_: unknown, commit: Commit): void {
    input(vorpal).then((message) => {
        commit(message);
    });
}
curly210102 commented 3 years ago

@escapedcat @a-pavlov-parc i did some digging and there seem to be an issue in commitizen

v11: code was transpiled with babel and async functions where converted to functions with regenerator v12: code is compiled with typescript and there is no need anymore to do this transformation

https://github.com/commitizen/cz-cli/blob/ba7eeb67c4d3347cc2369ff6538d9d97761cedc8/src/commitizen/adapter.js#L150-L153

function getPrompter (adapterPath) {
  // Resolve the adapter path
  let resolvedAdapterPath = resolveAdapterPath(adapterPath);

  // Load the adapter
  let adapter = require(resolvedAdapterPath);

  /* istanbul ignore next */
  if (adapter && adapter.prompter && isFunction(adapter.prompter)) {
     return adapter.prompter;
  } else if (adapter && adapter.default && adapter.default.prompter && isFunction(adapter.default.prompter)) {
     return adapter.default.prompter;
  } else {
    throw new Error(`Could not find prompter method in the provided adapter module: ${adapterPath}`);
  }
}

adapter.prompter is a async function but isFunction returns false

function isFunction (functionToCheck) {
  if (typeof functionToCheck === "undefined")
  {
    return false;
  } else if (functionToCheck === null) {
    return false;
  } else {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  }
}
Object.prototype.toString.call(async function () {}) // [object AsyncFunction]
Object.prototype.toString.call(function () {}) // [object Function]

this implementation is not correct as it should be

function isFunction (functionToCheck) {
   return typeof functionToCheck === 'function'
}

this issue should be reported to commitizen but as "workaround" we could take this

https://github.com/conventional-changelog/commitlint/blob/6a48e44dbc803bf49b1f9253e650ca1184421914/%40commitlint/prompt/src/index.ts#L14-L17

and replace it with

export function prompter(_: unknown, commit: Commit): void {
  input(vorpal).then((message) => {
      commit(message);
  });
}

@armano2 Thanks for help.

Will this fix to be released in the next version, or users have to modify it by fork and wait for commitizen to fix.

escapedcat commented 3 years ago

@curly210102 so far this hasn't been changed on our side. Happy if you want create a PR with the workaround here.
If someone opens up an issue at commitizen it would be nice to link it here.

armano2 commented 2 years ago

@escapedcat this can be closed as for #2697

escapedcat commented 2 years ago

Thanks @armano2 , closing

tal-rofe commented 2 years ago

This issue can be reproduced when bundling the application webpack and ts-loader together with tsconfig.json.

I consoled my output:

{ prompter: [AsyncFunction: prompter] }
Could not find prompter method in the provided adapter module: dist/index.js

This happens when I have async prompter.

escapedcat commented 2 years ago

@tal-rofe would you mind creating a new issue and maybe a repo for reproduction? Thanks

tal-rofe commented 2 years ago

@tal-rofe would you mind creating a new issue and maybe a repo for reproduction? Thanks

Hi, I opened the issue in commitizen repo page as it is more related with their code