Prettier-eslint: Not working properly width eslint 6

Created on 11 Jul 2019  路  13Comments  路  Source: prettier/prettier-eslint

Versions:

  • prettier-eslint 9.0.0:
  • node 10.15.3:
  • npm (or yarn) version: 6.10.0

Have you followed the debugging tips?

Yes

Relevant code or config
https://github.com/prettier/prettier-eslint/blob/c87b69b769c59bcea0d96c87dddb6a4925085d8a/src/index.js#L225-L243

  const eslintOptions = {};
  if (filePath) {
    eslintOptions.cwd = path.dirname(filePath);
  }
  logger.trace(
    oneLine`
      creating ESLint CLI Engine to get the config for
      "${filePath || process.cwd()}"
    `
  );
  const cliEngine = getESLintCLIEngine(eslintPath, eslintOptions);
  try {
    logger.debug(`getting eslint config for file at "${filePath}"`);
    const config = cliEngine.getConfigForFile(filePath);
+   config.cwd = eslintOptions.cwd
    logger.trace(
      `eslint config for "${filePath}" received`,
      prettyFormat(config)
    );
    return config;

What I did:

Support eslint 6

What happened:
when I set "prettier.eslintIntegration": true
then the error happend in VSCode

Failed to load plugin 'standard' declared in 'CLIOptions'/Users/nashaofu/edc/web/src/c.js:: Cannot find module 'eslint-plugin-standard'
Require stack:
- __placeholder__.js

My eslint configuration is as follows:

module.exports = {
  root: true,
  parser: 'babel-eslint',
  env: {
    es6: true,
    node: true,
    commonjs: true
  },
  extends: ['standard']
}

Problem description:

The CWD of the process pointed incorrectly

Suggested solution:
details https://github.com/prettier/prettier-vscode/issues/869

released

Most helpful comment

Any news on this?

All 13 comments

this is causing me issues too - any help resolving would be great!

We just recently added support for eslint 5 so prettier-eslint might not work with eslint 6 as it hasn't been tested by us and depends on eslint 5. Will accept a PR for eslint 6 support otherwise I'll try and get to it in a couple of weeks as I'm on vacation currently.

@zimme @nashaofu I found related one more issue related to eslint 6 and CWD.

Minimal reproducible repo: https://github.com/phts/prettier-eslint-overrides-bug

TL;DR:

Issue with overrides.

When I set override with explicit file path, e.g. src/myFile.js - eslint cannot match config to this file, and eslint will be just skipped by prettier-eslint --write **/*.js.

When override contains ** - then everything works fine.

And if I remove line eslintOptions.cwd = path.dirname(filePath); - all cases above work fine.

@phts If I remove line eslintOptions.cwd = path.dirname(filePath);, it doesn't seem to use eslint, it just uses prettier.

@nashaofu could you please make a pull request with your change :)?

@zimme please look into it, we really need this fix.

The reason for the problem (also reported in https://github.com/microsoft/vscode-eslint/issues/696 and https://github.com/prettier/prettier-atom/issues/505) is setting the cwd option as the path for resolving plugins in this line and trying to resolve plugins here which results in an error if cwd isn't set correctly.

Would appreciate some eyes on https://github.com/prettier/prettier-eslint/pull/236 (not just from this project's maintainers). I know this problem is affecting a lot of people who use VS Code and enjoy using Prettier format on save in projects that have an ESLint integration.

Removing eslintOptions.cwd = path.dirname(filePath); doesn't feel like a valid option.

Any news on this?

I don't want to ping people with a new comment, but _wow_ comments like the one below are not helpful. If it's such an emergency for you, feel free to fork the repo and publish the lib under your own NPM org

When open source fails

Fixed issue by adding cwd: __dirname to my .eslintrc.js, it works (since prettier-eslint pass eslintOptions to new CLIEngine, which accepts cwd) and then all works correctly.

But feels like a hack (and eslint complains about unknown property), prettier-eslint should add cwd by itself. Easiest way to do it like this:

 function createEslintFix(eslintConfig, eslintPath) {
        return function eslintFix(text, filePath) {
+               const cwd = path.dirname(filePath);
+
                const cliEngine = (0, _utils.getESLintCLIEngine)(
                        eslintPath,
-                       eslintConfig,
+                       Object.assign(Object.create(null), eslintConfig, { cwd }),
                );

But not sure, that it is not a hack too.

Alternatively you might want to check out: https://www.npmjs.com/package/@effective/prettier comes together with a brand new extension for VSCode: https://marketplace.visualstudio.com/items?itemName=sebastian-software.effective-prettier-vscode - hopefully this fixes the issues for some.

:tada: This issue has been resolved in version 10.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dimomir picture Dimomir  路  5Comments

elaijuh picture elaijuh  路  3Comments

jingsam picture jingsam  路  5Comments

m-diiorio picture m-diiorio  路  6Comments

chrisbobbe picture chrisbobbe  路  8Comments