question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use file location for `.eslintrc.js` resolution

See original GitHub issue

It seems that when using multiple .eslintrc.js files throughout the workspace (of which some may use root: true), vscode-eslint doesn’t follow the same resolution algorithm that eslint itself uses.

It seems that eslint.workingDirectories could be a (very inelegant and manual) solution to this problem, by specifying every directory, that has an .eslintrc.js file as a working directory.

Is there no way for vscode-eslint to “just” follow the same resolution logic that eslint uses? Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
buschtoenscommented, Aug 8, 2019

I think most issues can be traced back to an incorrect process.cwd().

I have a shareable config that looks up the package name from the package.json of the project, to configure import-helper/order-imports. It basically does this:

const APP_NAME = require(`${process.cwd()}/package.json`).name;

module.exports = {
  plugins: ['import-helpers'],
  rules: {
    'import-helpers/order-imports': [
      'error',
      {
        newlinesBetween: 'always',
        groups: [
          // ...
          [`/^${APP_NAME}\\//`],
          // ...
        ],
        alphabetize: { order: 'asc', ignoreCase: true }
      }
    ]
  }
};

I have a monorepo with a package.json at the root of the repository, that basically looks like this:

// ./package.json
{
  "name": "my-monorepo",
  "workspaces": [
    "packages/*"
  ]
}

And then in the package where I use my shareable import-helper/order-imports config, I basically got this package.json:

// ./packages/some-app/package.json
{
  "name": "some-app",
  "scripts": {
    "lint:js": "eslint --ext ts,js ."
  },
  "devDependencies": {
    "@clark/eslint-config-ember": "^1.8.0",
    "eslint": "^6.1.0"
  }
}

And this .eslintrc.js:

// ./packages/some-app/.eslintrc.js
module.exports = {
  root: true,
  extends: '@clark/eslint-config-ember'
};

When I run yarn lint:js somewhere inside the some-app package (can even be a subdirectory of some-app), the process.cwd() is ./packages/some-app.

The require() shown in the first code snippet thus works like this:

process.cwd(); // => ./packages/some-app
const APP_NAME = require(`${process.cwd()}/package.json`).name; // => 'some-app'

When vscode-eslint runs the litner for this any file inside the some-app, package, the process.cwd() is ./ (the repo root).

The require() shown in the first code snippet thus works like this:

process.cwd(); // => ./
const APP_NAME = require(`${process.cwd()}/package.json`).name; // => 'my-monorepo'

I think vscode-eslint should do two things:

When a file is linted, traverse up the directory hierarchy and read all package.json files on the way. Once a package.json that lists eslint as a dependency is found, use it’s directory as the process.cwd() and also resolve eslint relative to this directory. As an added bonus, this allows using different versions of eslint in a monorepo, even though I would generally consider this to be an anti-pattern.

This would basically be an “automated” eslint.workingDirectories.

0reactions
buschtoenscommented, Aug 9, 2019

Whoops, didn’t mean to close this issue. GitHub did this for me, since I referenced this as Fixes in a PR in a private repo.

If any of the other participants feel that this should be reopened, I can do so. But at least for me, the issue is solved for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration Files - ESLint - Pluggable JavaScript Linter
The configuration cascade works based on the location of the file being linted. If there is a .eslintrc file in the same directory...
Read more >
webpack - How to manually add a path to be resolved in eslintrc
Then ESLint resolve from src directory. You can require src/hoge/moge.js by writing const moge = require('hoge/moge'); and ESLint knows it.
Read more >
How to set up scanning to my files eslintrc. js - Sonar Community
I've a JavaScript project which I want to scan it using Sonar Scanner but Files under the project root directory are not scanned...
Read more >
ESLint | IntelliJ IDEA Documentation - JetBrains
With ESLint, you can also use JavaScript Standard Style as well as ... is found in the current file folder, IntelliJ IDEA will...
Read more >
specify a path to the 'eslint' package - You.com | The AI Search ...
You can set the ESLint module import resolution by adding this snippet to your .eslintrc.json configuration file: { ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found