Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: revert #3384 DEP0019 EOL #16634

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ code.
<a id="DEP0019"></a>
### DEP0019: require('.') resolved outside directory

Type: End-of-Life
Type: Runtime

In certain cases, `require('.')` may resolve outside the package directory.
This behavior is deprecated and will be removed in a future major Node.js
Expand Down
26 changes: 25 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function tryExtensions(p, exts, isMain) {
return false;
}

var warned = false;
Module._findPath = function(request, paths, isMain) {
if (path.isAbsolute(request)) {
paths = [''];
Expand Down Expand Up @@ -232,6 +233,18 @@ Module._findPath = function(request, paths, isMain) {
}

if (filename) {
// Warn once if '.' resolved outside the module dir
if (request === '.' && i > 0) {
if (!warned) {
warned = true;
process.emitWarning(
'warning: require(\'.\') resolved outside the package ' +
'directory. This functionality is deprecated and will be removed ' +
'soon.',
'DeprecationWarning', 'DEP0019');
}
}

Module._pathCache[cacheKey] = filename;
return filename;
}
Expand Down Expand Up @@ -334,7 +347,8 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
}

// Check for relative path
if (request.charCodeAt(0) !== 46/*.*/ &&
if (request.length < 2 ||
request.charCodeAt(0) !== 46/*.*/ ||
(request.charCodeAt(1) !== 46/*.*/ &&
request.charCodeAt(1) !== 47/*/*/)) {
var paths = modulePaths;
Copy link
Member

@jdalton jdalton Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of charCodeAt jazz really complicates the logic. It was introduced in a pass to optimize lots of things but I suspect it was micro-opts carried a bit too far. Anyways, would this do to fix it:

if (! (request.length === 1 && 
       request.charCodeAt(0) === codeOfDot) &&
    ! (request.charCodeAt(0) === codeOfDot &&
        (request.charCodeAt(1) === codeOfDot || 
         request.charCodeAt(1) === codeOfSlash))) {

Expand All @@ -345,6 +359,16 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
paths = parent.paths.concat(paths);
}

// Maintain backwards compat with certain broken uses of require('.')
// by putting the module's directory in front of the lookup paths.
if (request === '.') {
if (parent && parent.filename) {
paths.unshift(path.dirname(parent.filename));
} else {
paths.unshift(path.resolve(request));
}
}

debug('looking for %j in %j', request, paths);
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
}
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-require-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const b = require(fixtures.path('module-require', 'relative', 'dot-slash.js'));
assert.strictEqual(a.value, 42);
assert.strictEqual(a, b, 'require(".") should resolve like require("./")');

// require('.') should not lookup in NODE_PATH
process.env.NODE_PATH = fixtures.path('module-require', 'relative');
m._initPaths();
assert.throws(() => { require('.'); }, Error, "Cannot find module '.'");

const c = require('.');

assert.strictEqual(c.value, 42, 'require(".") should honor NODE_PATH');