Skip to content

Commit d9a77e1

Browse files
cortinicofacebook-github-bot
authored andcommitted
RNGP - Do not access project. during task execution. (#48745)
Summary: Pull Request resolved: #48745 This is the first step in a series of diff to make RNGP more Gradle-compliant (specifically for the sake of configuration caching). Specifically the problem in those 2 tasks is that we're accessing `project.copy()` and other functions from the `project` field. The project should never be accessed at execution time. See more on this here: https://docs.gradle.org/8.12/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution This diff fixes it. Changelog: [Internal] [Changed] - Reviewed By: cipolleschi Differential Revision: D68282777 fbshipit-source-id: 6d474f266b5bc50fba57c8cd478173c995864bbc
1 parent 6f6a438 commit d9a77e1

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/PrepareBoostTask.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
package com.facebook.react.tasks.internal
99

1010
import java.io.File
11+
import javax.inject.Inject
1112
import org.gradle.api.DefaultTask
1213
import org.gradle.api.file.ConfigurableFileCollection
1314
import org.gradle.api.file.DirectoryProperty
15+
import org.gradle.api.file.FileSystemOperations
1416
import org.gradle.api.provider.Property
1517
import org.gradle.api.tasks.*
1618

@@ -21,16 +23,19 @@ import org.gradle.api.tasks.*
2123
abstract class PrepareBoostTask : DefaultTask() {
2224

2325
@get:InputFiles abstract val boostPath: ConfigurableFileCollection
26+
@get:InputDirectory abstract val boostThirdPartyJniPath: DirectoryProperty
2427

2528
@get:Input abstract val boostVersion: Property<String>
2629

2730
@get:OutputDirectory abstract val outputDir: DirectoryProperty
2831

32+
@get:Inject abstract val fs: FileSystemOperations
33+
2934
@TaskAction
3035
fun taskAction() {
31-
project.copy { it ->
36+
fs.copy { it ->
3237
it.from(boostPath)
33-
it.from(project.file("src/main/jni/third-party/boost"))
38+
it.from(boostThirdPartyJniPath)
3439
it.include(
3540
"CMakeLists.txt",
3641
"boost_${boostVersion.get()}/boost/**/*.hpp",

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/PrepareGlogTask.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
package com.facebook.react.tasks.internal
99

1010
import java.io.File
11+
import javax.inject.Inject
1112
import org.apache.tools.ant.filters.ReplaceTokens
1213
import org.gradle.api.DefaultTask
1314
import org.gradle.api.file.ConfigurableFileCollection
1415
import org.gradle.api.file.DirectoryProperty
1516
import org.gradle.api.file.DuplicatesStrategy
17+
import org.gradle.api.file.FileSystemOperations
1618
import org.gradle.api.provider.Property
1719
import org.gradle.api.tasks.*
1820

@@ -23,16 +25,18 @@ import org.gradle.api.tasks.*
2325
abstract class PrepareGlogTask : DefaultTask() {
2426

2527
@get:InputFiles abstract val glogPath: ConfigurableFileCollection
26-
28+
@get:InputDirectory abstract val glogThirdPartyJniPath: DirectoryProperty
2729
@get:Input abstract val glogVersion: Property<String>
2830

2931
@get:OutputDirectory abstract val outputDir: DirectoryProperty
3032

33+
@get:Inject abstract val fs: FileSystemOperations
34+
3135
@TaskAction
3236
fun taskAction() {
33-
project.copy { action ->
37+
fs.copy { action ->
3438
action.from(glogPath)
35-
action.from(project.file("src/main/jni/third-party/glog/"))
39+
action.from(glogThirdPartyJniPath)
3640
action.include("glog-${glogVersion.get()}/src/**/*", "CMakeLists.txt", "config.h")
3741
action.duplicatesStrategy = DuplicatesStrategy.INCLUDE
3842
action.includeEmptyDirs = false
@@ -63,7 +67,7 @@ abstract class PrepareGlogTask : DefaultTask() {
6367
action.into(outputDir)
6468
}
6569
val exportedDir = File(outputDir.asFile.get(), "exported/glog/").apply { mkdirs() }
66-
project.copy { action ->
70+
fs.copy { action ->
6771
action.from(outputDir)
6872
action.include(
6973
"stl_logging.h",

packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/PrepareBoostTaskTest.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class PrepareBoostTaskTest {
3434
val boostpath = tempFolder.newFolder("boostpath")
3535
val output = tempFolder.newFolder("output")
3636
val project = createProject()
37+
val boostThirdPartyJniPath = File(project.projectDir, "src/main/jni/third-party/boost/")
3738
val task =
3839
createTestTask<PrepareBoostTask>(project = project) {
3940
it.boostPath.setFrom(boostpath)
41+
it.boostThirdPartyJniPath.set(boostThirdPartyJniPath)
4042
it.boostVersion.set("1.0.0")
4143
it.outputDir.set(output)
4244
}
43-
File(project.projectDir, "src/main/jni/third-party/boost/CMakeLists.txt").apply {
45+
File(boostThirdPartyJniPath, "CMakeLists.txt").apply {
4446
parentFile.mkdirs()
4547
createNewFile()
4648
}
@@ -52,10 +54,12 @@ class PrepareBoostTaskTest {
5254
@Test
5355
fun prepareBoostTask_copiesAsmFiles() {
5456
val boostpath = tempFolder.newFolder("boostpath")
57+
val boostThirdPartyJniPath = tempFolder.newFolder("boostpath/jni")
5558
val output = tempFolder.newFolder("output")
5659
val task =
57-
createTestTask<PrepareBoostTask>() {
60+
createTestTask<PrepareBoostTask> {
5861
it.boostPath.setFrom(boostpath)
62+
it.boostThirdPartyJniPath.set(boostThirdPartyJniPath)
5963
it.boostVersion.set("1.0.0")
6064
it.outputDir.set(output)
6165
}
@@ -71,10 +75,12 @@ class PrepareBoostTaskTest {
7175
@Test
7276
fun prepareBoostTask_copiesBoostSourceFiles() {
7377
val boostpath = tempFolder.newFolder("boostpath")
78+
val boostThirdPartyJniPath = tempFolder.newFolder("boostpath/jni")
7479
val output = tempFolder.newFolder("output")
7580
val task =
7681
createTestTask<PrepareBoostTask> {
7782
it.boostPath.setFrom(boostpath)
83+
it.boostThirdPartyJniPath.set(boostThirdPartyJniPath)
7884
it.boostVersion.set("1.0.0")
7985
it.outputDir.set(output)
8086
}
@@ -90,10 +96,12 @@ class PrepareBoostTaskTest {
9096
@Test
9197
fun prepareBoostTask_copiesVersionlessBoostSourceFiles() {
9298
val boostpath = tempFolder.newFolder("boostpath")
99+
val boostThirdPartyJniPath = tempFolder.newFolder("boostpath/jni")
93100
val output = tempFolder.newFolder("output")
94101
val task =
95102
createTestTask<PrepareBoostTask> {
96103
it.boostPath.setFrom(boostpath)
104+
it.boostThirdPartyJniPath.set(boostThirdPartyJniPath)
97105
it.boostVersion.set("1.0.0")
98106
it.outputDir.set(output)
99107
}

packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/PrepareGlogTaskTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class PrepareGlogTaskTest {
3131
val glogpath = tempFolder.newFolder("glogpath")
3232
val output = tempFolder.newFolder("output")
3333
val project = createProject()
34+
val glogThirdPartyJniPath = File(project.projectDir, "src/main/jni/third-party/glog/")
3435
val task =
3536
createTestTask<PrepareGlogTask>(project = project) {
3637
it.glogPath.setFrom(glogpath)
38+
it.glogThirdPartyJniPath.set(glogThirdPartyJniPath)
3739
it.glogVersion.set("1.0.0")
3840
it.outputDir.set(output)
3941
}
40-
File(project.projectDir, "src/main/jni/third-party/glog/CMakeLists.txt").apply {
42+
File(glogThirdPartyJniPath, "CMakeLists.txt").apply {
4143
parentFile.mkdirs()
4244
createNewFile()
4345
}
@@ -51,13 +53,15 @@ class PrepareGlogTaskTest {
5153
val glogpath = tempFolder.newFolder("glogpath")
5254
val output = tempFolder.newFolder("output")
5355
val project = createProject()
56+
val glogThirdPartyJniPath = File(project.projectDir, "src/main/jni/third-party/glog/")
5457
val task =
5558
createTestTask<PrepareGlogTask>(project = project) {
5659
it.glogPath.setFrom(glogpath)
60+
it.glogThirdPartyJniPath.set(glogThirdPartyJniPath)
5761
it.glogVersion.set("1.0.0")
5862
it.outputDir.set(output)
5963
}
60-
File(project.projectDir, "src/main/jni/third-party/glog/config.h").apply {
64+
File(glogThirdPartyJniPath, "config.h").apply {
6165
parentFile.mkdirs()
6266
createNewFile()
6367
}
@@ -69,10 +73,12 @@ class PrepareGlogTaskTest {
6973
@Test
7074
fun prepareGlogTask_copiesSourceCode() {
7175
val glogpath = tempFolder.newFolder("glogpath")
76+
val glogThirdPartyJniPath = tempFolder.newFolder("glogpath/jni")
7277
val output = tempFolder.newFolder("output")
7378
val task =
7479
createTestTask<PrepareGlogTask> {
7580
it.glogPath.setFrom(glogpath)
81+
it.glogThirdPartyJniPath.set(glogThirdPartyJniPath)
7682
it.glogVersion.set("1.0.0")
7783
it.outputDir.set(output)
7884
}
@@ -89,10 +95,12 @@ class PrepareGlogTaskTest {
8995
@Test
9096
fun prepareGlogTask_replacesTokenCorrectly() {
9197
val glogpath = tempFolder.newFolder("glogpath")
98+
val glogThirdPartyJniPath = tempFolder.newFolder("glogpath/jni")
9299
val output = tempFolder.newFolder("output")
93100
val task =
94101
createTestTask<PrepareGlogTask> {
95102
it.glogPath.setFrom(glogpath)
103+
it.glogThirdPartyJniPath.set(glogThirdPartyJniPath)
96104
it.glogVersion.set("1.0.0")
97105
it.outputDir.set(output)
98106
}
@@ -111,10 +119,12 @@ class PrepareGlogTaskTest {
111119
@Test
112120
fun prepareGlogTask_exportsHeaderCorrectly() {
113121
val glogpath = tempFolder.newFolder("glogpath")
122+
val glogThirdPartyJniPath = tempFolder.newFolder("glogpath/jni")
114123
val output = tempFolder.newFolder("output")
115124
val task =
116125
createTestTask<PrepareGlogTask> {
117126
it.glogPath.setFrom(glogpath)
127+
it.glogThirdPartyJniPath.set(glogThirdPartyJniPath)
118128
it.glogVersion.set("1.0.0")
119129
it.outputDir.set(output)
120130
}

packages/react-native/ReactAndroid/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ val prepareBoost by
266266
tasks.registering(PrepareBoostTask::class) {
267267
dependsOn(if (boostPathOverride != null) emptyList() else listOf(downloadBoost))
268268
boostPath.setFrom(if (boostPathOverride != null) boostPath else tarTree(downloadBoostDest))
269+
boostThirdPartyJniPath.set(project.file("src/main/jni/third-party/boost"))
269270
boostVersion.set(BOOST_VERSION)
270271
outputDir.set(File(thirdPartyNdkDir, "boost"))
271272
}
@@ -399,6 +400,7 @@ val prepareGlog by
399400
tasks.registering(PrepareGlogTask::class) {
400401
dependsOn(if (dependenciesPath != null) emptyList() else listOf(downloadGlog))
401402
glogPath.setFrom(dependenciesPath ?: tarTree(downloadGlogDest))
403+
glogThirdPartyJniPath.set(project.file("src/main/jni/third-party/glog/"))
402404
glogVersion.set(GLOG_VERSION)
403405
outputDir.set(File(thirdPartyNdkDir, "glog"))
404406
}

0 commit comments

Comments
 (0)