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

Implementation of --adjacent-node-modules to support module symlinking to global cache #9456

Closed
wants to merge 5 commits 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
47 changes: 39 additions & 8 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const path = require('path');
const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
const internalModuleStat = process.binding('fs').internalModuleStat;
const preserveSymlinks = !!process.binding('config').preserveSymlinks;
const adjacentNodeModules = !!process.binding('config').adjacentNodeModules
|| (process.env.NODE_ADJACENT_NODE_MODULES || '0') === '1';

// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
Expand Down Expand Up @@ -122,7 +124,7 @@ delete fs.realpathCacheKey;
// absolute realpath.
function tryFile(requestPath, isMain) {
const rc = stat(requestPath);
if (preserveSymlinks && !isMain) {
if (preserveSymlinks && !isMain || adjacentNodeModules) {
return rc === 0 && path.resolve(requestPath);
}
return rc === 0 && toRealPath(requestPath);
Expand Down Expand Up @@ -174,7 +176,7 @@ Module._findPath = function(request, paths, isMain) {
if (!trailingSlash) {
const rc = stat(basePath);
if (rc === 0) { // File.
if (preserveSymlinks && !isMain) {
if (preserveSymlinks && !isMain || adjacentNodeModules) {
filename = path.resolve(basePath);
} else {
filename = toRealPath(basePath);
Expand Down Expand Up @@ -255,9 +257,22 @@ if (process.platform === 'win32') {
// Use colon as an extra condition since we can get node_modules
// path for dirver root like 'C:\node_modules' and don't need to
// parse driver name.
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/) {
if (p !== nmLen)
paths.push(from.slice(0, last) + '\\node_modules');
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/
|| (adjacentNodeModules && code === 47/*.*/)) {
if (p !== nmLen) {
var parent = from.slice(0, last);
paths.push(parent + '\\node_modules');

if (adjacentNodeModules) {
paths.push(parent + '.node_modules');
if (code === 46/*.*/ && i > 0)
while (--i >= 0) {
const code = from.charCodeAt(i);
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/)
break;
}
}
}
last = i;
p = 0;
} else if (p !== -1) {
Expand All @@ -269,6 +284,8 @@ if (process.platform === 'win32') {
}
}

if (adjacentNodeModules) paths.pop();

return paths;
};
} else { // posix
Expand All @@ -289,9 +306,21 @@ if (process.platform === 'win32') {
var last = from.length;
for (var i = from.length - 1; i >= 0; --i) {
const code = from.charCodeAt(i);
if (code === 47/*/*/) {
if (p !== nmLen)
paths.push(from.slice(0, last) + '/node_modules');
if (code === 47/*/*/ || (adjacentNodeModules && code === 46/*.*/)) {
if (p !== nmLen) {
var parent = from.slice(0, last);
paths.push(parent + '/node_modules');

if (adjacentNodeModules) {
paths.push(parent + '.node_modules');
if (code === 46/*.*/ && i > 0)
while (--i >= 0) {
const code = from.charCodeAt(i);
if (code === 47/*/*/)
break;
}
}
}
last = i;
p = 0;
} else if (p !== -1) {
Expand All @@ -303,6 +332,8 @@ if (process.platform === 'win32') {
}
}

if (adjacentNodeModules) paths.pop();

// Append /node_modules to handle root paths.
paths.push('/node_modules');

Expand Down
6 changes: 6 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ bool trace_warnings = false;
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/module.js
bool config_preserve_symlinks = false;
// Set in node.cc by ParseArgs when --adjacent-node-modules is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/module.js
bool config_adjacent_node_modules = false;

bool v8_initialized = false;

Expand Down Expand Up @@ -3704,6 +3708,8 @@ static void ParseArgs(int* argc,
Revert(cve);
} else if (strcmp(arg, "--preserve-symlinks") == 0) {
config_preserve_symlinks = true;
} else if (strcmp(arg, "--adjacent-node-modules") == 0) {
config_adjacent_node_modules = true;
} else if (strcmp(arg, "--prof-process") == 0) {
prof_process = true;
short_circuit = true;
Expand Down
3 changes: 3 additions & 0 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ void InitConfig(Local<Object> target,

if (config_preserve_symlinks)
READONLY_BOOLEAN_PROPERTY("preserveSymlinks");

if (config_adjacent_node_modules)
READONLY_BOOLEAN_PROPERTY("adjacentNodeModules");
} // InitConfig

} // namespace node
Expand Down
5 changes: 5 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ extern const char* openssl_config;
// that is used by lib/module.js
extern bool config_preserve_symlinks;

// Set in node.cc by ParseArgs when --adjacent-node-modules is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/module.js
extern bool config_adjacent_node_modules;

// Tells whether it is safe to call v8::Isolate::GetCurrent().
extern bool v8_initialized;

Expand Down