Skip to content

Commit 1f62529

Browse files
cortinicofacebook-github-bot
authored andcommittedNov 5, 2024·
Properly handle paths with spaces in autolinking (#47388)
Summary: Pull Request resolved: #47388 Fixes #47364 Fixes #47377 Fixes #37124 We're having problems is a path contains a space ' ' because when autolinking, the `add_subdirectory()` function of CMake consider the path with space as 2 parameters. This fixes it by properly quoting the path. Changelog: [Android] [Fixed] - Properly handle paths with spaces in autolinking Reviewed By: cipolleschi Differential Revision: D65434413 fbshipit-source-id: b9147482f98f7e222405cc8d9e6f3c17a5f4ed02
1 parent 3956955 commit 1f62529

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed
 

‎packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTask.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
5959
val cxxModuleCMakeListsPath = dep.cxxModuleCMakeListsPath
6060
if (libraryName != null && cmakeListsPath != null) {
6161
// If user provided a custom cmakeListsPath, let's honor it.
62-
val nativeFolderPath = cmakeListsPath.replace("CMakeLists.txt", "")
62+
val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
6363
addDirectoryString +=
64-
"add_subdirectory($nativeFolderPath ${libraryName}_autolinked_build)"
64+
"add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
6565
}
6666
if (cxxModuleCMakeListsPath != null) {
6767
// If user provided a custom cxxModuleCMakeListsPath, let's honor it.
68-
val nativeFolderPath = cxxModuleCMakeListsPath.replace("CMakeLists.txt", "")
68+
val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
6969
addDirectoryString +=
70-
"\nadd_subdirectory($nativeFolderPath ${libraryName}_cxxmodule_autolinked_build)"
70+
"\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
7171
}
7272
addDirectoryString
7373
}
@@ -159,6 +159,9 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
159159
const val COMPONENT_DESCRIPTOR_FILENAME = "ComponentDescriptors.h"
160160
const val COMPONENT_INCLUDE_PATH = "react/renderer/components"
161161

162+
internal fun sanitizeCmakeListsPath(cmakeListsPath: String): String =
163+
cmakeListsPath.replace("CMakeLists.txt", "").replace(" ", "\\ ")
164+
162165
// language=cmake
163166
val CMAKE_TEMPLATE =
164167
"""

‎packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTaskTest.kt

+23-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.facebook.react.model.ModelAutolinkingConfigJson
1111
import com.facebook.react.model.ModelAutolinkingDependenciesJson
1212
import com.facebook.react.model.ModelAutolinkingDependenciesPlatformAndroidJson
1313
import com.facebook.react.model.ModelAutolinkingDependenciesPlatformJson
14+
import com.facebook.react.tasks.GenerateAutolinkingNewArchitecturesFileTask.Companion.sanitizeCmakeListsPath
1415
import com.facebook.react.tests.createTestTask
1516
import org.assertj.core.api.Assertions.assertThat
1617
import org.junit.Rule
@@ -145,9 +146,9 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
145146
# or link against a old prefab target (this is needed for React Native 0.76 on).
146147
set(REACTNATIVE_MERGED_SO true)
147148
148-
add_subdirectory(./a/directory/ aPackage_autolinked_build)
149-
add_subdirectory(./another/directory/ anotherPackage_autolinked_build)
150-
add_subdirectory(./another/directory/cxx/ anotherPackage_cxxmodule_autolinked_build)
149+
add_subdirectory("./a/directory/" aPackage_autolinked_build)
150+
add_subdirectory("./another/directory/with\ spaces/" anotherPackage_autolinked_build)
151+
add_subdirectory("./another/directory/cxx/" anotherPackage_cxxmodule_autolinked_build)
151152
152153
set(AUTOLINKED_LIBRARIES
153154
react_codegen_aPackage
@@ -258,6 +259,24 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
258259
.trimIndent())
259260
}
260261

262+
@Test
263+
fun sanitizeCmakeListsPath_withPathEndingWithFileName_removesFilename() {
264+
val input = "./a/directory/CMakeLists.txt"
265+
assertThat(sanitizeCmakeListsPath(input)).isEqualTo("./a/directory/")
266+
}
267+
268+
@Test
269+
fun sanitizeCmakeListsPath_withSpaces_removesSpaces() {
270+
val input = "./a/dir ectory/with spaces/"
271+
assertThat(sanitizeCmakeListsPath(input)).isEqualTo("./a/dir\\ ectory/with\\ spaces/")
272+
}
273+
274+
@Test
275+
fun sanitizeCmakeListsPath_withPathEndingWithFileNameAndSpaces_sanitizesIt() {
276+
val input = "./a/dir ectory/CMakeLists.txt"
277+
assertThat(sanitizeCmakeListsPath(input)).isEqualTo("./a/dir\\ ectory/")
278+
}
279+
261280
private val testDependencies =
262281
listOf(
263282
ModelAutolinkingDependenciesPlatformAndroidJson(
@@ -276,7 +295,7 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
276295
buildTypes = emptyList(),
277296
libraryName = "anotherPackage",
278297
componentDescriptors = listOf("AnotherPackageComponentDescriptor"),
279-
cmakeListsPath = "./another/directory/CMakeLists.txt",
298+
cmakeListsPath = "./another/directory/with spaces/CMakeLists.txt",
280299
cxxModuleCMakeListsPath = "./another/directory/cxx/CMakeLists.txt",
281300
cxxModuleHeaderName = "AnotherCxxModule",
282301
cxxModuleCMakeListsModuleName = "another_cxxModule",

0 commit comments

Comments
 (0)
Please sign in to comment.