Skip to content

Commit dd1f83e

Browse files
tjzelmeta-codesync[bot]
authored andcommitted
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 #54801 ## Changelog: [ANDROID] [FIXED] - RNGP using node invocation non-compatible with Gradle Compilation Cache Pull Request resolved: #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
1 parent 41c50fd commit dd1f83e

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

  • packages/gradle-plugin/react-native-gradle-plugin/src

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ internal fun detectedEntryFile(config: ReactExtension, envVariableOverride: Stri
4242
*/
4343
internal fun detectedCliFile(config: ReactExtension): File =
4444
detectCliFile(
45+
project = config.project,
4546
reactNativeRoot = config.root.get().asFile,
4647
preconfiguredCliFile = config.cliFile.asFile.orNull,
4748
)
@@ -71,7 +72,11 @@ private fun detectEntryFile(
7172
else -> File(reactRoot, "index.js")
7273
}
7374

74-
private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): File {
75+
private fun detectCliFile(
76+
project: Project,
77+
reactNativeRoot: File,
78+
preconfiguredCliFile: File?,
79+
): File {
7580
// 1. preconfigured path
7681
if (preconfiguredCliFile != null) {
7782
if (preconfiguredCliFile.exists()) {
@@ -81,14 +86,12 @@ private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): F
8186

8287
// 2. node module path
8388
val nodeProcess =
84-
Runtime.getRuntime()
85-
.exec(
86-
arrayOf("node", "--print", "require.resolve('react-native/cli');"),
87-
emptyArray(),
88-
reactNativeRoot,
89-
)
90-
91-
val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() }
89+
project.providers.exec { exec ->
90+
exec.commandLine("node", "--print", "require.resolve('react-native/cli');")
91+
exec.workingDir(reactNativeRoot)
92+
}
93+
94+
val nodeProcessOutput = nodeProcess.standardOutput.asText.get().trim()
9295

9396
if (nodeProcessOutput.isNotEmpty()) {
9497
val nodeModuleCliJs = File(nodeProcessOutput)

packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.facebook.react.tests.OsRule
1414
import com.facebook.react.tests.WithOs
1515
import java.io.File
1616
import org.assertj.core.api.Assertions.assertThat
17+
import org.gradle.process.ProcessExecutionException
1718
import org.gradle.testfixtures.ProjectBuilder
1819
import org.junit.Assume.assumeTrue
1920
import org.junit.Rule
@@ -99,7 +100,7 @@ class PathUtilsTest {
99100
assertThat(actual.readText()).isEqualTo("<!-- nothing to see here -->")
100101
}
101102

102-
@Test(expected = IllegalStateException::class)
103+
@Test(expected = ProcessExecutionException::class)
103104
fun detectedCliPath_failsIfNotFound() {
104105
val project = ProjectBuilder.builder().build()
105106
val extension = TestReactExtension(project)

0 commit comments

Comments
 (0)