Skip to content

Commit 7926d65

Browse files
tido64facebook-github-bot
authored andcommitted
fix: fix @react-native-community/cli-platform-* packages not being 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
1 parent d08f9ac commit 7926d65

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/react-native/react-native.config.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,24 @@
2020

2121
const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native');
2222

23+
function findCommunityPlatformPackage(spec, startDir = process.cwd()) {
24+
// In monorepos, we cannot make any assumptions on where
25+
// `@react-native-community/*` gets installed. The safest way to find it
26+
// (barring adding an optional peer dependency) is to start from the project
27+
// root.
28+
//
29+
// Note that we're assuming that the current working directory is the project
30+
// root. This is also what `@react-native-community/cli` assumes (see
31+
// https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts).
32+
const main = require.resolve(spec, {paths: [startDir]});
33+
return require(main);
34+
}
35+
2336
let android;
2437
try {
25-
android = require('@react-native-community/cli-platform-android');
38+
android = findCommunityPlatformPackage(
39+
'@react-native-community/cli-platform-android',
40+
);
2641
} catch {
2742
if (verbose) {
2843
console.warn(
@@ -33,7 +48,9 @@ try {
3348

3449
let ios;
3550
try {
36-
ios = require('@react-native-community/cli-platform-ios');
51+
ios = findCommunityPlatformPackage(
52+
'@react-native-community/cli-platform-ios',
53+
);
3754
} catch {
3855
if (verbose) {
3956
console.warn(

0 commit comments

Comments
 (0)