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

Further node resolve filename fix #784

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
1 change: 0 additions & 1 deletion internal/e2e/fine_grained_no_bin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ nodejs_binary(
name = "index",
data = [
"index.js",
"test/test.js",
"@fine_grained_no_bin//fs.realpath",
],
entry_point = "build_bazel_rules_nodejs/internal/e2e/fine_grained_no_bin/index.js",
Expand Down
4 changes: 1 addition & 3 deletions internal/e2e/fine_grained_no_bin/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const path = require("path");

console.log('hello ' + require.resolve("./test.js", { paths: [ path.join(__dirname, 'test') ] }));
console.log('hello World');
1 change: 1 addition & 0 deletions internal/e2e/node/lib1/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
13 changes: 13 additions & 0 deletions internal/e2e/node/module_resolution.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const Module = require('module');
const path = require('path');

const node_resolve_index = require('node_resolve_index');
const node_resolve_index_2 = require('node_resolve_index_2');
const node_resolve_index_3 = require('node_resolve_index_3');
Expand Down Expand Up @@ -36,4 +39,14 @@ describe('node npm resolution', () => {
it('should be able to deep-import from a nested src dir', () => {
expect(lib1some.a).toEqual('lib1 content');
});
it('should respect paths options for require.resolve', () => {
const customPath = path.join(__dirname, 'lib1', 'src');
const p = require.resolve('./index.js', {paths: [customPath]})
expect(p).toEqual(path.join(customPath, 'index.js'));
});
it('should respect parent paths option for Module._resolveFilename', () => {
const custom_path = path.join(__dirname, 'lib1', 'src', 'node_modules');
const p = Module._resolveFilename('test', {paths: [custom_path]});
expect(p).toEqual(path.join(custom_path, 'test', 'index.js'));
});
});
42 changes: 29 additions & 13 deletions internal/node/node_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,23 @@ function resolveRunfiles(parent, ...pathSegments) {
return defaultPath;
}

function resolvedWithinPath(parentFilename, parentSegments, parentNodeModulesSegment, request, resolved) {
const parentRoot = parentSegments.slice(0, parentNodeModulesSegment).join('/');
const relative = path.relative(parentRoot, resolved);
if (!relative.startsWith('..')) {
// Resolved within parent node_modules
if (DEBUG)
console.error(
`node_loader: resolved ${request} within parent node_modules to ` +
`${resolved} from ${parentFilename}`
);
return resolved;
} else {
throw new Error(
`Resolved to ${resolved} outside of parent node_modules ${parentFilename}`);
}
}

var originalResolveFilename = module.constructor._resolveFilename;
module.constructor._resolveFilename = function(request, parent, isMain, options) {
const parentFilename = (parent && parent.filename) ? parent.filename : undefined;
Expand Down Expand Up @@ -367,19 +384,18 @@ module.constructor._resolveFilename = function(request, parent, isMain, options)
const parentSegments = parentFilename ? parentFilename.replace(/\\/g, '/').split('/') : [];
const parentNodeModulesSegment = parentSegments.indexOf('node_modules');
if (parentNodeModulesSegment != -1) {
const parentRoot = parentSegments.slice(0, parentNodeModulesSegment).join('/');
const relative = path.relative(parentRoot, resolved);
if (!relative.startsWith('..')) {
// Resolved within parent node_modules
if (DEBUG)
console.error(
`node_loader: resolved ${request} within parent node_modules to ` +
`${resolved} from ${parentFilename}`
);
return resolved;
} else {
throw new Error(
`Resolved to ${resolved} outside of parent node_modules ${parentFilename}`);
return resolvedWithinPath(
parentFilename, parentSegments, parentNodeModulesSegment, request, resolved);
}
// Also allow imports of npm packages that are within custom provided node_modules paths
// Note: Note sure if this is something officially supported but it does exist in the wile
// and does work with the default node resolution algorithm
const parentPaths = parent.paths
if (Array.isArray(parentPaths) && parentPaths.length > 0) {
for (const p of parentPaths) {
const pSegments = p.replace(/\\/g, '/').split('/');
const pNodeModulesSegment = pSegments.indexOf('node_modules');
return resolvedWithinPath(p, pSegments, pNodeModulesSegment, request, resolved);
}
}
throw new Error('Not a built-in module, relative or absolute import');
Expand Down