From f8d8764e8f236e8495e7e5747bfe95162f3a165a Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Mon, 20 Feb 2023 05:52:20 -0800 Subject: [PATCH] RNGP - Fix defaults for PrivateReactExtension Summary: When building from source, the PrivateReactExtension is getting no defaults (or missing defaults). Specifically root should point to ../../ (as the build from source will originate from `./node_modules/react-native`). Without that root specified, all the subsequent paths are broken, specifically, the default being `../` causes the codegen to be searched inside: ``` project/node_modules/node_modules/react-native/codegen ``` which is broken Changelog: [Internal] [Changed] - RNGP - Fix defaults for PrivateReactExtension Reviewed By: cipolleschi Differential Revision: D43435590 fbshipit-source-id: 2ed5e26c1d63fd808fc2d559ea83d6d39d106ff6 --- .../react/internal/PrivateReactExtension.kt | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/internal/PrivateReactExtension.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/internal/PrivateReactExtension.kt index e0de3b1ed26281..056111f7f58800 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/internal/PrivateReactExtension.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/internal/PrivateReactExtension.kt @@ -25,11 +25,32 @@ abstract class PrivateReactExtension @Inject constructor(project: Project) { private val objects = project.objects - val root: DirectoryProperty = objects.directoryProperty() - - val reactNativeDir: DirectoryProperty = objects.directoryProperty() - - val nodeExecutableAndArgs: ListProperty = objects.listProperty(String::class.java) - - val codegenDir: DirectoryProperty = objects.directoryProperty() + val root: DirectoryProperty = + objects + .directoryProperty() + .convention( + // This is the default for the project root if the users hasn't specified anything. + // If the project is called "react-native-github" + // - We're inside the Github Repo -> root is defined by RN Tester (so no default + // needed) + // - We're inside an includedBuild as we're performing a build from source + // (then we're inside `node_modules/react-native`, so default should be ../../) + // If the project is called in any other name + // - We're inside a user project, so inside the ./android folder. Default should be + // ../ + // User can always override this default by setting a `root =` inside the template. + if (project.rootProject.name == "react-native-github") { + project.rootProject.layout.projectDirectory.dir("../../") + } else { + project.rootProject.layout.projectDirectory.dir("../") + }) + + val reactNativeDir: DirectoryProperty = + objects.directoryProperty().convention(root.dir("node_modules/react-native")) + + val nodeExecutableAndArgs: ListProperty = + objects.listProperty(String::class.java).convention(listOf("node")) + + val codegenDir: DirectoryProperty = + objects.directoryProperty().convention(root.dir("node_modules/@react-native/codegen")) }