Uh oh!
There was an error while loading. Please reload this page.
fix: fix @react-native-community/cli not being found in monorepos - #47304
fix: fix @react-native-community/cli not being found in monorepos#47304tido64 wants to merge 2 commits into
@react-native-community/cli not being found in monorepos#47304Conversation
facebook-github-bot
commented
Oct 30, 2024
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
| ); | ||
| }; | ||
| function findCommunityCli(startDir = process.cwd()) { |
There was a problem hiding this comment.
What is process.cwd() in this context? It seems fragile to assume that's where the dependency is declared.
There was a problem hiding this comment.
It's where the react-native command is being executed from, which should be project root. I am not familiar with use cases where it's being executed elsewhere.
blakef
commented
Oct 30, 2024
@tido64 how do I reproduce this issue locally? I'm trying to understand what's going on with pnpm. |
tido64
commented
Oct 30, 2024
Follow the repro steps in the description. |
blakef
commented
Oct 31, 2024
@tido64 thanks for working with me on this one. I wonder if we can tweak this approach slightly. The way I could reproduce this locally was with Yarn Berry using the pnpm nodeLinker. As a more general solution, we can look at updating the functionyarnPnpmWorkaround(){constproject=process.cwd();if(module.paths.indexOf(project)===-1){module.paths.push(project)}}This way we get the default module lookup paths, but add the workaround for the issue you're seeing in monorepos with pnpm linking. We could do more to only apply this when needed? constisYarnBerryPnpm=()=>/yarn\/[^1]/.test(process.env.npm_config_user_data)&&execSync('yarn config get nodeLinker').toString().trim()==='pnpm');constisPnpm=()=>/pnpm\//.test(process.env.npm_config_user_data)constmustUpdatePaths=isPnpm()||isYarnBerryPnpm();functionupdatePaths(){if(!mustUpdatePaths)return;yarnPnpmWorkaround();}@robhogan thoughts? |
tido64
commented
Oct 31, 2024
I think this is a big no-no. This changes the paths for everything. There are too many side-effects that can occur. Monorepos with a pnpm setup can reproduce this fairly consistently, but even with "normal" node_modules layout, there are no guarantees for how a package is hoisted. If there are multiple versions of CLI, but only one of RN, Yarn can (and probably will) hoist only RN to repo root, while leaving CLI in the individual packages. You'll run into the same issues there. |
IMO the original approach (assuming If we wanted to pick this into 0.76 it'd have to be non-breaking, and the safest way to do that is to fall back to a cwd-based resolution only if the assume-hoisted resolution fails (eg, (The blessed way to do this is theoretically with optional peer dependencies, but I don't know what the state of support is amongst package managers these days, and afaik |
tido64
commented
Nov 1, 2024
Just so I don't misunderstand, you want this change in |
@react-native-community/cli not being found in pnpm setups@react-native-community/cli not being found in monorepos
I think it makes sense to go in My thinking is to put
Does that seem reasonable? |
tido64
commented
Nov 1, 2024
My reason for going with cwd first is to avoid the scenario where we have multiple versions of React Native in a monorepo and the layout looks something like this: We've hit similar scenarios before (albeit with Metro). |
Cool, that makes sense. Update to that please. Update: Discussed with @robhogan some more. If you update to the equivalent of this: try{returnrequire('@react-native-community/cli');}catch{returnrequire(require.resolve('@react-native-community/cli',{paths: [startDir]}));}and target |
a2bf26d to
0c4b65dCompare…found in monorepos (#47308) Summary: Fix `react-native-community/cli-platform-*` packages not being found in monorepos. Note that we are making the assumption that `process.cwd()` returns the project root. This is the same assumption that `react-native-community/cli` makes. Specifically, `findProjectRoot()` has an optional argument that defaults to `process.cwd()`: - [`findProjectRoot()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts) - Which gets called without arguments in [`loadConfig()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli-config/src/loadConfig.ts#L89) - `loadConfig()` gets called from [`setupAndRun()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli/src/index.ts#L196), also without project root set As far as I can see, the project root argument is only ever used in tests. ## Changelog: [GENERAL] [FIXED] - Fix `react-native-community/cli-platform-*` packages not being found in monorepos Pull Request resolved: #47308 Test Plan: 1. Clone/check out this branch: microsoft/rnx-kit#3409 2. Cherry-pick #47304 3. Cherry-pick #47308 4. Run `react-native config` inside `packages/test-app` 5. Verify that `projects` is populated **Before:** ```js "healthChecks": [], "platforms": {}, "assets": [], "project": {} } ``` **After:** ```js "healthChecks": [], "platforms": { "ios": {}, "android": {} }, "assets": [], "project": { "ios": { "sourceDir": "/~/packages/test-app/ios", "xcodeProject": { "name": "SampleCrossApp.xcworkspace", "path": ".", "isWorkspace": true }, "automaticPodsInstallation": false, "assets": [] }, "android": { "sourceDir": "/~/packages/test-app/android", "appName": "app", "packageName": "com.msft.identity.client.sample.local", "applicationId": "com.msft.identity.client.sample.local", "mainActivity": "com.microsoft.reacttestapp.MainActivity", "assets": [] } } } ``` Reviewed By: cortinico Differential Revision: D69465533 Pulled By: robhogan fbshipit-source-id: 3d6cf32752a7a41d9c7e84f35b0f26ae7d7a971f
tido64
commented
Feb 12, 2025
@robhogan Could you also have a look at this again? Thanks ❤️ |
facebook-github-bot
commented
Feb 14, 2025
react-native-bot
commented
Feb 14, 2025
This pull request was successfully merged by @tido64 in a672a4d When will my fix make it into a release? | How to file a pick request? |
…#47304) Summary: Fix `react-native-community/cli` not being found in pnpm setups ## Changelog: [GENERAL] [FIXED] - Fix `react-native-community/cli` not being found in pnpm setups Pull Request resolved: #47304 Test Plan: 1. Clone/check out this branch: microsoft/rnx-kit#3409 2. Run `yarn react-native config` Reviewed By: cortinico Differential Revision: D65209065 Pulled By: robhogan fbshipit-source-id: 2ceb73ad140b4afe193e879779c2d8a4b9adf3fc
react-native-bot
commented
Mar 18, 2025
This pull request was successfully merged by @tido64 in 9ba96ad When will my fix make it into a release? | How to file a pick request? |
…found in monorepos (#47308) Summary: Fix `react-native-community/cli-platform-*` packages not being found in monorepos. Note that we are making the assumption that `process.cwd()` returns the project root. This is the same assumption that `react-native-community/cli` makes. Specifically, `findProjectRoot()` has an optional argument that defaults to `process.cwd()`: - [`findProjectRoot()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts) - Which gets called without arguments in [`loadConfig()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli-config/src/loadConfig.ts#L89) - `loadConfig()` gets called from [`setupAndRun()`](https://github.com/react-native-community/cli/blob/14.x/packages/cli/src/index.ts#L196), also without project root set As far as I can see, the project root argument is only ever used in tests. ## Changelog: [GENERAL] [FIXED] - Fix `react-native-community/cli-platform-*` packages not being found in monorepos Pull Request resolved: #47308 Test Plan: 1. Clone/check out this branch: microsoft/rnx-kit#3409 2. Cherry-pick #47304 3. Cherry-pick #47308 4. Run `react-native config` inside `packages/test-app` 5. Verify that `projects` is populated **Before:** ```js "healthChecks": [], "platforms": {}, "assets": [], "project": {} } ``` **After:** ```js "healthChecks": [], "platforms": { "ios": {}, "android": {} }, "assets": [], "project": { "ios": { "sourceDir": "/~/packages/test-app/ios", "xcodeProject": { "name": "SampleCrossApp.xcworkspace", "path": ".", "isWorkspace": true }, "automaticPodsInstallation": false, "assets": [] }, "android": { "sourceDir": "/~/packages/test-app/android", "appName": "app", "packageName": "com.msft.identity.client.sample.local", "applicationId": "com.msft.identity.client.sample.local", "mainActivity": "com.microsoft.reacttestapp.MainActivity", "assets": [] } } } ``` Reviewed By: cortinico Differential Revision: D69465533 Pulled By: robhogan fbshipit-source-id: 3d6cf32752a7a41d9c7e84f35b0f26ae7d7a971f
Summary:
Fix
@react-native-community/clinot being found in monorepos even though it is installed.Note that we are making the assumption that
process.cwd()returns the project root. This is the same assumption that@react-native-community/climakes. Specifically,findProjectRoot()has an optional argument that defaults toprocess.cwd():findProjectRoot()loadConfig()loadConfig()gets called fromsetupAndRun(), also without project root setAs far as I can see, the project root argument is only ever used in tests.
Changelog:
[GENERAL] [FIXED] - Fix
@react-native-community/clinot being found in monoreposTest Plan:
react-nativeto 0.76 microsoft/rnx-kit#3409yarn react-native configBefore:
After: The usual output of
react-native config