elicwhite / ESLint-Formatter

Sublime Text 3 Plugin to Autoformat with Eslint
MIT License
140 stars 20 forks source link

Config search regression introduced in 2.3.0 #52

Closed hudochenkov closed 7 years ago

hudochenkov commented 7 years ago

I have a monorepo project (inside project multiple sub-project each with own package.json). ESLint is set up in the project root directory, and not in sub-directories with own package.json. With this change introduced in 2.3.0 ESLint-Formatter looking in the closest package.json, but if there is no ESLint, it won't look into project root, and go straight to globally installed ESLint.

Using node.js path on 'osx': /usr/local/bin/node
Using eslint path on 'osx': /usr/local/bin/eslint
Unexpected error(<class 'Exception'>): Error: module.js:471
    throw err;
    ^

Error: Cannot find module '/usr/local/bin/eslint'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

error: Error: module.js:471
    throw err;
    ^

Error: Cannot find module '/usr/local/bin/eslint'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3
hudochenkov commented 7 years ago

Currently, I set up project-specific settings override as a workaround for this issue:

{
    "folders":
    [
        {
            "path": "."
        }
    ],
    "settings": {
        "ESLint-Formatter": {
            "eslint_path": {
                "osx": "/Users/aleks/project/node_modules/.bin/eslint"
            }
        }
    }
}
elicwhite commented 7 years ago

@ramiel, how should this be handled?

ramiel commented 7 years ago

I'll look at this tomorrow. We should stop at the project root instead of the first package.json probably.

ramiel commented 7 years ago

The problem can be a bit complex. At the moment we go up, starting from the current directory, and we stop at the first package.json we find. Then we suppose that is the point were eslint is installed. Setup like the one from @hudochenkov show that this is not always the case. We have so two possibilities.

The latter is not affordable because eslint allow to have more than one file and do not enforce you to put "root": true in the top one.
Reading the content of the package.json file instead is heavy, considering that the plugin has no cache system and that this operation is performed everytime a file if linted (easily at every save). It looks to me that using a per-project configuration for cases like this is the most viable solution.
@TheSavior have you any other idea?

ramiel commented 7 years ago

Another idea is to look for the first path where is present node_modules/eslint. This rely on the structure of the node_modules folder itself, which should be reliable until npm change it again or another fancy package manager is used. But it's fast. I'll propose a PR and we continue to discuss there