Fix core autolinking not working on Windows (#45572)

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

0.75-rc.5 is currently broken on Windows.

This is due to us invoking `npx react-native-community/cli config` without
a `cmd /c` prefix.

This fixes it by using our function `windowsAwareCommandLine`.
The problem is that this required a lot of refactoring since that util was not available for the settings plugin.

Fixes #45403

Changelog:
[Internal] [Changed] - Fix core autolinking not working on Windows

Reviewed By: cipolleschi

Differential Revision: D60037587

fbshipit-source-id: eefeda7aafc43b9ce08f0f9225b0847fad2f46b7
This commit is contained in:
Nicola Corti 2024-07-23 04:41:54 -07:00 committed by Facebook GitHub Bot
parent 7d17ae4d3b
commit fedbe2d486
16 changed files with 55 additions and 6 deletions

View File

@ -153,6 +153,7 @@ jobs:
- packages/gradle-plugin/react-native-gradle-plugin/build/
- packages/gradle-plugin/settings-plugin/build/
- packages/gradle-plugin/shared/build/
- packages/gradle-plugin/shared-testutil/build/
- packages/react-native-codegen/lib/
# -------------------------

View File

@ -2,3 +2,4 @@ build/
react-native-gradle-plugin/build/
settings-plugin/build/
shared/build/
shared-testutil/build/

View File

@ -48,6 +48,7 @@ dependencies {
implementation(libs.javapoet)
testImplementation(libs.junit)
testImplementation(project(":shared-testutil"))
}
// We intentionally don't build for Java 17 as users will see a cryptic bytecode version

View File

@ -38,6 +38,7 @@ dependencies {
implementation(libs.javapoet)
testImplementation(libs.junit)
testImplementation(project(":shared-testutil"))
}
// We intentionally don't build for Java 17 as users will see a cryptic bytecode version

View File

@ -8,6 +8,7 @@
package com.facebook.react
import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.windowsAwareCommandLine
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
@ -25,6 +26,11 @@ abstract class ReactSettingsExtension @Inject constructor(val settings: Settings
private val outputFolder =
settings.layout.rootDirectory.file("build/generated/autolinking/").asFile
private val defaultConfigCommand: List<String> =
windowsAwareCommandLine(listOf("npx", "@react-native-community/cli", "config")).map {
it.toString()
}
/**
* Utility function to autolink libraries using an external command as source of truth.
*
@ -39,7 +45,7 @@ abstract class ReactSettingsExtension @Inject constructor(val settings: Settings
*/
@JvmOverloads
public fun autolinkLibrariesFromCommand(
command: List<String> = listOf("npx", "@react-native-community/cli", "config"),
command: List<String> = defaultConfigCommand,
workingDirectory: File? = settings.layout.rootDirectory.dir("../").asFile,
lockFiles: FileCollection =
settings.layout.rootDirectory

View File

@ -19,6 +19,7 @@ include(
":react-native-gradle-plugin",
":settings-plugin",
":shared",
":shared-testutil",
)
rootProject.name = "gradle-plugin-root"

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { alias(libs.plugins.kotlin.jvm) }
repositories { mavenCentral() }
group = "com.facebook.react"
dependencies { implementation(libs.junit) }
java { targetCompatibility = JavaVersion.VERSION_11 }
kotlin { jvmToolchain(17) }
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
apiVersion = "1.6"
jvmTarget = "11"
allWarningsAsErrors = true
}
}
tasks.withType<Test>().configureEach {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
}
}

View File

@ -18,6 +18,7 @@ dependencies {
implementation(libs.gson)
implementation(libs.guava)
testImplementation(libs.junit)
testImplementation(project(":shared-testutil"))
}
java { targetCompatibility = JavaVersion.VERSION_11 }

View File

@ -10,7 +10,7 @@ package com.facebook.react.utils
import com.facebook.react.utils.KotlinStdlibCompatUtils.lowercaseCompat
import java.io.File
internal object Os {
object Os {
fun isWindows(): Boolean =
System.getProperty("os.name")?.lowercaseCompat()?.contains("windows") ?: false

View File

@ -7,17 +7,16 @@
package com.facebook.react.utils
internal fun windowsAwareCommandLine(vararg args: Any): List<Any> =
windowsAwareCommandLine(args.toList())
fun windowsAwareCommandLine(vararg args: Any): List<Any> = windowsAwareCommandLine(args.toList())
internal fun windowsAwareCommandLine(args: List<Any>): List<Any> =
fun windowsAwareCommandLine(args: List<Any>): List<Any> =
if (Os.isWindows()) {
listOf("cmd", "/c") + args
} else {
args
}
internal fun windowsAwareBashCommandLine(
fun windowsAwareBashCommandLine(
vararg args: String,
bashWindowsHome: String? = null
): List<String> =