Skip to content

fix: read correct package.json with read-pkg.sync #890

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

Closed
wants to merge 2 commits into from
Closed

fix: read correct package.json with read-pkg.sync #890

wants to merge 2 commits into from

Conversation

twsansbury
Copy link

Description

This corrects the read-pkg.sync() usage, providing the current working directory in the argument format read-pkg.sync() expects.

Motivation and Context

Fixes #887.

The read-pkg dependency upgrade (a876fb8) to v5.2.0 introduced an issue because read-pkg.sync's API introduced a breaking change. In v3.0.0 read-pkg.sync() accepts the filepath as the first argument. In v5.2.0, read-pkg.sync() accepts an object as its first argument, where the cwd key contains the filepath. See here.

As commitlint did not provide a cwd key, the read-pkg.sync() defaults to reading package.json from the current working directory.

$ docker container run --rm -it --workdir /tmp node:lts /bin/bash
root@451ac1a044a3:/tmp# npm install -g @commitlint/config-conventional
+ @commitlint/[email protected]
added 8 packages from 9 contributors in 2.064s

root@451ac1a044a3:/tmp# echo "feat: example" | npx commitlint -x @commitlint/config-conventional
ENOENT: no such file or directory, open '/tmp/package.json'

root@451ac1a044a3:/tmp# echo "feat: example" | npx [email protected] -x @commitlint/config-conventional

root@451ac1a044a3:/tmp#

Usage examples

// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional']
};
echo "FIX: some message" | commitlint # fails
echo "fix: some message" | commitlint # passes

How Has This Been Tested?

After setting up the local development environment, I reproduced the issue against master in a temporary directory. I then changed to the fix-read-pkg-v5-usage branch and re-ran the test case with success.

From within the git repository:

$ TMP_DIR="$(mktemp -d)"
$ git checkout master
$ (NODE_MODULES="$PWD"/node_modules; cd "$TMP_DIR"; echo "fix: some message" | "$NODE_MODULES"/.bin/commitlint -x "$NODE_MODULES"/@commitlint/config-conventional)
fs.js:125
    throw err;
    ^

Error: ENOENT: no such file or directory, open '/private/var/folders/hg/pn46533939vgwl3tc0ktm0vj8r095t/T/tmp.ZXkUozYH/package.json'
  ...
$ git checkout fix-read-pkg-v5-usage
$ (NODE_MODULES="$PWD"/node_modules; cd "$TMP_DIR"; echo "fix: some message" | "$NODE_MODULES"/.bin/commitlint -x "$NODE_MODULES"/@commitlint/config-conventional)
$

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@twsansbury twsansbury closed this Jan 3, 2020
@twsansbury twsansbury deleted the fix-read-pkg-v5-usage branch January 3, 2020 19:18
@byCedric
Copy link
Member

byCedric commented Jan 3, 2020

Thanks, and sorry for the inconvenience! ❤️ We patched it in 8.3.4 and I opened an issue to make sure it won't happen again.

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

Successfully merging this pull request may close these issues.

Bug: breaking API change in one of commitlint cli's dependencies: read-pkg
2 participants