From dd1f83e61656f21769bd66b28a4c389d6ec31a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Wed, 10 Dec 2025 07:25:29 -0800 Subject: [PATCH] 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 --- .../com/facebook/react/utils/PathUtils.kt | 19 +++++++++++-------- .../com/facebook/react/utils/PathUtilsTest.kt | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt index 510b157e6b1..fc5a9a63a7c 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt @@ -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) diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt index e4c9f599765..47b2d15ca80 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt @@ -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("") } - @Test(expected = IllegalStateException::class) + @Test(expected = ProcessExecutionException::class) fun detectedCliPath_failsIfNotFound() { val project = ProjectBuilder.builder().build() val extension = TestReactExtension(project)