Skip to content

Commit

Permalink
Fix bad module resolution (#42) (#43)
Browse files Browse the repository at this point in the history
* Fixes issue #42
  • Loading branch information
VeselovAlex authored and DavidAnson committed Jul 28, 2018
1 parent f5c5d5e commit e9392b8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions markdownlint.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/custom-rules/relative-to-cwd/package.json
@@ -0,0 +1 @@
{}
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -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';
Expand Down

0 comments on commit e9392b8

Please sign in to comment.