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

Fix hardcoded path to codegen cli for monorepos #35430

Closed
wants to merge 1 commit into from

Conversation

byCedric
Copy link
Contributor

@byCedric byCedric commented Nov 22, 2022

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, 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:

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

[iOS] [Fixed] - Fix incorrect codegen CLI paths in monorepo projects

Test Plan

See PR #35429

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Expo Partner: Expo Partner labels Nov 22, 2022
@cipolleschi
Copy link
Contributor

Hi @byCedric, thank you so much for the contribution. Could you log into https://app.circleci.com, please?
This will renew the GitHub token that CircleCI knows and it will start the CI pipelines. Thank you so much. 🙏

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@byCedric
Copy link
Contributor Author

byCedric commented Nov 22, 2022

Hi @byCedric, thank you so much for the contribution. Could you log into https://app.circleci.com, please? This will renew the GitHub token that CircleCI knows and it will start the CI pipelines. Thank you so much. 🙏

No problem @cipolleschi :) I just authenticated, but it might be related to me creating this PR from a fork? It also seems that I can't manually trigger a restart from CircleCI.

Either way, there is one thing that might be good to add to this fix. Technically, for isolated modules, this fix is not 100% correct. In isolation mode, the current directory where node --print is being executed matters. Because react-native-codegen is a child/dependency of react-native, it should be resolved from the react-native dir, not the project's ios folder. (see additional comment here)

But this is sufficient for the current package managers :)

@cipolleschi
Copy link
Contributor

/rebase

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,102,569 +0
android hermes armeabi-v7a 6,470,637 +0
android hermes x86 7,520,037 +0
android hermes x86_64 7,378,724 +0
android jsc arm64-v8a 8,967,444 +0
android jsc armeabi-v7a 7,698,293 +0
android jsc x86 9,029,584 +0
android jsc x86_64 9,507,429 +0

Base commit: e11cfe9
Branch: main

@analysis-bot
Copy link

analysis-bot commented Nov 24, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 9517320
Branch: main

@pull-bot
Copy link

PR build artifact for 189b5cb is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@pull-bot
Copy link

PR build artifact for 189b5cb is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@kelset
Copy link
Contributor

kelset commented Nov 24, 2022

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.

can't this be addressed in this PR? I am not aware of any reasons why the package.json of codegen can't have the main field set

@byCedric
Copy link
Contributor Author

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.

can't this be addressed in this PR? I am not aware of any reasons why the package.json of codegen can't have the main field set

I think it's a side-effect worth mentioning but not necessarily required to fix. We'd probably always want to resolve the <pkg>/package.json, because that's the only file that is guaranteed to be resolved in the "root of the package".

If you have an entry point like "main": "dist/index.js", node resolves require.resolve('<pkg>') to something like .../node_modules/<pkg>/dist/index.js, which isn't the package installation directory. You can see this in the Expo template files.

But maybe just adding a main stub, or even exports to the files that are relevant, might make it a bit nicer in general :)

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@peterpme
Copy link

Thank you @byCedric for the PR and thanks to everyone else for reviewing it :) Happy Thanksgiving!

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @byCedric in 4a4ccee.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 24, 2022
kelset pushed a commit that referenced this pull request Nov 30, 2022
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
@byCedric byCedric deleted the fix/codegen-monorepo-paths branch December 8, 2022 14:33
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Expo Partner: Expo Partner Platform: iOS iOS applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Codegen on iOS is failing in monorepo projects
9 participants