fix: RNGP using node invocation non-compatible with Gradle Compilation Cache (#54801)

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.

## Changelog:

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

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

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.

Reviewed By: mdvacca, huntie

Differential Revision: D88492310

Pulled By: cortinico

fbshipit-source-id: a91e47b90937945066bde938bdf16b2550322afb
This commit is contained in:
Tomasz Żelawski authored and meta-codesync[bot] committed 2025-12-08 03:37:13 -08:00
1 parent 38513f72d7
commit dc73ec438a
1 file changed
+10 -8
@@ -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,11 @@ 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 {
it.commandLine("node", "--print", "require.resolve('react-native/package.json')")
}
val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() }
val nodeProcessOutput = nodeProcess.standardOutput.asText.get().trim()
if (nodeProcessOutput.isNotEmpty()) {
val nodeModuleCliJs = File(nodeProcessOutput)