Skip to content
Draft
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
109 changes: 80 additions & 29 deletions packages/react-native/scripts/ios-sourcemap-upload.sh
Original file line number Diff line number Diff line change
@@ -1,67 +1,118 @@
#!/bin/bash

# Script responsible for preprocessing source maps with debugid and uploading it to Backtrace via backtrace-js.
# Adds the Backtrace debug id to a React Native source map and uploads it to Backtrace via backtrace-js.
#
# Usage: ./ios-sourcemap-upload.sh <source_map_file_path> <debug_id_file_path> <backtrace_configuration_path> <project_directory_path>
# Parameters:
# <source_map_file_path> (Required) Path to the source map file.
# <debug_id_file_path> (Required) Path to generated backtrace debug id.
# <debug_id_file_path> (Required) Path to the debug id file written by the Backtrace metro serializer.
# <backtrace_configuration_path> (Required) Path to the .backtracejsrc configuration file.
# <project_directory_path> (Required) Path to the react-native project directory
# <project_directory_path> (Required) Path to the react-native project directory.
# Environment:
# DEBUG_ID_PATH Overrides <debug_id_file_path>.
# NODE_BINARY Node executable, defaults to `node` on PATH. Inside an Xcode build phase, ios/.xcode.env and
# ios/.xcode.env.local are sourced first, like React Native's own bundle phase.
#
# Adjusting metro configuration is required in order to correctly use debug_id available in the debug_id_file_path.

# Inside an Xcode build phase (CONFIGURATION is set) the script never fails the build over a missing input:
# builds that produce no debug id (SKIP_BUNDLING, or any Debug configuration, which bundles in dev mode) are
# skipped, and a missing source map, debug id, configuration file, backtrace-js or node is reported as an Xcode
# warning. Outside Xcode the same conditions are errors. A failed upload fails in both modes.
#
# The Backtrace serializer must be set as customSerializer in metro.config.js for the debug id file to exist.

set -e
set -x

if [ -z "$1" ]; then
echo "Error: Missing path to the source map file."
in_xcode_build_phase=false
if [ -n "$CONFIGURATION" ]; then
in_xcode_build_phase=true
fi

fail() {
if [ "$in_xcode_build_phase" = true ]; then
echo "warning: Backtrace: $1" >&2
exit 0
fi
echo "Error: Backtrace: $1" >&2
exit 1
}

if [ -z "$1" ]; then
fail "Missing path to the source map file."
fi

source_map_file_path="$1"
if [ -z "$3" ]; then
fail "Missing path to the .backtracejsrc file."
fi

# Check if the file exists
if [ ! -f "$source_map_file_path" ]; then
echo "Error: File '$source_map_file_path' does not exist."
exit 1
if [ -z "$4" ]; then
fail "Missing path to the project directory."
fi

source_map_file_path="$1"
debug_id_file_path=${DEBUG_ID_PATH:-${2:-}}
backtrace_configuration_path="$3"
project_directory_path="$4"

if [ -z "$debug_id_file_path" ]; then
fail "Missing path to the debug id file."
fi

if [ "$in_xcode_build_phase" = true ]; then
if [ -n "$SKIP_BUNDLING" ]; then
echo "Backtrace: SKIP_BUNDLING is set, nothing was bundled, skipping source map upload."
exit 0
fi

# Debug configurations bundle in dev mode, and the Backtrace serializer writes no debug id for dev bundles.
case "$CONFIGURATION" in
*Debug*)
echo "Backtrace: Debug configuration produces no debug id, skipping source map upload."
exit 0
;;
esac
fi

if [ ! -f "$source_map_file_path" ]; then
fail "Source map file '$source_map_file_path' does not exist. Check that SOURCEMAP_FILE is exported before the React Native bundle step."
fi

if [ ! -f "$debug_id_file_path" ]; then
echo "Error: File '$debug_id_file_path' does not exist."
exit 1
fail "Debug id file '$debug_id_file_path' does not exist. Check if customSerializer has been set to the Backtrace serializer in metro.config.js."
fi

if [ -z "$3" ]; then
echo "Error: Missing path to the .backtracejsrc file."
exit 1
if [ ! -f "$backtrace_configuration_path" ]; then
fail "Configuration file '$backtrace_configuration_path' does not exist."
fi

if [ -z "$4" ]; then
echo "Error: Missing path to the project directory."
exit 1
backtrace_js_path="${project_directory_path}/node_modules/.bin/backtrace-js"

if [ ! -f "$backtrace_js_path" ]; then
fail "backtrace-js not found at '$backtrace_js_path'. Install @backtrace/javascript-cli in the project."
fi

project_directory_path="$4"
if [ -z "$NODE_BINARY" ] && [ -n "$PODS_ROOT" ]; then
if [ -f "$PODS_ROOT/../.xcode.env" ]; then
source "$PODS_ROOT/../.xcode.env"
fi
if [ -f "$PODS_ROOT/../.xcode.env.local" ]; then
source "$PODS_ROOT/../.xcode.env.local"
fi
fi

backtrace_configuration_path="$3"
NODE_BINARY="${NODE_BINARY:-node}"

# check and assign NODE_BINARY env
source "$REACT_NATIVE_PATH/scripts/node-binary.sh"
debug_id=$(<"$debug_id_file_path")
if ! type "$NODE_BINARY" >/dev/null 2>&1; then
fail "Cannot find the '$NODE_BINARY' binary. Set NODE_BINARY in ios/.xcode.env or in the build phase."
fi

debug_id=$(<"$debug_id_file_path")
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

"$NODE_BINARY" "$script_dir/addDebugIdToSourceMap.js" \
"$source_map_file_path" \
"$debug_id"

# path to react-native module dir relative from this script
backtrace_js_path="${project_directory_path}/node_modules/.bin/backtrace-js"

# run backtrace-js on bundle
"$NODE_BINARY" "$backtrace_js_path" upload \
-p "$source_map_file_path" \
--config "$backtrace_configuration_path"
Loading
Loading