Add warning when newArchEnabled is set (#39781)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39781

## Changelog
[Internal] - Add warning when newArchEnabled is set

Reviewed By: cortinico

Differential Revision: D49866356

fbshipit-source-id: 0b937b7f37010e5fbfc93b073cc70e293d61052c
This commit is contained in:
Riccardo Cipolleschi
2023-10-05 08:26:43 -07:00
committed by Facebook GitHub Bot
parent 983bd14d99
commit bad027c5f5
3 changed files with 249 additions and 0 deletions
@@ -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<Project> {
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<Project> {
}
}
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(
@@ -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
}
@@ -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))
}
}