Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

linter-eslint can't find package.json in lerna repo, but the CLI can #1304

Open
3 tasks done
kaiyoma opened this issue Sep 10, 2019 · 3 comments
Open
3 tasks done

linter-eslint can't find package.json in lerna repo, but the CLI can #1304

kaiyoma opened this issue Sep 10, 2019 · 3 comments
Labels

Comments

@kaiyoma
Copy link

kaiyoma commented Sep 10, 2019

Issue Type

Question

Issue Description

I have a lerna repo configured in a pretty standard way:

my-project
├── .eslintrc.json
├── package.json
└─┬ packages
  ├─┬ package1
  | ├── .eslintrc.json
  | └── package.json
  └─┬ package2
    ├── .eslintrc.json
    └── package.json

When I enter into a package directory and run yarn run lint, everything is great. If I run lerna run lint from the root of the repo, everything is also great (because under the hood, lerna will cd into each package directory). However, if I open any source file in Atom, I get this error:

The package.json file could not be found. (import/no-extraneous-dependencies)

For each of my packages, I have the following defined in the local .eslintrc.json file:

    "import/no-extraneous-dependencies": ["error", {
      "packageDir": [".", "../.."]
    }],

This tells eslint to look at the local package.json (the one in the package directory) and the one in the root of the repo, since we define our dependencies in both places. The fact that Atom is reporting this error suggests to me that the context (current working directory) is wrong somehow. For a lerna repo, the current working directory should be the package directory, not the root of the repo. Is there a way for linter-eslint to do the right thing here and use the package directory as the current working directory?

Bug Checklist

  • Restart Atom
  • Verify the eslint CLI gives the proper result, while linter-eslint does not
  • Paste the output of the Linter Eslint: Debug command from the Command Palette below
Atom version: 1.40.1
linter-eslint version: 8.5.5
ESLint version: 6.3.0
Hours since last Atom restart: 4.7
Platform: win32
Using local project ESLint from: <redacted>\node_modules\eslint
Current file's scopes: [
  "source.js",
  "support.variable"
]
linter-eslint configuration: {
  "disabling": {
    "disableWhenNoEslintConfig": true,
    "rulesToSilenceWhileTyping": []
  },
  "scopes": [
    "source.ts",
    "source.tsx",
    "source.js",
    "source.jsx",
    "source.flow",
    "source.babel",
    "source.js-semantic"
  ],
  "lintHtmlFiles": false,
  "autofix": {
    "fixOnSave": false,
    "ignoreFixableRulesWhileTyping": false,
    "rulesToDisableWhileFixing": []
  },
  "global": {
    "useGlobalEslint": false,
    "eslintrcPath": "",
    "globalNodePath": ""
  },
  "advanced": {
    "disableEslintIgnore": false,
    "disableFSCache": false,
    "showRuleIdInMessage": true,
    "eslintRulesDirs": [],
    "localNodeModules": ""
  }
}
@IanVS
Copy link
Member

IanVS commented Sep 11, 2019

This is an interesting one. Do you happen to have a minimal reproducing repo that we could try out and experiment with? Also, out of curiosity, what happens if, from the main root of your project, you run yarn eslint packages/package1/<any_file.js>?

@kaiyoma
Copy link
Author

kaiyoma commented Sep 11, 2019

If I do that, I get the same errors being reported in Atom, so that makes me feel a little bit better that it's reproducible on the command line. But this isn't how a lerna repo works; you have to be in the directory of the package to run the scripts/tools.

(Similarly, if I open the package directory in Atom, then all the linting errors go away, because the working directory is correct. But this is kind of an annoying workflow since I can't "see" all my packages at once.)

I can work on a repro.

@kaiyoma
Copy link
Author

kaiyoma commented Sep 11, 2019

Here's a pretty minimal repro: eslint-issue.zip

After you yarn install, you'll notice that you can lint from the root (which runs lerna):

$ yarn run lint
yarn run v1.17.3
$ lerna run lint
lerna notice cli v3.16.4
lerna info versioning independent
lerna info Executing command in 1 package: "yarn run lint"
lerna info run Ran npm script 'lint' in 'foo' in 3.2s:
$ eslint --max-warnings 0 *.js
lerna success run Ran npm script 'lint' in 1 package in 3.2s:
lerna success - foo
Done in 4.69s.

Or you can lint from within the package:

$ cd packages/foo/
$ yarn run lint
yarn run v1.17.3
warning package.json: No license field
$ eslint --max-warnings 0 *.js
Done in 1.58s.

But trying to lint a package file from the root doesn't work:

$ yarn eslint packages/foo/rollup.config.js
yarn run v1.17.3
$ eslint-issue/node_modules/.bin/eslint packages/foo/rollup.config.js

eslint-issue/packages/foo/rollup.config.js
  0:1  error  The package.json file could not be found
                        import/no-extraneous-dependencies
  1:1  error  'rollup-plugin-commonjs' should be listed in the project's dependencies. Run 'npm i -S rollup-plugin-commo
njs' to add it          import/no-extraneous-dependencies
  2:1  error  'rollup-plugin-node-resolve' should be listed in the project's dependencies. Run 'npm i -S rollup-plugin-n
ode-resolve' to add it  import/no-extraneous-dependencies

✖ 3 problems (3 errors, 0 warnings)

@UziTech UziTech added the bug label Mar 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants