Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNGP - fix: use relative paths for gradle exec invocations (#36076) #36080

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.tasks

import com.facebook.react.utils.Os.cliPath
import com.facebook.react.utils.detectOSAwareHermesCommand
import com.facebook.react.utils.moveTo
import com.facebook.react.utils.windowsAwareCommandLine
Expand Down Expand Up @@ -89,7 +90,10 @@ abstract class BundleHermesCTask : DefaultTask() {
runCommand(bundleCommand)

if (hermesEnabled.get()) {
val detectedHermesCommand = detectOSAwareHermesCommand(root.get().asFile, hermesCommand.get())
val detectedHermesCommand =
File(detectOSAwareHermesCommand(root.get().asFile, hermesCommand.get()))
.relativeTo(root.get().asFile)
.path
val bytecodeFile = File("${bundleFile}.hbc")
val outputSourceMap = resolveOutputSourceMap(bundleAssetFilename)
val compilerSourceMap = resolveCompilerSourceMap(bundleAssetFilename)
Expand Down Expand Up @@ -135,25 +139,26 @@ abstract class BundleHermesCTask : DefaultTask() {
internal fun getBundleCommand(bundleFile: File, sourceMapFile: File): List<Any> =
windowsAwareCommandLine(
buildList {
val rootFile = root.get().asFile
addAll(nodeExecutableAndArgs.get())
add(cliFile.get().asFile.absolutePath)
add(cliFile.get().asFile.cliPath(rootFile))
add(bundleCommand.get())
add("--platform")
add("android")
add("--dev")
add(devEnabled.get().toString())
add("--reset-cache")
add("--entry-file")
add(entryFile.get().asFile.toString())
add(entryFile.get().asFile.cliPath(rootFile))
add("--bundle-output")
add(bundleFile.toString())
add(bundleFile.cliPath(rootFile))
add("--assets-dest")
add(resourcesDir.get().asFile.toString())
add(resourcesDir.get().asFile.cliPath(rootFile))
add("--sourcemap-output")
add(sourceMapFile.toString())
add(sourceMapFile.cliPath(rootFile))
if (bundleConfig.isPresent) {
add("--config")
add(bundleConfig.get().asFile.absolutePath)
add(bundleConfig.get().asFile.cliPath(rootFile))
}
add("--minify")
add(minifyEnabled.get().toString())
Expand All @@ -165,26 +170,30 @@ abstract class BundleHermesCTask : DefaultTask() {
hermesCommand: String,
bytecodeFile: File,
bundleFile: File
): List<Any> =
windowsAwareCommandLine(
hermesCommand,
"-emit-binary",
"-out",
bytecodeFile.absolutePath,
bundleFile.absolutePath,
*hermesFlags.get().toTypedArray())
): List<Any> {
val rootFile = root.get().asFile
return windowsAwareCommandLine(
hermesCommand,
"-emit-binary",
"-out",
bytecodeFile.cliPath(rootFile),
bundleFile.cliPath(rootFile),
*hermesFlags.get().toTypedArray())
}

internal fun getComposeSourceMapsCommand(
composeScript: File,
packagerSourceMap: File,
compilerSourceMap: File,
outputSourceMap: File
): List<Any> =
windowsAwareCommandLine(
*nodeExecutableAndArgs.get().toTypedArray(),
composeScript.absolutePath,
packagerSourceMap.toString(),
compilerSourceMap.toString(),
"-o",
outputSourceMap.toString())
): List<Any> {
val rootFile = root.get().asFile
return windowsAwareCommandLine(
*nodeExecutableAndArgs.get().toTypedArray(),
composeScript.cliPath(rootFile),
packagerSourceMap.cliPath(rootFile),
compilerSourceMap.cliPath(rootFile),
"-o",
outputSourceMap.cliPath(rootFile))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.tasks

import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.Os.cliPath
import com.facebook.react.utils.windowsAwareCommandLine
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
Expand Down Expand Up @@ -63,16 +64,17 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
}

internal fun setupCommandLine(libraryName: String, codegenJavaPackageName: String) {
val workingDir = project.projectDir
commandLine(
windowsAwareCommandLine(
*nodeExecutableAndArgs.get().toTypedArray(),
reactNativeDir.file("scripts/generate-specs-cli.js").get().asFile.absolutePath,
reactNativeDir.file("scripts/generate-specs-cli.js").get().asFile.cliPath(workingDir),
"--platform",
"android",
"--schemaPath",
generatedSchemaFile.get().asFile.absolutePath,
generatedSchemaFile.get().asFile.cliPath(workingDir),
"--outputDir",
generatedSrcDir.get().asFile.absolutePath,
generatedSrcDir.get().asFile.cliPath(workingDir),
"--libraryName",
libraryName,
"--javaPackageName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.tasks

import com.facebook.react.utils.Os.cliPath
import com.facebook.react.utils.windowsAwareCommandLine
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
Expand Down Expand Up @@ -63,18 +64,19 @@ abstract class GenerateCodegenSchemaTask : Exec() {
}

internal fun setupCommandLine() {
val workingDir = project.projectDir
commandLine(
windowsAwareCommandLine(
*nodeExecutableAndArgs.get().toTypedArray(),
codegenDir
.file("lib/cli/combine/combine-js-to-schema-cli.js")
.get()
.asFile
.absolutePath,
.cliPath(workingDir),
"--platform",
"android",
generatedSchemaFile.get().asFile.absolutePath,
jsRootDir.asFile.get().absolutePath,
generatedSchemaFile.get().asFile.cliPath(workingDir),
jsRootDir.asFile.get().cliPath(workingDir),
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

package com.facebook.react.utils

object Os {
import java.io.File

internal object Os {

fun isWindows(): Boolean =
System.getProperty("os.name")?.lowercase()?.contains("windows") ?: false
Expand All @@ -28,4 +30,15 @@ object Os {
it
}
}

/**
* As Gradle doesn't support well path with spaces on Windows, we need to return relative path on
* Win. On Linux & Mac we'll default to return absolute path.
*/
fun File.cliPath(base: File): String =
if (isWindows()) {
this.relativeTo(base).path
} else {
this.absolutePath
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

package com.facebook.react.tasks

import com.facebook.react.tests.OS
import com.facebook.react.tests.OsRule
import com.facebook.react.tests.WithOs
import com.facebook.react.tests.createTestTask
import java.io.File
import org.junit.Assert.*
Expand Down Expand Up @@ -205,6 +207,7 @@ class BundleHermesCTaskTest {
val task =
createTestTask<BundleHermesCTask> {
it.nodeExecutableAndArgs.set(listOf("node", "arg1", "arg2"))
it.root.set(tempFolder.root)
it.cliFile.set(cliFile)
it.bundleCommand.set("bundle")
it.devEnabled.set(true)
Expand Down Expand Up @@ -244,6 +247,60 @@ class BundleHermesCTaskTest {
assertEquals(24, bundleCommand.size)
}

@Test
@WithOs(OS.WIN)
fun getBundleCommand_onWindows_returnsWinValidCommandsPaths() {
val entryFile = tempFolder.newFile("index.js")
val cliFile = tempFolder.newFile("cli.js")
val bundleFile = tempFolder.newFile("bundle.js")
val sourceMapFile = tempFolder.newFile("bundle.js.map")
val resourcesDir = tempFolder.newFolder("res")
val bundleConfig = tempFolder.newFile("bundle.config")
val task =
createTestTask<BundleHermesCTask> {
it.nodeExecutableAndArgs.set(listOf("node", "arg1", "arg2"))
it.root.set(tempFolder.root)
it.cliFile.set(cliFile)
it.bundleCommand.set("bundle")
it.devEnabled.set(true)
it.entryFile.set(entryFile)
it.resourcesDir.set(resourcesDir)
it.bundleConfig.set(bundleConfig)
it.minifyEnabled.set(true)
it.extraPackagerArgs.set(listOf("--read-global-cache"))
}

val bundleCommand = task.getBundleCommand(bundleFile, sourceMapFile)

assertEquals("cmd", bundleCommand[0])
assertEquals("/c", bundleCommand[1])
assertEquals("node", bundleCommand[2])
assertEquals("arg1", bundleCommand[3])
assertEquals("arg2", bundleCommand[4])
assertEquals(cliFile.relativeTo(tempFolder.root).path, bundleCommand[5])
assertEquals("bundle", bundleCommand[6])
assertEquals("--platform", bundleCommand[7])
assertEquals("android", bundleCommand[8])
assertEquals("--dev", bundleCommand[9])
assertEquals("true", bundleCommand[10])
assertEquals("--reset-cache", bundleCommand[11])
assertEquals("--entry-file", bundleCommand[12])
assertEquals(entryFile.relativeTo(tempFolder.root).path, bundleCommand[13])
assertEquals("--bundle-output", bundleCommand[14])
assertEquals(bundleFile.relativeTo(tempFolder.root).path, bundleCommand[15])
assertEquals("--assets-dest", bundleCommand[16])
assertEquals(resourcesDir.relativeTo(tempFolder.root).path, bundleCommand[17])
assertEquals("--sourcemap-output", bundleCommand[18])
assertEquals(sourceMapFile.relativeTo(tempFolder.root).path, bundleCommand[19])
assertEquals("--config", bundleCommand[20])
assertEquals(bundleConfig.relativeTo(tempFolder.root).path, bundleCommand[21])
assertEquals("--minify", bundleCommand[22])
assertEquals("true", bundleCommand[23])
assertEquals("--read-global-cache", bundleCommand[24])
assertEquals("--verbose", bundleCommand[25])
assertEquals(26, bundleCommand.size)
}

@Test
fun getBundleCommand_withoutConfig_returnsCommandWithoutConfig() {
val entryFile = tempFolder.newFile("index.js")
Expand All @@ -254,6 +311,7 @@ class BundleHermesCTaskTest {
val task =
createTestTask<BundleHermesCTask> {
it.nodeExecutableAndArgs.set(listOf("node", "arg1", "arg2"))
it.root.set(tempFolder.root)
it.cliFile.set(cliFile)
it.bundleCommand.set("bundle")
it.devEnabled.set(true)
Expand All @@ -274,7 +332,10 @@ class BundleHermesCTaskTest {
val bytecodeFile = tempFolder.newFile("bundle.js.hbc")
val bundleFile = tempFolder.newFile("bundle.js")
val task =
createTestTask<BundleHermesCTask> { it.hermesFlags.set(listOf("my-custom-hermes-flag")) }
createTestTask<BundleHermesCTask> {
it.root.set(tempFolder.root)
it.hermesFlags.set(listOf("my-custom-hermes-flag"))
}

val hermesCommand = task.getHermescCommand(customHermesc, bytecodeFile, bundleFile)

Expand All @@ -287,6 +348,31 @@ class BundleHermesCTaskTest {
assertEquals(6, hermesCommand.size)
}

@Test
@WithOs(OS.WIN)
fun getHermescCommand_onWindows_returnsRelativePaths() {
val customHermesc = "hermesc"
val bytecodeFile = tempFolder.newFile("bundle.js.hbc")
val bundleFile = tempFolder.newFile("bundle.js")
val task =
createTestTask<BundleHermesCTask> {
it.root.set(tempFolder.root)
it.hermesFlags.set(listOf("my-custom-hermes-flag"))
}

val hermesCommand = task.getHermescCommand(customHermesc, bytecodeFile, bundleFile)

assertEquals("cmd", hermesCommand[0])
assertEquals("/c", hermesCommand[1])
assertEquals(customHermesc, hermesCommand[2])
assertEquals("-emit-binary", hermesCommand[3])
assertEquals("-out", hermesCommand[4])
assertEquals(bytecodeFile.relativeTo(tempFolder.root).path, hermesCommand[5])
assertEquals(bundleFile.relativeTo(tempFolder.root).path, hermesCommand[6])
assertEquals("my-custom-hermes-flag", hermesCommand[7])
assertEquals(8, hermesCommand.size)
}

@Test
fun getComposeSourceMapsCommand_returnsCorrectCommand() {
val packagerMap = tempFolder.newFile("bundle.js.packager.map")
Expand All @@ -296,6 +382,7 @@ class BundleHermesCTaskTest {
val composeSourceMapsFile = File(reactNativeDir, "scripts/compose-source-maps.js")
val task =
createTestTask<BundleHermesCTask> {
it.root.set(tempFolder.root)
it.nodeExecutableAndArgs.set(listOf("node", "arg1", "arg2"))
}

Expand All @@ -312,4 +399,34 @@ class BundleHermesCTaskTest {
assertEquals(outputMap.absolutePath, composeSourcemapCommand[7])
assertEquals(8, composeSourcemapCommand.size)
}

@Test
@WithOs(OS.WIN)
fun getComposeSourceMapsCommand_onWindows_returnsRelativePaths() {
val packagerMap = tempFolder.newFile("bundle.js.packager.map")
val compilerMap = tempFolder.newFile("bundle.js.compiler.map")
val outputMap = tempFolder.newFile("bundle.js.map")
val reactNativeDir = tempFolder.newFolder("node_modules/react-native")
val composeSourceMapsFile = File(reactNativeDir, "scripts/compose-source-maps.js")
val task =
createTestTask<BundleHermesCTask> {
it.root.set(tempFolder.root)
it.nodeExecutableAndArgs.set(listOf("node", "arg1", "arg2"))
}

val composeSourcemapCommand =
task.getComposeSourceMapsCommand(composeSourceMapsFile, packagerMap, compilerMap, outputMap)

assertEquals("cmd", composeSourcemapCommand[0])
assertEquals("/c", composeSourcemapCommand[1])
assertEquals("node", composeSourcemapCommand[2])
assertEquals("arg1", composeSourcemapCommand[3])
assertEquals("arg2", composeSourcemapCommand[4])
assertEquals(composeSourceMapsFile.relativeTo(tempFolder.root).path, composeSourcemapCommand[5])
assertEquals(packagerMap.relativeTo(tempFolder.root).path, composeSourcemapCommand[6])
assertEquals(compilerMap.relativeTo(tempFolder.root).path, composeSourcemapCommand[7])
assertEquals("-o", composeSourcemapCommand[8])
assertEquals(outputMap.relativeTo(tempFolder.root).path, composeSourcemapCommand[9])
assertEquals(10, composeSourcemapCommand.size)
}
}
Loading