diff --git a/lib/options-manager.js b/lib/options-manager.js index a0470c62..874ec04f 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -414,7 +414,7 @@ const buildExtendsConfig = options => config => { if (options.extends && options.extends.length > 0) { const configs = options.extends.map(name => { // Don't do anything if it's a filepath - if (existsSync(name)) { + if (existsSync(path.resolve(options.cwd || process.cwd(), name))) { return name; } diff --git a/test/fixtures/config-files/extends-relative/.xo-config.json b/test/fixtures/config-files/extends-relative/.xo-config.json new file mode 100644 index 00000000..29bc67f9 --- /dev/null +++ b/test/fixtures/config-files/extends-relative/.xo-config.json @@ -0,0 +1,3 @@ +{ + "extends": "../../extends.js" +} diff --git a/test/fixtures/config-files/extends-relative/file.js b/test/fixtures/config-files/extends-relative/file.js new file mode 100644 index 00000000..eb542496 --- /dev/null +++ b/test/fixtures/config-files/extends-relative/file.js @@ -0,0 +1,5 @@ +const object = { + a: 1 +}; + +console.log(object.a); diff --git a/test/lint-files.js b/test/lint-files.js index f1b4e455..a2b6b5a2 100644 --- a/test/lint-files.js +++ b/test/lint-files.js @@ -318,3 +318,20 @@ test(configType, {type: '.xo-config.js', dir: 'xo-config_js'}); test(configType, {type: '.xo-config.cjs', dir: 'xo-config_cjs'}); test(configType, {type: '.xo-config.json', dir: 'xo-config_json'}); test(configType, {type: '.xo-config', dir: 'xo-config'}); + +test('load config file with relative extends from different cwd', async t => { + const directory = 'extends-relative'; + + const {results, rulesMeta} = await xo.lintFiles('**/*', { + cwd: path.resolve('fixtures', 'config-files', directory), + }); + + t.true( + hasRule( + results, + path.resolve('fixtures', 'config-files', directory, 'file.js'), + 'comma-dangle', + rulesMeta, + ), + ); +}); diff --git a/test/lint-text.js b/test/lint-text.js index d5e6b32a..b04fb4fa 100644 --- a/test/lint-text.js +++ b/test/lint-text.js @@ -135,7 +135,7 @@ test('extends `react` support with `prettier` option', async t => { test('regression test for #71', async t => { const {results} = await xo.lintText('const foo = { key: \'value\' };\nconsole.log(foo);\n', { - extends: path.join(__dirname, 'fixtures/extends.js'), + extends: './fixtures/extends.js', }); t.is(results[0].errorCount, 0); }); diff --git a/test/options-manager.js b/test/options-manager.js index e102b36f..057d78c8 100644 --- a/test/options-manager.js +++ b/test/options-manager.js @@ -416,12 +416,14 @@ test('buildConfig: extends', t => { extends: [ 'plugin:foo/bar', 'eslint-config-prettier', + './fixtures/config-files/xo_config_js/xo.config.js', ], }); - t.deepEqual(config.baseConfig.extends.slice(-2), [ + t.deepEqual(config.baseConfig.extends.slice(-3), [ 'plugin:foo/bar', path.resolve('../node_modules/eslint-config-prettier/index.js'), + './fixtures/config-files/xo_config_js/xo.config.js', ]); });