From 52c5c94d8a407d70279a7689b7aa8527b3c0aa2d Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:52:49 +0100 Subject: [PATCH 1/2] fix: fix `@react-native-community/cli-platform-*` packages not being found in pnpm setups --- packages/react-native/react-native.config.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 3b572bb1e9c6d6..2d67386b18c1c4 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -20,9 +20,17 @@ const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native'); +function findCommunityPlatformPackage(spec, startDir = process.cwd()) { + // In a pnpm setup, we won't be able to find `@react-native-community/*` + // starting from the `react-native` directory. Instead, we must use the + // project's root. + const main = require.resolve(spec, { paths: [startDir] }); + return require(main); +} + let android; try { - android = require('@react-native-community/cli-platform-android'); + android = findCommunityPlatformPackage('@react-native-community/cli-platform-android'); } catch { if (verbose) { console.warn( @@ -33,7 +41,7 @@ try { let ios; try { - ios = require('@react-native-community/cli-platform-ios'); + ios = findCommunityPlatformPackage('@react-native-community/cli-platform-ios'); } catch { if (verbose) { console.warn( From 3e5609582defae87d6d546edcd3dc4fa4515ae93 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:20:02 +0100 Subject: [PATCH 2/2] comment on cwd usage --- packages/react-native/react-native.config.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 2d67386b18c1c4..077ad1cac0630e 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -21,9 +21,14 @@ const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native'); function findCommunityPlatformPackage(spec, startDir = process.cwd()) { - // In a pnpm setup, we won't be able to find `@react-native-community/*` - // starting from the `react-native` directory. Instead, we must use the - // project's root. + // In monorepos, we cannot make any assumptions on where + // `@react-native-community/*` gets installed. The safest way to find it + // (barring adding an optional peer dependency) is to start from the project + // root. + // + // Note that we're assuming that the current working directory is the project + // root. This is also what `@react-native-community/cli` assumes (see + // https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts). const main = require.resolve(spec, { paths: [startDir] }); return require(main); }