From b057b9f3073ddeb6999d967f0a15cf975875da82 Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Thu, 7 Apr 2022 10:49:06 -0700 Subject: [PATCH] Use dirname comparison instead --- .../node-resolver-core/src/NodeResolver.js | 10 +- .../utils/node-resolver-core/test/resolver.js | 124 ++++++++++++++++++ 2 files changed, 129 insertions(+), 5 deletions(-) diff --git a/packages/utils/node-resolver-core/src/NodeResolver.js b/packages/utils/node-resolver-core/src/NodeResolver.js index 7b7cd1b1f4c..3b8614b5d42 100644 --- a/packages/utils/node-resolver-core/src/NodeResolver.js +++ b/packages/utils/node-resolver-core/src/NodeResolver.js @@ -779,7 +779,6 @@ export default class NodeResolver { let res = (await this.loadAsFile({ file: entry.filename, - asPackageEntry: true, extensions, env, pkg, @@ -973,14 +972,12 @@ export default class NodeResolver { extensions, env, pkg, - asPackageEntry, ctx, }: {| file: string, extensions: Array, env: Environment, pkg: InternalPackageJSON | null, - asPackageEntry?: boolean, ctx: ResolverContext, |}): Promise { // Try all supported extensions @@ -1002,9 +999,12 @@ export default class NodeResolver { if (found) { return { path: found, - // If this is an entrypoint in package.json, it's possible pkg is not the + // If this package.json isn't a sibling of found, it's possible pkg is not the // closest package.json to the resolved file. Reload it instead. - pkg: asPackageEntry ? await this.findPackage(found, ctx) : pkg, + pkg: + pkg == null || pkg?.pkgdir !== path.dirname(found) + ? await this.findPackage(found, ctx) + : pkg, }; } diff --git a/packages/utils/node-resolver-core/test/resolver.js b/packages/utils/node-resolver-core/test/resolver.js index 41d70d711a8..3b7e3e3fff8 100644 --- a/packages/utils/node-resolver-core/test/resolver.js +++ b/packages/utils/node-resolver-core/test/resolver.js @@ -379,6 +379,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(__dirname, '..', 'src', '_empty.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(__dirname, '..', 'src', '_empty.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -721,6 +725,16 @@ describe('resolver', function () { 'package.json', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-main-directory', + 'nested', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -758,6 +772,16 @@ describe('resolver', function () { fileName: 'node_modules/foo', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'foo', + 'nested', + 'baz.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -822,6 +846,17 @@ describe('resolver', function () { fileName: 'node_modules/@scope/pkg', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + '@scope', + 'pkg', + 'foo', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -858,6 +893,16 @@ describe('resolver', function () { fileName: 'node_modules/side-effects-false', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -898,6 +943,16 @@ describe('resolver', function () { fileName: 'node_modules/side-effects-false', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -957,6 +1012,16 @@ describe('resolver', function () { ), fileName: 'package.json', }, + { + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + fileName: 'package.json', + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1016,6 +1081,16 @@ describe('resolver', function () { ), fileName: 'package.json', }, + { + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + fileName: 'package.json', + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1582,6 +1657,17 @@ describe('resolver', function () { 'nested', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-browser-alias', + 'subfolder1', + 'subfolder2', + 'subfile.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1720,6 +1806,16 @@ describe('resolver', function () { 'test', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-alias-glob', + 'src', + 'test.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1941,6 +2037,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -1979,6 +2079,10 @@ describe('resolver', function () { { filePath: path.join(rootDir, 'nested', 'package.json'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested/index.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2008,6 +2112,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2046,6 +2154,10 @@ describe('resolver', function () { { filePath: path.join(rootDir, 'nested', 'package.json'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested/index.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2104,6 +2216,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2133,6 +2249,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2167,6 +2287,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'),