prettier / prettier-eslint-cli

CLI for prettier-eslint
https://npm.im/prettier-eslint-cli
MIT License
539 stars 85 forks source link

Support Yarn Plug n Play #446

Closed jeffmccormick closed 1 year ago

jeffmccormick commented 1 year ago

Minimal repo setup with PnP enabled:

yarn init
yarn set version stable
echo "nodeLinker: pnp" >> .yarnrc.yml
yarn install
# Throw in React ESLint config as an example module to import
yarn add -D eslint@8.13.0 prettier@2.5.1 prettier-eslint-cli@7.1.0 eslint-config-react-app@7.0.1

Example ESLint config:

{
    "root": true,
    "extends": ["eslint:recommended", "react-app"]
}

Some random JS file to lint:

// test.js
import 'eslint-config-react-app';

let test = {};

Try to run prettier-eslint-cli:

yarn prettier-eslint test.js

Error output:

prettier-eslint [ERROR]: eslint fix failed due to an eslint error
prettier-eslint-cli [ERROR]: There was an error formatting "[working dir]\test.js":
    TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''
        at createRequire (internal/modules/cjs/loader.js:1169:13)
        at Object.resolve ([working dir]\.yarn\cache\@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:2325:16)
        at Object.ModuleResolver.resolve ([working dir]\.yarn\cache\@rushstack-eslint-patch-npm-1.2.0-917f402e4e-faa749faae.zip\node_modules\@rushstack\eslint-patch\lib\modern-module-resolution.js:210:48)
        at ConfigArrayFactory._loadPlugin ([working dir]\.yarn\cache\@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3392:33)
        at ConfigArrayFactory._loadPlugin ([working dir]\.yarn\cache\@rushstack-eslint-patch-npm-1.2.0-917f402e4e-faa749faae.zip\node_modules\@rushstack\eslint-patch\lib\modern-module-resolution.js:219:43)
        at [working dir]\.yarn\cache\@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3283:33
        at Array.reduce (<anonymous>)
        at ConfigArrayFactory._loadPlugins ([working dir]\.yarn\cache\@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3279:22)
        at ConfigArrayFactory._normalizeObjectConfigDataBody ([working dir]\.yarn\cache\@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3079:44)
        at _normalizeObjectConfigDataBody.next (<anonymous>)
failure formatting 1 file with prettier-eslint

Looks like there is some disagreement about the format in which modules are getting passed around Yarn, eslint, and prettier-eslint, but I haven't been able to dig in further.

jeffmccormick commented 1 year ago

Upon further investigation, it appears that this is a deeply rooted issue that goes all the way to ESLint and how it load modules. This it not a prettier-eslint issue, so I will close this.

If anyone else has this issue, there is some relevant discussion over here. And the temporary hack fix is to use @rushstack/eslint-patch in your config file (this is what CRA uses) to convince ESLint to load modules via the Yarn PnP resolver. So basically, I needed to change my .eslintrc.json to a .eslintrc.js like so:

require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
    "root": true,
    "extends": ["eslint:recommended", "react-app"]
}

If you're here after (presumed) v9.x of ESLint has been released, you should instead just use the new config system.