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
156 changes: 156 additions & 0 deletions .github/workflow-scripts/spm-add-lib-incremental.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/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.

# Adds packages/react-native-test-library to an app that is already on SwiftPM
# and built once, then builds three times in Debug:
# 1. fails with "native modules changed. Build again.", because Xcode planned
# the build with the package graph from before the library was added;
# 2. succeeds, with the library compiled into the app;
# 3. succeeds, with the library still there.
#
# Usage:
# spm-add-lib-incremental.sh <app-ios-dir> <name.xcodeproj> <scheme> <derived-data-path>

set -euo pipefail

if [ $# -ne 4 ]; then
echo "Usage: $0 <app-ios-dir> <name.xcodeproj> <scheme> <derived-data-path>"
exit 2
fi

APP_IOS_DIR=$(cd "$1" && pwd)
XCODE_PROJECT=$2
XCODE_SCHEME=$3
case "$4" in
/*) DERIVED_DATA=$4 ;;
*) DERIVED_DATA="$PWD/$4" ;;
esac

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
LIBRARY_DIR=$(cd "$SCRIPT_DIR/../../packages/react-native-test-library" && pwd)
APP_ROOT=$(dirname "$APP_IOS_DIR")
APP_PACKAGE_JSON="$APP_ROOT/package.json"
AUTOLINKED_MANIFEST="$APP_IOS_DIR/build/generated/autolinking/Package.swift"
LIBRARIES=(apple common)
LOG_DIR=$(mktemp -d)
BUILD_ERROR='native modules changed. Build again.'

# Restored from a copy, not from git: the CI setup rewrites the app's
# package.json before this runs, and later steps need that version.
PACKAGE_JSON_COPY=$(mktemp)
cp "$APP_PACKAGE_JSON" "$PACKAGE_JSON_COPY"

cleanup() {
cp "$PACKAGE_JSON_COPY" "$APP_PACKAGE_JSON"
rm -f "$PACKAGE_JSON_COPY"
rm -rf "$LOG_DIR"
local library
for library in "${LIBRARIES[@]}"; do
rm -f "$APP_ROOT/node_modules/react-native-test-library-$library"
rm -rf "$LIBRARY_DIR/$library/Package.swift" \
"$LIBRARY_DIR/$library/include" \
"$LIBRARY_DIR/$library/react-native-spm-prefix.h"
done
}
trap cleanup EXIT

fail() {
echo "::error::$1"
exit 1
}

show_log() {
echo "Last lines of $1:"
tail -n 30 "$1"
echo "Error lines in $1:"
grep 'error:' "$1" | head -n 20 || true
}

build() {
local log=$1
if (cd "$APP_IOS_DIR" && xcodebuild \
-project "$XCODE_PROJECT" \
-scheme "$XCODE_SCHEME" \
-configuration Debug \
-sdk iphonesimulator \
-destination 'generic/platform=iOS Simulator' \
-derivedDataPath "$DERIVED_DATA" \
build) >"$log" 2>&1; then
echo 0
else
echo $?
fi
}

# In Debug, Xcode puts the app's code in <App>.debug.dylib, not in the app
# executable.
library_symbol_count() {
local dylib
dylib=$(find "$DERIVED_DATA/Build/Products/Debug-iphonesimulator" \
-maxdepth 2 -path '*.app/*.debug.dylib' -type f 2>/dev/null | head -n 1 || true)
if [ -z "$dylib" ]; then
echo 0
return
fi
nm "$dylib" | grep -cF "OBJC_CLASS_\$_TestLibrary" || true
}

if [ ! -f "$AUTOLINKED_MANIFEST" ]; then
fail "$AUTOLINKED_MANIFEST is missing. Build the app on SwiftPM once before this script."
fi

# The manifests are created before the dependency is added, as for a library
# that ships its own Package.swift. Scaffolding through the app would refresh
# the app's autolinked manifest too, and build 1 would then not see a change.
MANIFEST_SHA256=$(shasum -a 256 "$AUTOLINKED_MANIFEST" | cut -d ' ' -f 1)
node "$SCRIPT_DIR/spm-scaffold-test-library.js" "$APP_IOS_DIR"
if [ "$(shasum -a 256 "$AUTOLINKED_MANIFEST" | cut -d ' ' -f 1)" != "$MANIFEST_SHA256" ]; then
fail "Writing the library manifests changed the app's autolinked Package.swift."
fi

mkdir -p "$APP_ROOT/node_modules"
for library in "${LIBRARIES[@]}"; do
ln -s "$LIBRARY_DIR/$library" "$APP_ROOT/node_modules/react-native-test-library-$library"
done
APP_ROOT="$APP_ROOT" LIBRARY_DIR="$LIBRARY_DIR" LIBRARIES="${LIBRARIES[*]}" node -e '
const fs = require("fs");
const path = require("path");
const {APP_ROOT, LIBRARY_DIR, LIBRARIES} = process.env;
const file = path.join(APP_ROOT, "package.json");
const pkg = JSON.parse(fs.readFileSync(file, "utf8"));
for (const library of LIBRARIES.split(" ")) {
pkg.dependencies["react-native-test-library-" + library] =
"file:" + path.relative(APP_ROOT, path.join(LIBRARY_DIR, library));
}
fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + "\n");
'

echo "Build 1: expect it to fail with '$BUILD_ERROR'"
STATUS=$(build "$LOG_DIR/build-1.log")
if [ "$STATUS" -eq 0 ]; then
show_log "$LOG_DIR/build-1.log"
fail "Build 1 after adding the library succeeded. It must fail with '$BUILD_ERROR', because Xcode planned it with the old package graph."
fi
if ! grep -qF "$BUILD_ERROR" "$LOG_DIR/build-1.log"; then
show_log "$LOG_DIR/build-1.log"
fail "Build 1 failed for another reason than '$BUILD_ERROR'."
fi

for n in 2 3; do
echo "Build $n: expect it to succeed with the library in the app"
STATUS=$(build "$LOG_DIR/build-$n.log")
if [ "$STATUS" -ne 0 ]; then
show_log "$LOG_DIR/build-$n.log"
fail "Build $n after adding the library failed with exit code $STATUS."
fi
SYMBOLS=$(library_symbol_count)
echo "TestLibrary Objective-C classes in the app: $SYMBOLS"
if [ "$SYMBOLS" -eq 0 ]; then
fail "Build $n succeeded, but the app does not contain the library."
fi
done

echo "The library was added without a clean build."
86 changes: 86 additions & 0 deletions .github/workflow-scripts/spm-scaffold-test-library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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.
*
* @format
*/

// Writes Package.swift for packages/react-native-test-library, as if the
// library shipped its own manifest.
//
// Usage: node spm-scaffold-test-library.js <app-ios-dir>
//
// `npx react-native spm scaffold` would also regenerate the app's autolinked
// Package.swift, so the next build would already know the library. This calls
// the scaffolder with a throwaway autolinking.json instead, and leaves the app
// untouched.

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

const LIBRARY_ROOT = path.resolve(
__dirname,
'../../packages/react-native-test-library/apple',
);

function main(appIosDir) {
const appRoot = path.resolve(appIosDir);
// The react-native the app installed, which in CI is the packaged one.
const reactNativeRoot = path.dirname(
require.resolve('react-native/package.json', {paths: [appRoot]}),
);
const {scaffoldAll} = require(
path.join(reactNativeRoot, 'scripts/spm/scaffold-package-swift'),
);

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-test-library-'));
let results;
try {
const autolinkingJsonPath = path.join(tmpDir, 'autolinking.json');
fs.writeFileSync(
autolinkingJsonPath,
JSON.stringify({
dependencies: {
'react-native-test-library-apple': {
root: LIBRARY_ROOT,
platforms: {
ios: {
podspecPath: path.join(
LIBRARY_ROOT,
'TestLibraryApple.podspec',
),
},
},
},
},
}),
);
results = scaffoldAll({
appRoot,
projectRoot: path.dirname(appRoot),
reactNativeRoot,
autolinkingJsonPath,
force: true,
});
} finally {
fs.rmSync(tmpDir, {recursive: true, force: true});
}

for (const result of results) {
const reason = result.reason != null ? ` (${result.reason})` : '';
console.log(`${result.depName}: ${result.status}${reason}`);
}
return results.length > 0 && results.every(r => r.status === 'written');
}

if (process.argv.length !== 3) {
console.error('Usage: node spm-scaffold-test-library.js <app-ios-dir>');
process.exit(2);
}
if (!main(process.argv[2])) {
console.error('Not every test library manifest was written.');
process.exit(1);
}
9 changes: 9 additions & 0 deletions .github/workflows/test-ios-spm-newapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ jobs:
echo "::error::Release build embeds a Debug React.framework ($COUNT getDebugProps symbols, expected none)"
exit 1
fi
# Reuses the first build's derived data: the test is that a library added
# after a build is compiled without a clean build.
- name: Add a native library and build again (Debug)
if: matrix.flavor == 'Debug'
shell: bash
working-directory: ${{ env.APP_IOS_DIR }}
run: |
"$GITHUB_WORKSPACE/.github/workflow-scripts/spm-add-lib-incremental.sh" \
. "$XCODE_PROJECT" "$XCODE_SCHEME" build/spm-e2e-dd
- name: Take HelloWorld back to CocoaPods
if: matrix.flavor == 'Debug'
shell: bash
Expand Down
Loading