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

Monorepo Support: Use node resolver for react native location #2337

Conversation

morganick
Copy link
Contributor

@morganick morganick commented Sep 6, 2024

Description

Monorepos are configured and structured on what works best for the project. Since this can vary from project to project, having a dynamic way to find dependencies will make this work for more projects. Currently, the repo only supports certain configurations of Monorepo structures. This PR adopts a similar approach as Expo to locate the react native package within the project. Expo usage for reference

Changes

  • Updates build.gradle to have node resolve the react native package directory
  • Updates CONTRIBUTING.md to correct setup step as yarn prepare does not exist inside of react-navigation submodule.

Test code and steps to reproduce

  1. Put a println statement in the build.gradle file to examine the output of reactNativePackage.parentFile
  2. Build Example app for android
  3. Build FabricExample app for android
  4. Both apps build successfully.

Checklist

@morganick morganick marked this pull request as ready for review September 6, 2024 20:49

// Use node resolver to locate react-native package
def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, projectDir).text.trim())
if (reactNativePackage.exists()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inspect with:

println reactNativePackage.parentFile

You'll then see this output in the terminal when you run yarn android again.

@morganick
Copy link
Contributor Author

I'm able to run the same command locally that is failing in CI. I've also dropped back to node 18.20.4 locally to see if that made a difference. Everything continues to build normally.

It appears that for some reason projectDir is null on CI. I'd like to try running this with rootDir as it continues to work locally with that. rootDir resolves to the android directory in each of the Example and FabricExample projects.

@kkafar kkafar self-requested a review September 12, 2024 07:48
@kkafar kkafar self-assigned this Sep 25, 2024
Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

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

Hello, I've finally found time to test this, thank you for the contribution 🎉 We will surely merge this!

First, however, I need to articulate my confusion regarding differences between old and new approach.

Honestly I do not understand, why does the current main code even work. In context of react-native-screens, $projectDir in "regular client react-native application" resolves to App/node_modules/react-native-screens/android, thus "$projectDir/../node_modules/react-native/android" resolves to App/node_modules/react-native-screens/node_modules/react-native/android => This look like we've been pointing to wrong react-native module over the last couple of years!? What's even more baffling is that when you inspect App/node_modules/react-native-screens/node_modules/react-native it surely does not contain android subdirectory... I'm sure there are mechanisms I'm just now aware now as of right now, but that's just crazy ;D

Okay, so looking at the solution you propose I think we're fine on the first part, and the fallback to "new-old" approach with rootDir instead of projectDir also does make more sense than starting point old approach, at least for now.

I've tested this on:

  1. newly created standard react native app & it seems to be working,
  2. our examples in the library repository - they work just fine.

Only remark that my purist brain brings up is that maybe we should first look for "standard" application structure and if it fails, just then try to handle other cases with fallback code. Basically swap the cases around, so that we do not run whole search algorithm if it is not needed.

I'm thinking this through right now & I believe such swap won't break any behaviour you expect in monorepo, hence I'll allow myself to change it. Let me know, if I'm in wrong and the order is important.

There is one more thing to do: revert changes to guides/CONTRIBUTING.md file. You are right - these guidelines are wrong, however your suggestion is also wrong, despite the fact that yarn prepare commands exists in our repo :D I'll fix this in separate PR.

Since I can't push to your fork I'll close this PR, cherry-pick your changes and open a new one.

@kkafar
Copy link
Member

kkafar commented Sep 25, 2024

Surrogate PR: #2352

kkafar added a commit that referenced this pull request Sep 25, 2024
## Description

Based on:
#2337 by
@morganick.
Please see the original PR and its description for details.

See:
#2337 (review)
for discussion why new PR has been created.

## Changes

- **Using node resolver to find react native package for better monorepo
support**
- **Using rootDir instead of projectDir**
- **Change the order of path lookup**

## Test code and steps to reproduce

I've tested it using our both example apps and additionally I've created
fresh RN app
and tested the Android build there.


## Checklist

- [x] Ensured that CI passes

---------

Co-authored-by: Nick Morgan <[email protected]>
ja1ns pushed a commit to WiseOwlTech/react-native-screens that referenced this pull request Oct 9, 2024
## Description

Based on:
software-mansion#2337 by
@morganick.
Please see the original PR and its description for details.

See:
software-mansion#2337 (review)
for discussion why new PR has been created.

## Changes

- **Using node resolver to find react native package for better monorepo
support**
- **Using rootDir instead of projectDir**
- **Change the order of path lookup**

## Test code and steps to reproduce

I've tested it using our both example apps and additionally I've created
fresh RN app
and tested the Android build there.


## Checklist

- [x] Ensured that CI passes

---------

Co-authored-by: Nick Morgan <[email protected]>
kkafar added a commit that referenced this pull request Oct 25, 2024
## Description

Based on:
#2337 by
@morganick.
Please see the original PR and its description for details.

See:
#2337 (review)
for discussion why new PR has been created.

## Changes

- **Using node resolver to find react native package for better monorepo
support**
- **Using rootDir instead of projectDir**
- **Change the order of path lookup**

## Test code and steps to reproduce

I've tested it using our both example apps and additionally I've created
fresh RN app
and tested the Android build there.

## Checklist

- [x] Ensured that CI passes

---------

Co-authored-by: Nick Morgan <[email protected]>
(cherry picked from commit 408112a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants