require.resolve with paths and non-existent directory returns incorrect local file when using ./

  • Version: 9.3.0
  • Platform: Linux 4.9.77 x86_64 GNU/Linux
  • Subsystem: require.resolve

To replicate create a directory with the following files:
test.js
index.js
exists/index.js

both of the index.js files can contain anything. They only have to exist.

test.js should contain the following:

'use strict';

let assert = require('assert');

const OPTS = { paths: ['./imaginary', './exists'] };
const FILE = 'index.js';

let bare = require.resolve(FILE, OPTS);
let dot = require.resolve(`./${FILE}`, OPTS);
console.info(`bare: ${bare}`);
console.info(`dot: ${dot}`);
assert.strictEqual(bare, dot);

Execute the test.js file with node test.js on v9.3.0 or later.

As you can see from the code in the test, simply adding ./ to the file name to be resolved causes a local matching file to be returned.

Specifically:

  • When an earlier path in the paths array doesn't exist
  • Even though there is a matching file in a path that does exist later in the paths array.

This bug was introduced in v9.3.0. v9.2.1 and prior behave correctly.