fix: RNGP CCache compatibility (#54829)

Summary:
Invoking `Runtime.getRuntime().exec()` is not compatible with Gradle Configuration Cache and providers should be used instead.

This error hasn't surfaced yet due to fact that this branch of code is rarely hit as the users usually provide correct path to the CLI in their `app/build.gradle` file. I stumbled upon it accidentally when bumping RN in monorepo and the CLI path was no longer valid.

Re-opened due to failing tests in the previous PR https://github.com/facebook/react-native/issues/54801

## Changelog:

[ANDROID] [FIXED] - RNGP using node invocation non-compatible with Gradle Compilation Cache

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

Test Plan:
I tested it in a rnc-cli app, where I provided an invalid path for `cliFile` - the error was fixed after applying this patch.

`./gradlew -p packages/gradle-plugin/ test` now works properly.

Reviewed By: mdvacca

Differential Revision: D88757054

Pulled By: cortinico

fbshipit-source-id: 07b86f8cf4d242dee30b97b7768602ff9b60ed54
This commit is contained in:
Tomasz Żelawski authored and meta-codesync[bot] committed 2025-12-10 07:25:29 -08:00
1 parent 41c50fd20d
commit dd1f83e616
2 files changed
+13 -9

No files matched your search

@@ -42,6 +42,7 @@ internal fun detectedEntryFile(config: ReactExtension, envVariableOverride: Stri
*/
internal fun detectedCliFile(config: ReactExtension): File =
detectCliFile(
project = config.project,
reactNativeRoot = config.root.get().asFile,
preconfiguredCliFile = config.cliFile.asFile.orNull,
)
@@ -71,7 +72,11 @@ private fun detectEntryFile(
else -> File(reactRoot, "index.js")
}
private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): File {
private fun detectCliFile(
project: Project,
reactNativeRoot: File,
preconfiguredCliFile: File?,
): File {
// 1. preconfigured path
if (preconfiguredCliFile != null) {
if (preconfiguredCliFile.exists()) {
@@ -81,14 +86,12 @@ private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): F
// 2. node module path
val nodeProcess =
Runtime.getRuntime()
.exec(
arrayOf("node", "--print", "require.resolve('react-native/cli');"),
emptyArray(),
reactNativeRoot,
)
project.providers.exec { exec ->
exec.commandLine("node", "--print", "require.resolve('react-native/cli');")
exec.workingDir(reactNativeRoot)
}
val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() }
val nodeProcessOutput = nodeProcess.standardOutput.asText.get().trim()
if (nodeProcessOutput.isNotEmpty()) {
val nodeModuleCliJs = File(nodeProcessOutput)
@@ -14,6 +14,7 @@ import com.facebook.react.tests.OsRule
import com.facebook.react.tests.WithOs
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.gradle.process.ProcessExecutionException
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Assume.assumeTrue
import org.junit.Rule
@@ -99,7 +100,7 @@ class PathUtilsTest {
assertThat(actual.readText()).isEqualTo("<!-- nothing to see here -->")
}
@Test(expected = IllegalStateException::class)
@Test(expected = ProcessExecutionException::class)
fun detectedCliPath_failsIfNotFound() {
val project = ProjectBuilder.builder().build()
val extension = TestReactExtension(project)