Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix getPackageForModule implementation against a package root path
Summary: Fixes #965. **Cause**: The implementation of `context.getPackageForModule(modulePath: string)` attempts to locate a `package.json` file starting with `parsedPath.dir` — however this clobbers the path basename when `modulePath` is initially the package directory. e.g. for the path `/root/node_modules/ipld/dag-cbor` (a package root): - `/root/node_modules/ipld/dag-cbor/package.json` **exists**. - `getPackageForModule('/root/node_modules/ipld/dag-cbor')` begins traversal, **incorrectly**, from `/root/node_modules/ipld`. https://pxl.cl/2Cb1T **Why this didn't matter until now** - This was previously inconsequential for `mainFields` (`"browser"` field spec), since redirections ***only*** support subpaths. (`"browser"` does not describe/permit a `"."` subpath, but instead a redirection of a *file path*, e.g. `"./index.js"`.) https://github.com/defunctzombie/package-browser-field-spec#replace-specific-files---advanced - On fall through from `mainFields` subpath resolution (implemented via `context.redirectModulePath` and not sharing the regular `nodeModulesPath` iteration loop), `resolveModulePath` will attempt `./package.json` to call [`getPackageEntryPoint`](https://github.com/facebook/metro/blob/d0b8ea57749d081f66c62ad4a4863caff62aa1d4/packages/metro-resolver/src/PackageResolve.js#L23). Note: In tests, this behaviour was already (necessarily) added in D43055160, but did not line up!: https://github.com/facebook/metro/blob/d0b8ea57749d081f66c62ad4a4863caff62aa1d4/packages/metro-resolver/src/__tests__/utils.js#L102 Changelog: **[Experimental]** Fix `package.json` discovery against root package specifiers for Package Exports (⬆️ Would previously fall-through to file-based resolution.) Reviewed By: robhogan Differential Revision: D44877146 fbshipit-source-id: bb02d81b9e7d2ef71846d492cfbc38c85a4bf98b
- Loading branch information