diff --git a/.gitignore b/.gitignore index f524bcd5..5658f8a4 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ build/Release # Dependency directory # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git node_modules +# Keep it for test +!test/custom-rules/relative-to-cwd/node_modules # Optional npm cache directory .npm diff --git a/markdownlint.js b/markdownlint.js index e747c193..d9fc2ce5 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -4,6 +4,7 @@ var fs = require('fs'); var path = require('path'); +var Module = require('module'); var program = require('commander'); var getStdin = require('get-stdin'); var jsYaml = require('js-yaml'); @@ -141,8 +142,15 @@ function loadCustomRuleFromFile(filepath) { function tryResolvePath(filepath) { try { if (path.basename(filepath) === filepath && path.extname(filepath) === '') { - // Looks like a package name - return require.resolve(filepath); + // Looks like a package name, resolve it relative to cwd + // Get list of directories, where requested module can be. + var paths = Module._nodeModulePaths(process.cwd()); + paths = paths.concat(Module.globalPaths); + if (require.resolve.paths) { + // Node >= 8.9.0 + return require.resolve(filepath, {paths: paths}); + } + return Module._resolveFilename(filepath, {paths: paths}); } // Maybe it is a path to package installed locally return require.resolve(path.join(process.cwd(), filepath)); diff --git a/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/index.js b/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/index.js new file mode 100644 index 00000000..b7502a0a --- /dev/null +++ b/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/index.js @@ -0,0 +1,10 @@ +module.exports = { + names: ['test-rule-package'], + description: 'Test rule package relative to cwd broken', + tags: ['test'], + function: (params, onError) => { + onError({ + lineNumber: 1 + }); + } +}; diff --git a/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/package.json b/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/package.json new file mode 100644 index 00000000..0914709b --- /dev/null +++ b/test/custom-rules/relative-to-cwd/node_modules/test-rule-package/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-rule-package", + "main": "./index.js", + "version": "0.0.1", + "private": true +} \ No newline at end of file diff --git a/test/custom-rules/relative-to-cwd/package.json b/test/custom-rules/relative-to-cwd/package.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/test/custom-rules/relative-to-cwd/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/test.js b/test/test.js index 651c8258..81dacf68 100644 --- a/test/test.js +++ b/test/test.js @@ -394,6 +394,25 @@ test('Custom rule from node_modules package loaded', async t => { } }); +test('Custom rule from node_modules package loaded relative to cwd', async t => { + try { + var input = '# Input'; + await execa(path.resolve('..', 'markdownlint.js'), + ['--rules', 'test-rule-package', '--stdin'], { + input, + cwd: path.join(__dirname, 'custom-rules', 'relative-to-cwd') + }); + t.fail(); + } catch (err) { + const expected = [ + 'stdin: 1: test-rule-package Test rule package relative to cwd broken', + '' + ].join('\n'); + t.true(err.stdout === ''); + t.true(err.stderr === expected); + } +}); + test('Custom rule from package loaded', async t => { try { var input = '# Input';