Skip to content

Commit

Permalink
Allow overriding eslint path with environment var (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgramigna authored Dec 19, 2024
1 parent 9f3c61f commit ba10df0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ All arguments are passed to eslint, except for the following commands:
- `ESLINT_D_MISS` How to behave if local eslint is missing. "fallback" uses the
bundled eslint (default). "fail" logs an error and exits with code 1.
"ignore" silently exits with code 0.
- `ESLINT_D_ROOT` Provide specific directory to search for `node_modules/eslint`.
Useful in monorepos where `node_modules` might be in a location other than
the project root.

## Automatic fixing

Expand Down
3 changes: 3 additions & 0 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ Environment variables:
ESLINT_D_MISS How to behave if local eslint is missing. "fallback" uses the
bundled eslint (default). "fail" logs an error and exits with
code 1. "ignore" silently exits with code 0.
ESLINT_D_ROOT Provide specific directory to search for node_modules/eslint.
Useful in monorepos where node_modules might be in a location other than
the project root.
`);
}
10 changes: 9 additions & 1 deletion lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export function createResolver() {
const local = getLocal();
let bundled = false;
let path;

try {
path = require.resolve('eslint/package.json', { paths: [process.cwd()] });
path = require.resolve('eslint/package.json', {
// Allow specific path to eslint with ESLINT_D_ROOT environment variable
// This is useful for monorepos where the location of node_modules can be more complex
paths: [
...(process.env.ESLINT_D_ROOT ? [process.env.ESLINT_D_ROOT] : []),
process.cwd()
]
});
} catch (err) {
if (local === 'ignore') {
return 'ignore';
Expand Down
17 changes: 17 additions & 0 deletions lib/resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,21 @@ describe('lib/resolver', () => {
});
});
});

context('with override', () => {
beforeEach(() => {
process.env.ESLINT_D_ROOT = resolve('test/fixture/v8.0.x');
});

it('uses path from environment variable when present', () => {
const resolver = createResolver();

refute.isString(resolver);
assert.isFalse(resolver['bundled']);
assert.equals(
resolver['base'],
normalize(`${process.env.ESLINT_D_ROOT}/node_modules/eslint`)
);
});
});
});

0 comments on commit ba10df0

Please sign in to comment.