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: restore and warn on require('.') usage with NODE_PATH #1363

Closed
wants to merge 6 commits into from
Closed
Changes from 2 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
13 changes: 12 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ Module._findPath = function(request, paths) {
}

if (filename) {
if (request === '.' && i > 0) {
console.error(`(node) warning: require('.') resolved to ${filename}`);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any advice on how to use util.deprecate in this case? It seems util.deprecate is tuned for deprecating functions. In this case, I'd like to print a single warning if this condition is met.

Copy link
Member

Choose a reason for hiding this comment

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

just a thought, and perhaps this is too much of a hack but:

var noopDeprecateRequireDotFile = util.deprecate(function() {}, 'message here')

...

     if (request === '.' && i > 0) noopDeprecateRequireDotFile();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd be fine with something like that, but in the long term, we probably need something like util.warnOnce(msg). These probably shouldn't be public methods either.

Module._pathCache[cacheKey] = filename;
return filename;
}
Expand Down Expand Up @@ -205,12 +208,20 @@ Module._resolveLookupPaths = function(request, parent) {
}

var start = request.substring(0, 2);
if (start !== '.' && start !== './' && start !== '..') {
if (start !== './' && start !== '..') {
var paths = modulePaths;
if (parent) {
if (!parent.paths) parent.paths = [];
paths = parent.paths.concat(paths);
}

// For '.', put the module's dir at the front of the resolve paths
// TODO(silverwind): Treat '.' exactly the same as './'
if (request === '.') {
paths.splice(0, 0, parent && parent.filename ?
path.dirname(parent.filename) : path.resolve(request));
}

return [request, paths];
}

Expand Down