From d3fb7e1b3658a6f00e1c631aa551f2ea0ab81f5e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 29 Mar 2019 03:11:41 +0100 Subject: [PATCH 1/2] module: mark DEP0019 as End-of-Life In certain cases, `require('.')` could resolve outside the package directory. This behavior has been removed. --- doc/api/deprecations.md | 10 ++++--- lib/internal/modules/cjs/loader.js | 45 +++++++----------------------- test/parallel/test-require-dot.js | 11 ++++---- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 9820b8039c0792..4641d591ea9ac5 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -442,6 +442,9 @@ code. ### DEP0019: require('.') resolved outside directory -Type: Runtime +Type: End-of-Life -In certain cases, `require('.')` may resolve outside the package directory. -This behavior is deprecated and will be removed in a future major Node.js -release. +In certain cases, `require('.')` could resolve outside the package directory. +This behavior has been removed. ### DEP0020: Server.connections diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 3d68f8fc625bfe..62c03fc3e795d8 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -77,7 +77,6 @@ const { CHAR_FORWARD_SLASH, CHAR_BACKWARD_SLASH, CHAR_COLON, - CHAR_DOT, CHAR_UNDERSCORE, CHAR_0, CHAR_9, @@ -382,18 +381,6 @@ 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; } @@ -497,35 +484,23 @@ Module._resolveLookupPaths = function(request, parent, newReturn) { return (newReturn ? null : [request, []]); } - // Check for non-relative path - if (request.length < 2 || - request.charCodeAt(0) !== CHAR_DOT || - (request.charCodeAt(1) !== CHAR_DOT && - request.charCodeAt(1) !== CHAR_FORWARD_SLASH && - (!isWindows || request.charCodeAt(1) !== CHAR_BACKWARD_SLASH))) { - var paths = modulePaths; - if (parent) { - if (!parent.paths) - paths = parent.paths = []; - else - paths = parent.paths.concat(paths); - } + // Check for node modules paths. + if (request.charAt(0) !== '.' || + (request.length > 1 && + request.charAt(1) !== '.' && + request.charAt(1) !== '/' && + (!isWindows || request.charAt(1) !== '\\'))) { - // 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)); - } + let paths = modulePaths; + if (parent != null && parent.paths && parent.paths.length) { + paths = parent.paths.concat(paths); } debug('looking for %j in %j', request, paths); return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]); } - // with --eval, parent.id is not set and parent.filename is null + // With --eval, parent.id is not set and parent.filename is null. if (!parent || !parent.id || !parent.filename) { // Make require('./path/to/foo') work - normally the path is taken // from realpath(__filename) but with eval there is no filename diff --git a/test/parallel/test-require-dot.js b/test/parallel/test-require-dot.js index 423441cfcde435..7145e688d4759f 100644 --- a/test/parallel/test-require-dot.js +++ b/test/parallel/test-require-dot.js @@ -14,9 +14,10 @@ assert.strictEqual(a, b); process.env.NODE_PATH = fixtures.path('module-require', 'relative'); m._initPaths(); -const c = require('.'); -assert.strictEqual( - c.value, - 42, - `require(".") should honor NODE_PATH; expected 42, found ${c.value}` +assert.throws( + () => require('.'), + { + message: /Cannot find module '\.'/, + code: 'MODULE_NOT_FOUND' + } ); From 5ca774d61f13261c491f5e9a1788fb8ae13b5399 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 31 Mar 2019 12:34:08 +0200 Subject: [PATCH 2/2] fixup! module: mark DEP0019 as End-of-Life --- lib/internal/modules/cjs/loader.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 62c03fc3e795d8..c3ff9c0f5066da 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -312,7 +312,6 @@ function findLongestRegisteredExtension(filename) { return '.js'; } -var warned = false; Module._findPath = function(request, paths, isMain) { if (path.isAbsolute(request)) { paths = [''];