Skip to content

Commit 4a4ccee

Browse files
byCedricfacebook-github-bot
authored andcommitted
Fix hardcoded path to codegen cli for monorepos (#35430)
Summary: Fixes #35429 This fix is fairly straightforward. Instead of hardcoding two separate paths for this repo or a simple user's project, we ask Node to resolve `react-native-codegen`. Because `react-native-codegen` is moved [into the `/packages/*` folder](https://github.com/facebook/react-native/blob/main/package.json#L104), it's part of a workspace in this repository. That means that this fix works not only for monorepos in general but also resolves the right path within the react native repository. You can test this out yourself when running this command in either the root, or any other workspace in this repository: ```bash node --print "require('path').dirname(require.resolve('react-native-codegen/package.json'))" ``` I've tested this on Node 16 and 18, and seems to work nicely. Note that you _**have to specify `react-native-codegen/package.json`**_. That's because the `react-native-codegen` package itself is technically invalid; it's missing an entry point within the package (no `main`). When running `node --print "require.resolve('react-native-codegen')"` Node can't resolve the main entry point, and will fail during this process. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [iOS] [Fixed] - Fix incorrect codegen CLI paths in monorepo projects Pull Request resolved: #35430 Test Plan: See PR #35429 Reviewed By: cortinico Differential Revision: D41475878 Pulled By: cipolleschi fbshipit-source-id: f0c362b64cf9c3543a3a031d7eaf302c1314e3f0
1 parent 4c5eb8d commit 4a4ccee

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

scripts/react_native_pods_utils/script_phases.sh

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ GENERATED_SCHEMA_FILE="$GENERATED_SRCS_DIR/schema.json"
1313

1414
cd "$RCT_SCRIPT_RN_DIR"
1515

16-
CODEGEN_REPO_PATH="$RCT_SCRIPT_RN_DIR/packages/react-native-codegen"
17-
CODEGEN_NPM_PATH="$RCT_SCRIPT_RN_DIR/../react-native-codegen"
1816
CODEGEN_CLI_PATH=""
1917

2018
error () {
@@ -23,15 +21,6 @@ error () {
2321
exit 1
2422
}
2523

26-
# Determine path to react-native-codegen
27-
if [ -d "$CODEGEN_REPO_PATH" ]; then
28-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
29-
elif [ -d "$CODEGEN_NPM_PATH" ]; then
30-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
31-
else
32-
error "error: Could not determine react-native-codegen location in $CODEGEN_REPO_PATH or $CODEGEN_NPM_PATH. Try running 'yarn install' or 'npm install' in your project root."
33-
fi
34-
3524
find_node () {
3625
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
3726
if [ -z "$NODE_BINARY" ]; then
@@ -41,6 +30,13 @@ find_node () {
4130
fi
4231
}
4332

33+
find_codegen () {
34+
CODEGEN_CLI_PATH=$("$NODE_BINARY" --print "require('path').dirname(require.resolve('react-native-codegen/package.json'))")
35+
if ! [ -d "$CODEGEN_CLI_PATH" ]; then
36+
error "error: Could not determine react-native-codegen location, using node module resolution. Try running 'yarn install' or 'npm install' in your project root."
37+
fi
38+
}
39+
4440
setup_dirs () {
4541
set +e
4642
rm -rf "$GENERATED_SRCS_DIR"
@@ -116,13 +112,15 @@ moveOutputs () {
116112
withCodgenDiscovery () {
117113
setup_dirs
118114
find_node
115+
find_codegen
119116
generateArtifacts
120117
moveOutputs
121118
}
122119

123120
noCodegenDiscovery () {
124121
setup_dirs
125122
find_node
123+
find_codegen
126124
generateCodegenSchemaFromJavaScript
127125
generateCodegenArtifactsFromSchema
128126
moveOutputs

0 commit comments

Comments
 (0)