Skip to content

Commit 3782511

Browse files
mfazekasfacebook-github-bot
authored andcommitted
fix: android autolinkLibrariesFromCommand should handle timeout or non zero exit code (#45333)
Summary: Fixes: #45307 ## Changelog: [Android] [Fixed] - if `npx react-native-community/cli config` fails or timeouts proper error is shown and built is aborted, instead of leaving and empty autolinking.json During build `npx react-native-community/cli config` is generated into autolinking.json. When command fails, we should error and should not leave and empty `autolinking.json` Pull Request resolved: #45333 Test Plan: Output of the reproducer in #45307 looks like this: ```log android % ./gradlew assembleDebug Starting a Gradle Daemon (subsequent builds will be faster) ERROR: autolinkLibrariesFromCommand: Failed to create /Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/android/build/generated/autolinking/autolinking.json - process npx react-native-community/cli config exited with error code: 126 FAILURE: Build failed with an exception. * Where: Settings file '/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/android/settings.gradle' line: 3 * What went wrong: A problem occurred evaluating settings 'android'. > ERROR: autolinkLibrariesFromCommand: Failed to create /Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/android/build/generated/autolinking/autolinking.json - process npx react-native-community/cli config exited with error code: 126 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 10s 8 actionable tasks: 4 executed, 4 up-to-date ``` Output if you modify the package.json to be invalid looks like this: ```log android % ./gradlew assembleDebug ERROR: autolinkLibrariesFromCommand: process npx react-native-community/cli config exited with error code: 1 JSONError: JSON Error in /Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/package.json: 35 | "node": ">=18" 36 | }, > 37 | SOMETHING_NON_JSON | ^ 38 | "packageManager": "[email protected]", 39 | "resolutions": { 40 | "rtn-centered-text": "portal:../RTNCenteredText" Unexpected token "S" (0x53) in JSON at position 1019 while parsing near "...ode\": \">=18\"\n },\n SOMETHING_NON_JSON\n ..." 35 | "node": ">=18" 36 | }, > 37 | SOMETHING_NON_JSON | ^ 38 | "packageManager": "[email protected]", 39 | "resolutions": { 40 | "rtn-centered-text": "portal:../RTNCenteredText" at parseJson (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/parse-json/index.js:29:21) at loadJson (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/loaders.js:48:16) at #loadConfiguration (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/ExplorerSync.js:116:36) at #loadConfigFileWithImports (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/ExplorerSync.js:87:54) at #readConfiguration (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/ExplorerSync.js:84:82) at search (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/ExplorerSync.js:50:63) at emplace (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/util.js:36:20) at ExplorerSync.search (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/ExplorerSync.js:78:42) at getUserDefinedOptionsFromMetaConfig (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/index.js:32:37) at mergeOptionsBase (/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/node_modules/react-native-community/cli-config/node_modules/cosmiconfig/dist/index.js:60:31) FAILURE: Build failed with an exception. * Where: Settings file '/Users/boga/Work/OSS/RNMBGL/rn-fabric-boolattribute/ReproducerApp/android/settings.gradle' line: 3 * What went wrong: A problem occurred evaluating settings 'android'. > ERROR: autolinkLibrariesFromCommand: process npx react-native-community/cli config exited with error code: 1 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 2s 8 actionable tasks: 4 executed, 4 up-to-date ``` Reviewed By: cipolleschi Differential Revision: D59582430 Pulled By: cortinico fbshipit-source-id: bedb9563175cc5c46f5af80cf309769e56b803cc
1 parent 52dc7a8 commit 3782511

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

packages/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import java.math.BigInteger
1313
import java.security.MessageDigest
1414
import java.util.concurrent.TimeUnit
1515
import javax.inject.Inject
16+
import org.gradle.api.GradleException
1617
import org.gradle.api.file.FileCollection
1718
import org.gradle.api.initialization.Settings
19+
import org.gradle.api.logging.Logging
1820

1921
abstract class ReactSettingsExtension @Inject constructor(val settings: Settings) {
2022

@@ -47,12 +49,27 @@ abstract class ReactSettingsExtension @Inject constructor(val settings: Settings
4749
outputFile.parentFile.mkdirs()
4850
val lockFilesChanged = checkAndUpdateLockfiles(lockFiles, outputFolder)
4951
if (lockFilesChanged || outputFile.exists().not() || outputFile.length() != 0L) {
50-
ProcessBuilder(command)
51-
.directory(workingDirectory)
52-
.redirectOutput(ProcessBuilder.Redirect.to(outputFile))
53-
.redirectError(ProcessBuilder.Redirect.INHERIT)
54-
.start()
55-
.waitFor(5, TimeUnit.MINUTES)
52+
val process =
53+
ProcessBuilder(command)
54+
.directory(workingDirectory)
55+
.redirectOutput(ProcessBuilder.Redirect.to(outputFile))
56+
.redirectError(ProcessBuilder.Redirect.INHERIT)
57+
.start()
58+
val finished = process.waitFor(5, TimeUnit.MINUTES)
59+
if (!finished || (process.exitValue() != 0)) {
60+
val prefixCommand =
61+
"ERROR: autolinkLibrariesFromCommand: process ${command.joinToString(" ")}"
62+
val message =
63+
if (!finished) "${prefixCommand} timed out"
64+
else "${prefixCommand} exited with error code: ${process.exitValue()}"
65+
val logger = Logging.getLogger("ReactSettingsExtension")
66+
logger.error(message)
67+
if (outputFile.length() != 0L) {
68+
logger.error(outputFile.readText().substring(0, 1024))
69+
}
70+
outputFile.delete()
71+
throw GradleException(message)
72+
}
5673
}
5774
linkLibraries(getLibrariesToAutolink(outputFile))
5875
}

0 commit comments

Comments
 (0)