From 8826dc073bc77491de37ddd2b714edb7b1f40fc4 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:13:39 +0100 Subject: [PATCH 1/2] fix: fix `@react-native-community/cli` not being found in pnpm setups --- packages/react-native/cli.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/react-native/cli.js b/packages/react-native/cli.js index 23a9f1779ac60a..3618a41cd7916e 100755 --- a/packages/react-native/cli.js +++ b/packages/react-native/cli.js @@ -23,6 +23,14 @@ const deprecated = () => { ); }; +function findCommunityCli(startDir = process.cwd()) { + // In a pnpm, we won't be able to find `@react-native-community/cli` starting + // from the `react-native` directory. Instead, we must use the project's root. + const options = { paths: [startDir] }; + const rncli = require.resolve('@react-native-community/cli', options); + return require(rncli); +} + function isMissingCliDependency(error) { return ( error.code === 'MODULE_NOT_FOUND' && @@ -217,7 +225,7 @@ async function main() { } try { - return require('@react-native-community/cli').run(name); + return findCommunityCli().run(name); } catch (e) { if (isMissingCliDependency(e)) { warnWithExplicitDependency(); @@ -231,7 +239,7 @@ if (require.main === module) { main(); } else { try { - cli = require('@react-native-community/cli'); + cli = findCommunityCli(); } catch (e) { // We silence @react-native-community/cli missing as it is no // longer a dependency From 0c4b65d31f891c566dc3d7c54695bdfa31167417 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:15:19 +0100 Subject: [PATCH 2/2] comment on cwd usage --- packages/react-native/cli.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/react-native/cli.js b/packages/react-native/cli.js index 3618a41cd7916e..5d7517746fad8c 100755 --- a/packages/react-native/cli.js +++ b/packages/react-native/cli.js @@ -24,8 +24,14 @@ const deprecated = () => { }; function findCommunityCli(startDir = process.cwd()) { - // In a pnpm, we won't be able to find `@react-native-community/cli` 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/cli` 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 options = { paths: [startDir] }; const rncli = require.resolve('@react-native-community/cli', options); return require(rncli);