diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt index 6ab11a38c07..8f7c78043c5 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt @@ -22,6 +22,7 @@ import com.facebook.react.utils.JdkConfiguratorUtils.configureJavaToolChains import com.facebook.react.utils.JsonUtils import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson +import com.facebook.react.utils.ProjectUtils.shouldWarnIfNewArchFlagIsSetInPrealpha import com.facebook.react.utils.findPackageJsonFile import java.io.File import kotlin.system.exitProcess @@ -36,6 +37,7 @@ class ReactPlugin : Plugin { override fun apply(project: Project) { checkJvmVersion(project) val extension = project.extensions.create("react", ReactExtension::class.java, project) + checkIfNewArchFlagIsSet(project, extension) // We register a private extension on the rootProject so that project wide configs // like codegen config can be propagated from app project to libraries. @@ -104,6 +106,23 @@ class ReactPlugin : Plugin { } } + private fun checkIfNewArchFlagIsSet(project: Project, extension: ReactExtension) { + if (project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) { + project.logger.warn( + """ + + ******************************************************************************** + + WARNING: This version of React Native is ignoring the `newArchEnabled` flag you set. Please set it to true or remove it to suppress this warning. + + + ******************************************************************************** + + """ + .trimIndent()) + } + } + /** This function sets up `react-native-codegen` in our Gradle plugin. */ @Suppress("UnstableApiUsage") private fun configureCodegen( diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt index bc04a48c4b4..7c3003acf9a 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt @@ -28,9 +28,11 @@ internal object ProjectUtils { internal fun Project.isNewArchEnabled(extension: ReactExtension): Boolean { return (project.hasProperty(NEW_ARCH_ENABLED) && project.property(NEW_ARCH_ENABLED).toString().toBoolean()) || + (project.hasProperty(SCOPED_NEW_ARCH_ENABLED) && project.property(SCOPED_NEW_ARCH_ENABLED).toString().toBoolean()) || shouldEnableNewArchForReactNativeVersion(project.reactNativeDir(extension)) + } internal val Project.isHermesEnabled: Boolean @@ -114,4 +116,21 @@ internal object ProjectUtils { val major = matchResult.groupValues[1].toInt() return major > 0 && major < 1000 } + + internal fun Project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension: ReactExtension): Boolean { + + val propertySetToFalse = + (this.hasPropertySetToFalse(NEW_ARCH_ENABLED)) || + (this.hasPropertySetToFalse(SCOPED_NEW_ARCH_ENABLED)) + + + val shouldEnableNewArch = + shouldEnableNewArchForReactNativeVersion(this.reactNativeDir(extension)) + + return shouldEnableNewArch && propertySetToFalse + } + + internal fun Project.hasPropertySetToFalse(property: String): Boolean = + this.hasProperty(property) && this.property(property).toString().toBoolean() == false + } diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt index 3d8fd6e1d48..f2f5063f447 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt @@ -15,6 +15,7 @@ import com.facebook.react.utils.ProjectUtils.getReactNativeArchitectures import com.facebook.react.utils.ProjectUtils.isHermesEnabled import com.facebook.react.utils.ProjectUtils.isNewArchEnabled import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson +import com.facebook.react.utils.ProjectUtils.shouldWarnIfNewArchFlagIsSetInPrealpha import java.io.File import org.junit.Assert.* import org.junit.Rule @@ -319,4 +320,214 @@ class ProjectUtilsTest { assertEquals("x86", archs[2]) assertEquals("x86_64", archs[3]) } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToFalseAndOnMajor_returnTrue() { + val project = createProject() + project.extensions.extraProperties.set("newArchEnabled", "false") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertTrue(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToFalseAndOnMajor_returnTrue() { + val project = createProject() + project.extensions.extraProperties.set("react.newArchEnabled", "false") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertTrue(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToFalseAndOnMajor_returnTrue() { + val project = createProject() + project.extensions.extraProperties.set("newArchEnabled", "false") + project.extensions.extraProperties.set("react.newArchEnabled", "false") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertTrue(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("newArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("react.newArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("newArchEnabled", "true") + project.extensions.extraProperties.set("react.newArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndOnMajor_returnFalse() { + val project = createProject() + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "1.2.3" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndNotOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("newxArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "0.73.0" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndNotOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("react.newxArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "0.73.0" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndNotOnMajor_returnFalse() { + val project = createProject() + project.extensions.extraProperties.set("newArchEnabled", "true") + project.extensions.extraProperties.set("react.newxArchEnabled", "true") + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "0.73.0" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } + + @Test + fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndNotOnMajor_returnFalse() { + val project = createProject() + val extension = TestReactExtension(project) + File(tempFolder.root, "package.json").apply { + writeText( + // language=json + """ + { + "version": "0.73.0" + } + """ + .trimIndent()) + } + extension.reactNativeDir.set(tempFolder.root) + assertFalse(project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension)) + } }