Skip to content

Commit 5dc1522

Browse files
Igor Klemenskifacebook-github-bot
Igor Klemenski
authored andcommitted
Fix building RN Android from source on Windows. (#30535)
Summary: Running `.\gradlew installArchives` is currently broken on Windows. This is because .sh scripts were added in the codegen module, which cannot be run by the Command Prompt on Windows. It can be worked around by installing eg. Git Bash., which can be then leveraged using the code modifications in this PR, which include sanitizing mixed Linux-Windows relative paths, and other minor Windows-specific adjustments. It's required that the user adds a Windows Environment variable storing the path to their bash binary named `REACT_WINDOWS_BASH`. Pair-programmed with davinci26 ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fix] - Fix building React Android on Windows. Fixes #30271 Pull Request resolved: #30535 Reviewed By: ShikaSD Differential Revision: D25909760 Pulled By: appden fbshipit-source-id: ea0e6e7c161a5e4a937d46e8e6972ce142fead4e
1 parent fc1f0df commit 5dc1522

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/react-native-codegen/android/build.gradle

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import org.apache.tools.ant.taskdefs.condition.Os
9+
810
buildscript {
911
repositories {
1012
mavenLocal()
@@ -42,5 +44,23 @@ task('buildCodegenCLI', type: Exec) {
4244
nodeModulesDir.mkdirs();
4345
outputs.dirs(libDir, nodeModulesDir)
4446

45-
commandLine("$codegenRoot/scripts/oss/build.sh")
47+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
48+
// Convert path to Linux format: use canonical path to strip it off relative elements in the middle of the string.
49+
// Then replace baskslashes with slashes, remove leading colon, add leading slash.
50+
// Eg. D:\path1\sub2/.. -> /D/path1/path2
51+
String canonicalPath = new File(codegenRoot).getCanonicalPath()
52+
String linuxPath = canonicalPath.replace('\\', '/');
53+
linuxPath = linuxPath.replace(':', '')
54+
linuxPath = '/' + linuxPath
55+
56+
// Get the location of bash in the system; assume environment variable created to store it.
57+
String bashHome = "$System.env.REACT_WINDOWS_BASH"
58+
if (bashHome == null) {
59+
throw new GradleException("REACT_WINDOWS_BASH is not defined.")
60+
}
61+
commandLine(bashHome, "-c", "$linuxPath/scripts/oss/build.sh")
62+
}
63+
else {
64+
commandLine("$codegenRoot/scripts/oss/build.sh")
65+
}
4666
}

packages/react-native-codegen/android/gradlePlugin-build/gradlePlugin/src/main/java/com/facebook/react/codegen/plugin/CodegenPlugin.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public void apply(final Project project) {
3333
final File generatedSchemaFile = new File(generatedSrcDir, "schema.json");
3434

3535
// 2. Task: produce schema from JS files.
36+
String os = System.getProperty("os.name").toLowerCase();
37+
3638
project
3739
.getTasks()
3840
.register(
@@ -62,7 +64,7 @@ public void apply(final Project project) {
6264

6365
ImmutableList<String> execCommands =
6466
new ImmutableList.Builder<String>()
65-
.add("yarn")
67+
.add(os.contains("windows") ? "yarn.cmd" : "yarn")
6668
.addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs))
6769
.add(extension.codegenGenerateSchemaCLI().getAbsolutePath())
6870
.add(generatedSchemaFile.getAbsolutePath())
@@ -96,7 +98,7 @@ public void apply(final Project project) {
9698

9799
ImmutableList<String> execCommands =
98100
new ImmutableList.Builder<String>()
99-
.add("yarn")
101+
.add(os.contains("windows") ? "yarn.cmd" : "yarn")
100102
.addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs))
101103
.add(extension.codegenGenerateNativeModuleSpecsCLI().getAbsolutePath())
102104
.add("android")

packages/react-native-codegen/scripts/oss/build.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ if [[ ${FBSOURCE_ENV:-0} -eq 1 ]]; then
3232
"$YARN_BINARY" run build
3333

3434
popd >/dev/null
35+
3536
else
3637
# Run yarn install in a separate tmp dir to avoid conflict with the rest of the repo.
3738
# Note: OSS-only.
3839
TMP_DIR=$(mktemp -d)
3940

40-
cp -R "$CODEGEN_DIR/." "$TMP_DIR"
41+
# On Windows this script gets run by a seprate Git Bash instance, which cannot perform the copy
42+
# due to file locks created by the host process. Need to exclude .lock files while copying.
43+
# Using in-memory tar operation because piping `find` and `grep` doesn't preserve folder structure
44+
# during recursive copying, and `rsync` is not installed by default in Git Bash.
45+
# As an added benefit, blob copy is faster.
46+
if [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "cygwin" ]; then
47+
tar cf - --exclude='*.lock' "$CODEGEN_DIR" | (cd "$TMP_DIR" && tar xvf - );
48+
else
49+
cp -R "$CODEGEN_DIR/." "$TMP_DIR";
50+
fi
4151

4252
pushd "$TMP_DIR" >/dev/null
4353

0 commit comments

Comments
 (0)