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

[metro-resolver] Respect transitive deps when using nodeModulesPaths #738

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion packages/metro-resolver/src/__tests__/index-test.js
Expand Up @@ -56,9 +56,19 @@ const CONTEXT: ResolutionContext = (() => {
},
'other-root': {
node_modules: {
'banana-module': {
'package.json': true,
'main.js': true,
},
banana: {
'package.json': true,
'main.js': true,
node_modules: {
'banana-module': {
'package.json': true,
'main.js': true,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the example when you suggested a regular module under banana/node_modules/other-module/main.js that can also be resolved from banana/main.js.

},
},
},
},
Expand Down Expand Up @@ -216,14 +226,35 @@ it('uses `nodeModulesPaths` to find additional node_modules not in the direct pa
expect(() => Resolver.resolve(context, 'kiwi', null))
.toThrowErrorMatchingInlineSnapshot(`
"Module does not exist in the Haste module map or in these directories:
/other-root/node_modules
/root/project/node_modules
/root/node_modules
/node_modules
/other-root/node_modules
"
`);
});

it('resolves transitive dependencies when using `nodeModulesPaths`', () => {
const context = Object.assign(
{},
{...CONTEXT, originModulePath: '/other-root/node_modules/banana/main.js'},
{
nodeModulesPaths: ['/other-root/node_modules'],
},
);

expect(Resolver.resolve(context, 'banana-module', null)).toEqual({
type: 'sourceFile',
filePath:
'/other-root/node_modules/banana/node_modules/banana-module/main.js',
});

expect(Resolver.resolve(context, 'banana-module', null)).not.toEqual({
type: 'sourceFile',
filePath: '/other-root/node_modules/banana-module/main.js',
});
});

describe('disableHierarchicalLookup', () => {
const context = Object.assign({}, CONTEXT, {
disableHierarchicalLookup: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/metro-resolver/src/resolve.js
Expand Up @@ -90,9 +90,10 @@ function resolve(
} catch (error) {}
}

const nodeModulesPaths = Array.from(context.nodeModulesPaths);
const nodeModulesPaths = [];
let next = path.dirname(originModulePath);
const {disableHierarchicalLookup} = context;

if (!disableHierarchicalLookup) {
let candidate;
do {
Expand All @@ -102,6 +103,9 @@ function resolve(
} while (candidate !== next);
}

// Fall back to `nodeModulesPaths` after hierarchical lookup, similar to $NODE_PATH
nodeModulesPaths.push(...context.nodeModulesPaths);
sharmilajesupaul marked this conversation as resolved.
Show resolved Hide resolved

const extraPaths = [];
const {extraNodeModules} = context;
if (extraNodeModules) {
Expand Down