-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Codegen on iOS is failing in monorepo projects #35429
Comments
Great writeup 💯 FWIW also happens with |
As a follow up, I found this piece of code that re-implements Node module resolution in Android. Implemented by no other than @ide 😄 It's nice, but I don't think this would work for isolated modules. Unfortunately, it still requires an additional path for this monorepo. That shouldn't be necessary, since That said, I'm not sure why you'd want to install from source without running |
The reason why I stress the isolated modules part is because Metro seems to be working on supporting the default pnpm behavior. pnpm is one of the few package managers that uses this isolated modules mode by default, unless using A short summary of isolated modules is that only the direct dependencies are installed in the
In the 1st example above, the project's direct dependencies are Now in the 2nd example, only the project's direct dependencies are installed in the project node_modules folder. And the This means that "just iterating from a nested folder, checking each parent folder for Just adding this here to give you a good context on what kind of fix is required to make this "properly" work with cutting edge package managers. |
Thanks for sharing. I can answer for Android as you called it out here:
Please note that you're referencing a comment. The Android template allows you to customize where the For iOS: #35430 should do the fix right? |
@cortinico, that makes sense, thanks. Might be worth defaulting in the templates to non-hardcoded paths that work out of the box in most environments, though :) I can confirm that PR #35430 does fix the issue in my monorepo templates, including the repo I linked - made a patch to apply this change. Is it worth to make an RFC to discuss supporting node module resolutions, isolated mode, and possibly plug and play (with |
What would be the suggested path for the codegen that works out of the box in most cases?
Yes it is 👍 or at least let's move the discussion to |
Summary: Fixes #35429 This fix is fairly straightforward. Instead of hardcoding two separate paths for this repo or a simple user's project, we ask Node to resolve `react-native-codegen`. Because `react-native-codegen` is moved [into the `/packages/*` folder](https://github.com/facebook/react-native/blob/main/package.json#L104), it's part of a workspace in this repository. That means that this fix works not only for monorepos in general but also resolves the right path within the react native repository. You can test this out yourself when running this command in either the root, or any other workspace in this repository: ```bash node --print "require('path').dirname(require.resolve('react-native-codegen/package.json'))" ``` I've tested this on Node 16 and 18, and seems to work nicely. Note that you _**have to specify `react-native-codegen/package.json`**_. That's because the `react-native-codegen` package itself is technically invalid; it's missing an entry point within the package (no `main`). When running `node --print "require.resolve('react-native-codegen')"` Node can't resolve the main entry point, and will fail during this process. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [iOS] [Fixed] - Fix incorrect codegen CLI paths in monorepo projects Pull Request resolved: #35430 Test Plan: See PR #35429 Reviewed By: cortinico Differential Revision: D41475878 Pulled By: cipolleschi fbshipit-source-id: f0c362b64cf9c3543a3a031d7eaf302c1314e3f0
Summary: Fixes facebook#35429 This fix is fairly straightforward. Instead of hardcoding two separate paths for this repo or a simple user's project, we ask Node to resolve `react-native-codegen`. Because `react-native-codegen` is moved [into the `/packages/*` folder](https://github.com/facebook/react-native/blob/main/package.json#L104), it's part of a workspace in this repository. That means that this fix works not only for monorepos in general but also resolves the right path within the react native repository. You can test this out yourself when running this command in either the root, or any other workspace in this repository: ```bash node --print "require('path').dirname(require.resolve('react-native-codegen/package.json'))" ``` I've tested this on Node 16 and 18, and seems to work nicely. Note that you _**have to specify `react-native-codegen/package.json`**_. That's because the `react-native-codegen` package itself is technically invalid; it's missing an entry point within the package (no `main`). When running `node --print "require.resolve('react-native-codegen')"` Node can't resolve the main entry point, and will fail during this process. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [iOS] [Fixed] - Fix incorrect codegen CLI paths in monorepo projects Pull Request resolved: facebook#35430 Test Plan: See PR facebook#35429 Reviewed By: cortinico Differential Revision: D41475878 Pulled By: cipolleschi fbshipit-source-id: f0c362b64cf9c3543a3a031d7eaf302c1314e3f0
Description
As mentioned in the partner sync, the current codegen scripts contain hardcoded paths which are incorrect in monorepo projects.
To demonstrate the issue, I updated my Expo monorepo example repository. You can see a failed build in this PR with the relevant xcode logging pointing to these hardcoded paths.
Core issue
In React Native's native files, we use quite some scripts to generate certain things. Paths to these scripts are often hard coded. These hardcoded paths are fine, but only within the
react-native
package.When hardcoding paths to other packages, like
react-native-codegen
, you could easily break general monorepo support. Some monorepo tools allow people to mark certain packages as "do not hoist". While this works for some cases, it's often not the best usage of monorepos as this would duplicate installations in bigger repos and consume more disk space. With that in mind, we basically can't assume the location of a package, we have have to ask Node to resolve it.Issue from the example repo
The linked example repository uses a relatively simple structure listed below. We hoist as many packages as possible to speed up installation and consume minimal disk space.
In this case, the path to react native codegen is incorrect, as the monorepo tool (pnpm) is installing codegen in the root node_modules folder.
Looking to npm & future
We might run into more issues in the future when the npm isolation mode RFC is accepted. It's probably worth it to think of solutions before that.
Using a similar but slightly different solution could even help make React Native compatible with Plug'n'Play modules. Instead of using hard-coded paths, and "expect files to be installed on disk", we could ask Node or the package manager to resolve the file locations. If all of these requests go through a Plug'n'Play manager, it could download those files on the fly, and return the location. The first run will be slow, but IMHO it's worth investigating.
Version
0.70.5
Output of
npx react-native info
Steps to reproduce
To trigger the issue:
$ git clone [email protected]:byCedric/expo-monorepo-example.git ./example
$ cd ./example
$ git checkout 0c94b0aa06e95a3ac9dcb87aff3bf3e3f767d7ac
(this is the latest onmain
without a patch)$ pnpm install
$ cd apps/mobile
$ npx expo prebuild --platform ios
$ xed ios
To test the fix from PR #73
$ git clone [email protected]:byCedric/expo-monorepo-example.git ./example
$ cd ./example
$ git checkout fix/codegen-monorepo-paths
(this is the latest onmain
without a patch)$ pnpm install
$ cd apps/mobile
$ npx expo prebuild --platform ios
$ xed ios
Snack, code example, screenshot, or link to a repository
https://github.com/byCedric/expo-monorepo-example
The text was updated successfully, but these errors were encountered: