Files
react-native/packages/react-native/scripts/react-native-xcode.sh
T
Kræn Hansen 2fee13094b App fails to build when Node binary path contains a space (#48275)
Summary:
Building RNTester fails locally because my node path contains a space " ".
(I'm using [fnm](https://github.com/Schniz/fnm) which installs into `/Users/{username}/Library/Application Support/fnm`).

I haven't verified this, but I suspect this is broken for other apps as well, as the script is called when bundling for any app.

<details>

<summary>Expand to see output from the failed build</summary>

```
Node found at: /Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node
+ DEST=/tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/RNTester.app
+ [[ ! -n '' ]]
+ [[ Release = *Debug* ]]
+ [[ -n '' ]]
+ case "$CONFIGURATION" in
+ DEV=false
+++ dirname ../react-native/scripts/react-native-xcode.sh
++ cd ../react-native/scripts/..
++ pwd
+ REACT_NATIVE_DIR=/Users/kraen.hansen/Repositories/react-native/packages/react-native
+ PROJECT_ROOT=/Users/kraen.hansen/Repositories/react-native/packages/rn-tester
+ cd /Users/kraen.hansen/Repositories/react-native/packages/rn-tester
+ [[ -n /Users/kraen.hansen/Repositories/react-native/packages/rn-tester/js/RNTesterApp.ios.js ]]
+ :
+ source /Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/node-binary.sh
++ '[' -z '/Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node' ']'
++ type '/Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node'
+ HERMES_ENGINE_PATH=/Users/kraen.hansen/Repositories/react-native/packages/rn-tester/Pods/hermes-engine
+ '[' -z /Users/kraen.hansen/Repositories/react-native/packages/rn-tester/Pods/hermes-engine/build_host_hermesc/bin/hermesc ']'
+ [[ true != false ]]
+ [[ -f /Users/kraen.hansen/Repositories/react-native/packages/rn-tester/Pods/hermes-engine ]]
+ '[' -z '' ']'
+ export NODE_ARGS=
+ NODE_ARGS=
+ '[' -z '' ']'
+ CLI_PATH=/Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/bundle.js
+ '[' -z '' ']'
+ BUNDLE_COMMAND=bundle
+ '[' -z '' ']'
+ COMPOSE_SOURCEMAP_PATH=/Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/compose-source-maps.js
+ [[ -z '' ]]
+ CONFIG_ARG=
+ [[ -z '' ]]
+ BUNDLE_NAME=main
+ BUNDLE_FILE=/tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/main.jsbundle
+ EXTRA_ARGS=()
+ case "$PLATFORM_NAME" in
+ BUNDLE_PLATFORM=ios
+ '[' '' = YES ']'
+ EMIT_SOURCEMAP=
+ [[ ! -z ../sourcemap.ios.map ]]
+ EMIT_SOURCEMAP=true
+ PACKAGER_SOURCEMAP_FILE=
+ [[ true == true ]]
+ [[ true != false ]]
++ basename ../sourcemap.ios.map
+ PACKAGER_SOURCEMAP_FILE=/tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/sourcemap.ios.map
+ EXTRA_ARGS+=("--sourcemap-output" "$PACKAGER_SOURCEMAP_FILE")
+ [[ true != false ]]
+ [[ false == false ]]
+ EXTRA_ARGS+=("--minify" "false")
+ [[ -n '' ]]
+ [[ -n '' ]]
+ EXTRA_ARGS+=("--config-cmd" "$NODE_BINARY $NODE_ARGS $REACT_NATIVE_DIR/cli.js config")
+ '/Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node' /Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/bundle.js bundle --entry-file /Users/kraen.hansen/Repositories/react-native/packages/rn-tester/js/RNTesterApp.ios.js --platform ios --dev false --reset-cache --bundle-output /tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/main.jsbundle --assets-dest /tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/RNTester.app --sourcemap-output /tmp/RNTesterBuild/Build/Products/Release-iphonesimulator/sourcemap.ios.map --minify false --config-cmd '/Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node  /Users/kraen.hansen/Repositories/react-native/packages/react-native/cli.js config'
/bin/sh: /Users/kraen.hansen/Library/Application: No such file or directory
node:internal/errors:983
  const err = new Error(message);
              ^

Error: Command failed: /Users/kraen.hansen/Library/Application Support/fnm/node-versions/v22.11.0/installation/bin/node  /Users/kraen.hansen/Repositories/react-native/packages/react-native/cli.js config
/bin/sh: /Users/kraen.hansen/Library/Application: No such file or directory

    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:888:11)
    at execSync (node:child_process:960:15)
    at Command.handleAction (/Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/bundle.js:48:9)
    at Command.listener [as _actionHandler] (/Users/kraen.hansen/Repositories/react-native/node_modules/commander/lib/command.js:542:17)
    at /Users/kraen.hansen/Repositories/react-native/node_modules/commander/lib/command.js:1502:14
    at Command._chainOrCall (/Users/kraen.hansen/Repositories/react-native/node_modules/commander/lib/command.js:1386:12)
    at Command._parseCommand (/Users/kraen.hansen/Repositories/react-native/node_modules/commander/lib/command.js:1501:27)
    at Command.parse (/Users/kraen.hansen/Repositories/react-native/node_modules/commander/lib/command.js:1064:10)
    at Object.<anonymous> (/Users/kraen.hansen/Repositories/react-native/packages/react-native/scripts/bundle.js:71:11)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Object.<anonymous> (node:internal/modules/cjs/loader:1689:10)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49 {
  status: 127,
  signal: null,
  output: [
    null,
    '',
    '/bin/sh: /Users/kraen.hansen/Library/Application: No such file or directory\n'
  ],
  pid: 64660,
  stdout: '',
  stderr: '/bin/sh: /Users/kraen.hansen/Library/Application: No such file or directory\n'
}

Node.js v22.11.0
Command PhaseScriptExecution failed with a nonzero exit code

warning: Run script build phase '[CP-User] [RN]Check FBReactNativeSpec' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'React-RCTFBReactNativeSpec' from project 'Pods')
warning: Run script build phase 'Build JS Bundle' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'RNTester' from project 'RNTesterPods')
warning: Run script build phase '[RN] Copy Hermes Framework' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'RNTester' from project 'RNTesterPods')
** BUILD FAILED **
```
</details>

This PR add single quotes around the `$NODE_BINARY` and `$REACT_NATIVE_DIR` to avoid `--config-cmd` escaping similarly to the way they're escaped when invoked just below: https://github.com/facebook/react-native/blob/00c7174c24fd15db7723633e3e67aa59a7e73a6c/packages/react-native/scripts/react-native-xcode.sh#L155

## Changelog:

[IOS] [FIXED] - Properly escape paths in Xcode build script used when bundling an app.

Pull Request resolved: https://github.com/facebook/react-native/pull/48275

Test Plan:
- Change your node path to contain a space (possibly through a symlink and manually updating the `.xcode.env.local` file in `packages/rn-tester`.
- Build the RNTester app for e2e tests: `yarn e2e-build-ios`
- See the failure mentioned above 💥
- Apply this patch and re-run the build command to success 

Reviewed By: NickGerleman

Differential Revision: D67256815

Pulled By: robhogan

fbshipit-source-id: e27a8cd079347fdf982c28b5af347be621c8feba
2024-12-15 18:15:49 -08:00

193 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# Bundle React Native app's code and image assets.
# This script is supposed to be invoked as part of Xcode build process
# and relies on environment variables (including PWD) set by Xcode
# Print commands before executing them (useful for troubleshooting)
set -x -e
DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH
# Enables iOS devices to get the IP address of the machine running Metro
if [[ ! "$SKIP_BUNDLING_METRO_IP" && "$CONFIGURATION" = *Debug* && ! "$PLATFORM_NAME" == *simulator ]]; then
for num in 0 1 2 3 4 5 6 7 8; do
IP=$(ipconfig getifaddr en${num} || echo "")
if [ ! -z "$IP" ]; then
break
fi
done
if [ -z "$IP" ]; then
IP=$(ifconfig | grep 'inet ' | grep -v ' 127.' | grep -v ' 169.254.' |cut -d\ -f2 | awk 'NR==1{print $1}')
fi
echo "$IP" > "$DEST/ip.txt"
fi
if [[ "$SKIP_BUNDLING" ]]; then
echo "SKIP_BUNDLING enabled; skipping."
exit 0;
fi
case "$CONFIGURATION" in
*Debug*)
if [[ "$PLATFORM_NAME" == *simulator ]]; then
if [[ "$FORCE_BUNDLING" ]]; then
echo "FORCE_BUNDLING enabled; continuing to bundle."
else
echo "Skipping bundling in Debug for the Simulator (since the packager bundles for you). Use the FORCE_BUNDLING flag to change this behavior."
exit 0;
fi
else
echo "Bundling for physical device. Use the SKIP_BUNDLING flag to change this behavior."
fi
DEV=true
;;
"")
echo "$0 must be invoked by Xcode"
exit 1
;;
*)
DEV=false
;;
esac
# Path to react-native folder inside node_modules
REACT_NATIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Most projects have their project root, one level up from their Xcode project dir (the "ios" directory)
PROJECT_ROOT=${PROJECT_ROOT:-"$PROJECT_DIR/.."}
cd "$PROJECT_ROOT" || exit
# Define entry file
if [[ "$ENTRY_FILE" ]]; then
# Use ENTRY_FILE defined by user
:
elif [[ -s "index.ios.js" ]]; then
ENTRY_FILE=${1:-index.ios.js}
else
ENTRY_FILE=${1:-index.js}
fi
# check and assign NODE_BINARY env
# shellcheck source=/dev/null
source "$REACT_NATIVE_DIR/scripts/node-binary.sh"
HERMES_ENGINE_PATH="$PODS_ROOT/hermes-engine"
[ -z "$HERMES_CLI_PATH" ] && HERMES_CLI_PATH="$HERMES_ENGINE_PATH/destroot/bin/hermesc"
# If hermesc is not available and USE_HERMES is not set to false, show error.
if [[ $USE_HERMES != false && -f "$HERMES_ENGINE_PATH" && ! -f "$HERMES_CLI_PATH" ]]; then
echo "error: Hermes is enabled but the hermesc binary could not be found at ${HERMES_CLI_PATH}." \
"Perhaps you need to run 'bundle exec pod install' or otherwise " \
"point the HERMES_CLI_PATH variable to your custom location." >&2
exit 2
fi
[ -z "$NODE_ARGS" ] && export NODE_ARGS=""
[ -z "$CLI_PATH" ] && CLI_PATH="$REACT_NATIVE_DIR/scripts/bundle.js"
[ -z "$BUNDLE_COMMAND" ] && BUNDLE_COMMAND="bundle"
[ -z "$COMPOSE_SOURCEMAP_PATH" ] && COMPOSE_SOURCEMAP_PATH="$REACT_NATIVE_DIR/scripts/compose-source-maps.js"
if [[ -z "$BUNDLE_CONFIG" ]]; then
CONFIG_ARG=""
else
CONFIG_ARG="--config $BUNDLE_CONFIG"
fi
if [[ -z "$BUNDLE_NAME" ]]; then
BUNDLE_NAME="main"
fi
BUNDLE_FILE="$CONFIGURATION_BUILD_DIR/$BUNDLE_NAME.jsbundle"
EXTRA_ARGS=()
case "$PLATFORM_NAME" in
"macosx")
BUNDLE_PLATFORM="macos"
;;
*)
BUNDLE_PLATFORM="ios"
;;
esac
if [ "${IS_MACCATALYST}" = "YES" ]; then
BUNDLE_PLATFORM="ios"
fi
EMIT_SOURCEMAP=
if [[ ! -z "$SOURCEMAP_FILE" ]]; then
EMIT_SOURCEMAP=true
fi
PACKAGER_SOURCEMAP_FILE=
if [[ $EMIT_SOURCEMAP == true ]]; then
if [[ $USE_HERMES != false ]]; then
PACKAGER_SOURCEMAP_FILE="$CONFIGURATION_BUILD_DIR/$(basename "$SOURCEMAP_FILE")"
else
PACKAGER_SOURCEMAP_FILE="$SOURCEMAP_FILE"
fi
EXTRA_ARGS+=("--sourcemap-output" "$PACKAGER_SOURCEMAP_FILE")
fi
# Hermes doesn't require JS minification.
if [[ $USE_HERMES != false && $DEV == false ]]; then
EXTRA_ARGS+=("--minify" "false")
fi
# Allow opting out of using npx react-native config
if [[ -n "$CONFIG_JSON" ]]; then
EXTRA_ARGS+=("--load-config" "$CONFIG_JSON")
elif [[ -n "$CONFIG_CMD" ]]; then
EXTRA_ARGS+=("--config-cmd" "$CONFIG_CMD")
else
EXTRA_ARGS+=("--config-cmd" "'$NODE_BINARY' $NODE_ARGS '$REACT_NATIVE_DIR/cli.js' config")
fi
# shellcheck disable=SC2086
"$NODE_BINARY" $NODE_ARGS "$CLI_PATH" $BUNDLE_COMMAND \
$CONFIG_ARG \
--entry-file "$ENTRY_FILE" \
--platform "$BUNDLE_PLATFORM" \
--dev $DEV \
--reset-cache \
--bundle-output "$BUNDLE_FILE" \
--assets-dest "$DEST" \
"${EXTRA_ARGS[@]}" \
$EXTRA_PACKAGER_ARGS
if [[ $USE_HERMES == false ]]; then
cp "$BUNDLE_FILE" "$DEST/"
BUNDLE_FILE="$DEST/$BUNDLE_NAME.jsbundle"
else
EXTRA_COMPILER_ARGS=
if [[ $DEV == true ]]; then
EXTRA_COMPILER_ARGS=-Og
else
EXTRA_COMPILER_ARGS=-O
fi
if [[ $EMIT_SOURCEMAP == true ]]; then
EXTRA_COMPILER_ARGS="$EXTRA_COMPILER_ARGS -output-source-map"
fi
"$HERMES_CLI_PATH" -emit-binary -max-diagnostic-width=80 $EXTRA_COMPILER_ARGS -out "$DEST/$BUNDLE_NAME.jsbundle" "$BUNDLE_FILE"
if [[ $EMIT_SOURCEMAP == true ]]; then
HBC_SOURCEMAP_FILE="$DEST/$BUNDLE_NAME.jsbundle.map"
"$NODE_BINARY" "$COMPOSE_SOURCEMAP_PATH" "$PACKAGER_SOURCEMAP_FILE" "$HBC_SOURCEMAP_FILE" -o "$SOURCEMAP_FILE"
rm "$HBC_SOURCEMAP_FILE"
rm "$PACKAGER_SOURCEMAP_FILE"
fi
BUNDLE_FILE="$DEST/$BUNDLE_NAME.jsbundle"
fi
if [[ $DEV != true && ! -f "$BUNDLE_FILE" ]]; then
echo "error: File $BUNDLE_FILE does not exist. Your environment is misconfigured as Metro was not able to produce the bundle so your release application won't work!" >&2
exit 2
fi