diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b7c0ba741..b7af3b6ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ ### Fixes +- Fix the iOS Release build failing in hoisted monorepos because `sentry-xcode.sh` looked for React Native in `../node_modules` ([#7182](https://github.com/getsentry/sentry-react-native/pull/7182)) - Keep the original exception as the main one when it has linked errors, so the `cause` no longer becomes the issue title ([#7179](https://github.com/getsentry/sentry-react-native/pull/7179)) - Errors with a linked cause may group into new issues after upgrading diff --git a/packages/core/scripts/sentry-xcode.sh b/packages/core/scripts/sentry-xcode.sh index f1c3a4df05..792a2c5f2d 100755 --- a/packages/core/scripts/sentry-xcode.sh +++ b/packages/core/scripts/sentry-xcode.sh @@ -41,7 +41,15 @@ $SENTRY_CLI_ENV SENTRY_CLI_ENV_EOF set -x -REACT_NATIVE_XCODE_DEFAULT="../node_modules/react-native/scripts/react-native-xcode.sh" +# React Native's bundle phase runs this script without an argument when REACT_NATIVE_XCODE points at it. +# Prefer the script in REACT_NATIVE_PATH, the build setting React Native's CocoaPods setup writes (0.69+), +# so hoisted monorepos, where react-native is not in ../node_modules, find it. Only use it if the script +# exists there. See https://github.com/getsentry/sentry-react-native/issues/7175 +if [ -n "$REACT_NATIVE_PATH" ] && [ -f "$REACT_NATIVE_PATH/scripts/react-native-xcode.sh" ]; then + REACT_NATIVE_XCODE_DEFAULT="$REACT_NATIVE_PATH/scripts/react-native-xcode.sh" +else + REACT_NATIVE_XCODE_DEFAULT="../node_modules/react-native/scripts/react-native-xcode.sh" +fi REACT_NATIVE_XCODE="${1:-$REACT_NATIVE_XCODE_DEFAULT}" [[ "$AUTO_RELEASE" == false ]] && [[ -z "$BUNDLE_COMMAND" || "$BUNDLE_COMMAND" != "ram-bundle" ]] && NO_AUTO_RELEASE="--no-auto-release" diff --git a/packages/core/test/scripts/sentry-xcode-scripts.test.ts b/packages/core/test/scripts/sentry-xcode-scripts.test.ts index c9bb824c06..4055a2e261 100644 --- a/packages/core/test/scripts/sentry-xcode-scripts.test.ts +++ b/packages/core/test/scripts/sentry-xcode-scripts.test.ts @@ -501,6 +501,113 @@ describe('sentry-xcode.sh', () => { expect(args.slice(-2)).toEqual(['--build-script', mockReactNativeScript]); }); + describe('React Native script resolution without an argument (issue #7175)', () => { + // React Native's bundle phase runs `$WITH_ENVIRONMENT $REACT_NATIVE_XCODE`, so when + // REACT_NATIVE_XCODE points at sentry-xcode.sh the script gets no argument. + const writeRnScript = (dir: string, message: string): string => { + const scriptsDir = path.join(dir, 'scripts'); + fs.mkdirSync(scriptsDir, { recursive: true }); + const script = path.join(scriptsDir, 'react-native-xcode.sh'); + fs.writeFileSync(script, `#!/bin/bash\necho "${message}"\nexit 0\n`); + fs.chmodSync(script, '755'); + return script; + }; + + const runWithoutArg = (cwd: string, env: Record = {}) => { + const mockCollectModulesScript = path.join(tempDir, 'collect-modules.sh'); + fs.writeFileSync(mockCollectModulesScript, '#!/bin/bash\nexit 0\n'); + fs.chmodSync(mockCollectModulesScript, '755'); + const childEnv: Record = { + ...process.env, + NODE_BINARY: process.execPath, + SENTRY_CLI_EXECUTABLE: mockSentryCliScript, + PROJECT_DIR: cwd, + DERIVED_FILE_DIR: tempDir, + SENTRY_COLLECT_MODULES: mockCollectModulesScript, + SENTRY_DISABLE_AUTO_UPLOAD: 'true', + ...env, + }; + if (!('REACT_NATIVE_PATH' in env)) { + delete childEnv.REACT_NATIVE_PATH; + } + const result = spawnSync('bash', [XCODE_SCRIPT], { cwd, env: childEnv, encoding: 'utf8' }); + return { stdout: result.stdout, stderr: result.stderr, exitCode: result.status }; + }; + + // Hoisted monorepo: /node_modules/react-native, app in /apps/mobile/ios. + const makeHoistedMonorepo = () => { + const root = path.join(tempDir, 'monorepo'); + const iosDir = path.join(root, 'apps', 'mobile', 'ios'); + fs.mkdirSync(iosDir, { recursive: true }); + const reactNativePath = path.join(root, 'node_modules', 'react-native'); + writeRnScript(reactNativePath, 'Hoisted React Native bundle'); + return { iosDir, reactNativePath }; + }; + + it('runs the script from REACT_NATIVE_PATH in a hoisted monorepo', () => { + const { iosDir, reactNativePath } = makeHoistedMonorepo(); + + const result = runWithoutArg(iosDir, { REACT_NATIVE_PATH: reactNativePath }); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain('Hoisted React Native bundle'); + }); + + it('runs the script from REACT_NATIVE_PATH for the upload', () => { + const { iosDir, reactNativePath } = makeHoistedMonorepo(); + const echoScript = path.join(tempDir, 'mock-sentry-cli-echo-args.js'); + fs.writeFileSync(echoScript, "console.log('ARGS=' + JSON.stringify(process.argv.slice(2)));"); + + const result = runWithoutArg(iosDir, { + REACT_NATIVE_PATH: reactNativePath, + SENTRY_DISABLE_AUTO_UPLOAD: 'false', + SENTRY_CLI_EXECUTABLE: echoScript, + }); + + expect(result.exitCode).toBe(0); + const args: string[] = JSON.parse(/ARGS=(.+)/.exec(result.stdout)![1]); + expect(args.slice(-2)).toEqual([ + '--build-script', + path.join(reactNativePath, 'scripts', 'react-native-xcode.sh'), + ]); + }); + + it('falls back to ../node_modules/react-native when REACT_NATIVE_PATH is not set', () => { + const appRoot = path.join(tempDir, 'app'); + const iosDir = path.join(appRoot, 'ios'); + fs.mkdirSync(iosDir, { recursive: true }); + writeRnScript(path.join(appRoot, 'node_modules', 'react-native'), 'Relative React Native bundle'); + + const result = runWithoutArg(iosDir); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain('Relative React Native bundle'); + }); + + it('falls back to ../node_modules/react-native when REACT_NATIVE_PATH has no React Native script', () => { + const appRoot = path.join(tempDir, 'app'); + const iosDir = path.join(appRoot, 'ios'); + fs.mkdirSync(iosDir, { recursive: true }); + writeRnScript(path.join(appRoot, 'node_modules', 'react-native'), 'Relative React Native bundle'); + const staleReactNativePath = path.join(tempDir, 'moved', 'node_modules', 'react-native'); + + const result = runWithoutArg(iosDir, { REACT_NATIVE_PATH: staleReactNativePath }); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain('Relative React Native bundle'); + }); + + it('prefers the script passed as an argument over REACT_NATIVE_PATH', () => { + const { reactNativePath } = makeHoistedMonorepo(); + + const result = runScript({ REACT_NATIVE_PATH: reactNativePath, SENTRY_DISABLE_AUTO_UPLOAD: 'true' }); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain('Mock React Native bundle'); + expect(result.stdout).not.toContain('Hoisted React Native bundle'); + }); + }); + describe('project path containing spaces (issue #6583)', () => { // Put react-native-xcode.sh in a directory whose path contains a space. The disable and Debug // branches re-parse REACT_NATIVE_XCODE via `/bin/sh -c`, which word-splits on the space unless