xojs/xo

Relative 'extends' can fail; should resolve relative to config file

andrewdbond opened this issue · 1 comments

Description
'extends' using relative paths aren't always resolved relative to the configuration file.

To Reproduce
Steps to reproduce the behavior:

  1. Create a config file that extends from another config file that exists, using a relative path. Example: xo.config.js:
module.exports = {
 extends: ['./xo.and.eslint.shared.overrides.yaml']
};
  1. Use the XO Visual Studio Code extension.
  2. XO produces an error. XO's output:
[Info  - 7:52:41 PM] XO Server Starting in Node v16.13.2
[Info  - 7:52:42 PM] XO Library 0.52.2
                Resolved in Workspace d:\src
                Cached for Folder d:\src
[Info  - 7:52:42 PM] XO Library 0.52.2
                Resolved in Workspace d:\src
                Cached for Folder d:\src
[19:52:44:936] Cannot find module 'eslint-config-./xo.and.eslint.shared.overrides.yaml'
Require stack:
- d:\src\__placeholder__.js 
[19:52:44:937] Error: Cannot find module 'eslint-config-./xo.and.eslint.shared.overrides.yaml'
Require stack:
- d:\src\__placeholder__.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:987:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at resolve (file:///d:/src/node_modules/@eslint/eslintrc/lib/shared/relative-module-resolver.js:23:46)
    at resolveFrom (file:///d:/src/node_modules/xo/lib/options-manager.js:35:66)
    at file:///d:/src/node_modules/xo/lib/options-manager.js:425:24
    at Array.map (<anonymous>)
    at file:///d:/src/node_modules/xo/lib/options-manager.js:414:35
    at file:///d:/src/node_modules/lodash-es/_createFlow.js:71:31
    at buildConfig (file:///d:/src/node_modules/xo/lib/options-manager.js:283:3)
    at parseOptions (file:///d:/src/node_modules/xo/lib/options-manager.js:588:52)

Alternatively, in place of step 2 above, run XO from a different folder with the cwd option set to the config folder:

xo --cwd=<configFileDir>

Expected behavior
XO loads the extended config file.

Additional context
For comparison, "ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it."

Tested on Node.js v16.

@andrewdbond - the extension latest update has changed the logic and I believe the cwd will always be where it finds a package.json with xo as a dependency, so as a hack if you make the extends relative to that directory it will probably work as expected until this issue is fully resolved.