fix(mac): share Swift packaging preflight

This commit is contained in:
Vincent Koc
2026-07-04 07:53:18 +02:00
parent 57513baa27
commit d71a24f3a4
11 changed files with 117 additions and 36 deletions

View File

@@ -69,7 +69,7 @@ const ANDROID_VERSION_SYNC_PATHS = new Set([
"apps/android/version.json",
]);
const MACOS_APP_CI_PATH_RE =
/^(?:apps\/(?:macos|macos-mlx-tts|shared|swabble)\/|Swabble\/|scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.sh$|scripts\/lib\/plistbuddy\.sh$|test\/scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.test\.ts$)/u;
/^(?:apps\/(?:macos|macos-mlx-tts|shared|swabble)\/|Swabble\/|scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.sh$|scripts\/lib\/(?:plistbuddy|swift-toolchain)\.sh$|test\/scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.test\.ts$)/u;
let corepackPnpmShimDir;
let corepackPnpmShimCleanupRegistered = false;

View File

@@ -37,7 +37,7 @@ const NATIVE_PROTOCOL_GEN_RE = /^apps\/shared\/OpenClawKit\/Sources\/OpenClawPro
const MACOS_NATIVE_RE =
/^(apps\/macos\/|apps\/macos-mlx-tts\/|apps\/ios\/|apps\/shared\/|apps\/swabble\/|Swabble\/)/;
const MACOS_SCRIPT_SCOPE_RE =
/^(?:scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.sh|scripts\/lib\/plistbuddy\.sh|test\/scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.test\.ts)$/;
/^(?:scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.sh|scripts\/lib\/(?:plistbuddy|swift-toolchain)\.sh|test\/scripts\/(?:codesign-mac-app|create-dmg|notarize-mac-artifact|package-mac-app|package-mac-dist)\.test\.ts)$/;
const IOS_BUILD_RE =
/^(apps\/ios\/|apps\/shared\/|apps\/swabble\/|Swabble\/|config\/(?:swiftformat|swiftlint\.yml)$|scripts\/ios-(?:configure-signing|team-id|write-version-xcconfig)\.sh$|scripts\/ios-version\.ts$|scripts\/lib\/(?:ios-version\.ts|npm-publish-plan\.mjs|version-script-args\.ts)$)/;
const ANDROID_NATIVE_RE = /^(apps\/android\/|apps\/shared\/)/;

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
REQUIRED_SWIFT_TOOLS_MAJOR=6
REQUIRED_SWIFT_TOOLS_MINOR=2
require_swift_toolchain() {
local swift_version
if ! swift_version="$(swift --version 2>&1)"; then
printf '%s\n' "$swift_version" >&2
echo "ERROR: OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
echo " Install/select Xcode 26.x or newer before running macOS packaging scripts." >&2
return 1
fi
local major_minor
major_minor="$(printf '%s\n' "$swift_version" | sed -nE 's/.*Apple Swift version ([0-9]+)\.([0-9]+).*/\1 \2/p' | head -n 1)"
if [[ -z "$major_minor" ]]; then
printf '%s\n' "$swift_version" >&2
echo "ERROR: Could not parse selected Swift toolchain version." >&2
echo " OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
return 1
fi
local major minor
read -r major minor <<< "$major_minor"
if (( major < REQUIRED_SWIFT_TOOLS_MAJOR )) ||
(( major == REQUIRED_SWIFT_TOOLS_MAJOR && minor < REQUIRED_SWIFT_TOOLS_MINOR )); then
printf '%s\n' "$swift_version" >&2
echo "ERROR: OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
echo " Current Swift is ${major}.${minor}; install/select Xcode 26.x or newer." >&2
return 1
fi
}

View File

@@ -6,6 +6,7 @@ set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
source "$ROOT_DIR/scripts/lib/plistbuddy.sh"
source "$ROOT_DIR/scripts/lib/swift-toolchain.sh"
APP_ROOT="$ROOT_DIR/dist/OpenClaw.app"
BUILD_ROOT="$ROOT_DIR/apps/macos/.build"
PRODUCT="OpenClaw"
@@ -40,8 +41,6 @@ if [[ "$BUNDLE_ID" == *.debug ]]; then
SPARKLE_FEED_URL=""
AUTO_CHECKS=false
fi
REQUIRED_SWIFT_TOOLS_MAJOR=6
REQUIRED_SWIFT_TOOLS_MINOR=2
sparkle_canonical_build_from_version() {
(cd "$ROOT_DIR" && node --import tsx "$ROOT_DIR/scripts/sparkle-build.ts" canonical-build "$1")
@@ -91,35 +90,6 @@ run_pnpm() {
(cd "$ROOT_DIR" && "${PNPM_CMD[@]}" "$@")
}
require_swift_toolchain() {
local swift_version
if ! swift_version="$(swift --version 2>&1)"; then
printf '%s\n' "$swift_version" >&2
echo "ERROR: OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
echo " Install/select Xcode 26.x or newer before running scripts/package-mac-app.sh." >&2
return 1
fi
local major_minor
major_minor="$(printf '%s\n' "$swift_version" | sed -nE 's/.*Apple Swift version ([0-9]+)\.([0-9]+).*/\1 \2/p' | head -n 1)"
if [[ -z "$major_minor" ]]; then
printf '%s\n' "$swift_version" >&2
echo "ERROR: Could not parse selected Swift toolchain version." >&2
echo " OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
return 1
fi
local major minor
read -r major minor <<< "$major_minor"
if (( major < REQUIRED_SWIFT_TOOLS_MAJOR )) ||
(( major == REQUIRED_SWIFT_TOOLS_MAJOR && minor < REQUIRED_SWIFT_TOOLS_MINOR )); then
printf '%s\n' "$swift_version" >&2
echo "ERROR: OpenClaw macOS app packaging requires Swift tools ${REQUIRED_SWIFT_TOOLS_MAJOR}.${REQUIRED_SWIFT_TOOLS_MINOR}+." >&2
echo " Current Swift is ${major}.${minor}; install/select Xcode 26.x or newer." >&2
return 1
fi
}
merge_framework_machos() {
local primary="$1"
local dest="$2"

View File

@@ -10,11 +10,12 @@ set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
source "$ROOT_DIR/scripts/lib/plistbuddy.sh"
source "$ROOT_DIR/scripts/lib/swift-toolchain.sh"
BUILD_ROOT="$ROOT_DIR/apps/macos/.build"
PRODUCT="OpenClaw"
BUILD_CONFIG="${BUILD_CONFIG:-release}"
APP_VERSION_INPUT="${APP_VERSION:-$(cd "$ROOT_DIR" && node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")}"
APP_VERSION_INPUT="${APP_VERSION:-}"
# Default to universal binary for distribution builds (supports both Apple Silicon and Intel Macs)
export BUILD_ARCHS="${BUILD_ARCHS:-all}"
@@ -128,6 +129,12 @@ correction_build_from_exact_tag() {
# Local fallback releases must not silently fall back to a git-rev-count build number.
# For correction tags, pass a higher explicit APP_BUILD than the canonical floor.
require_swift_toolchain
if [[ -z "$APP_VERSION_INPUT" ]]; then
APP_VERSION_INPUT="$(cd "$ROOT_DIR" && node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")"
fi
if [[ -z "${APP_BUILD:-}" && "$BUILD_CONFIG" == "release" ]]; then
CANONICAL_APP_BUILD="$(require_canonical_sparkle_build "$APP_VERSION_INPUT")"
APP_BUILD="$(correction_build_from_exact_tag "$APP_VERSION_INPUT" "$CANONICAL_APP_BUILD")"

View File

@@ -1233,6 +1233,10 @@ const TOOLING_SOURCE_TEST_TARGETS = new Map([
"test/scripts/package-mac-dist.test.ts",
],
],
[
"scripts/lib/swift-toolchain.sh",
["test/scripts/package-mac-app.test.ts", "test/scripts/package-mac-dist.test.ts"],
],
[
"scripts/lib/plugin-npm-runtime-build.mjs",
["test/scripts/plugin-npm-runtime-build-args.test.ts", "test/plugin-npm-runtime-build.test.ts"],

View File

@@ -372,6 +372,7 @@ describe("detectChangedScope", () => {
"scripts/codesign-mac-app.sh",
"scripts/create-dmg.sh",
"scripts/lib/plistbuddy.sh",
"scripts/lib/swift-toolchain.sh",
"scripts/notarize-mac-artifact.sh",
"scripts/package-mac-app.sh",
"scripts/package-mac-dist.sh",

View File

@@ -1581,6 +1581,7 @@ describe("scripts/changed-lanes", () => {
"scripts/codesign-mac-app.sh",
"scripts/create-dmg.sh",
"scripts/lib/plistbuddy.sh",
"scripts/lib/swift-toolchain.sh",
"scripts/notarize-mac-artifact.sh",
"scripts/package-mac-app.sh",
"scripts/package-mac-dist.sh",

View File

@@ -49,9 +49,9 @@ function getPackageManagerHelperBlock(): string {
}
function getSwiftToolchainBlock(): string {
const script = readFileSync(scriptPath, "utf8");
const script = readFileSync("scripts/lib/swift-toolchain.sh", "utf8");
const start = script.indexOf("REQUIRED_SWIFT_TOOLS_MAJOR=");
const end = script.indexOf("merge_framework_machos()");
const end = script.length;
expect(start).toBeGreaterThanOrEqual(0);
expect(end).toBeGreaterThan(start);
@@ -301,6 +301,7 @@ describe("package-mac-app plist stamping", () => {
const installIndex = script.indexOf('if [[ "${SKIP_PNPM_INSTALL:-0}" != "1" ]]');
const preInstallBlock = script.slice(0, installIndex);
expect(script).toContain('source "$ROOT_DIR/scripts/lib/swift-toolchain.sh"');
expect(preInstallBlock).toContain("\nrequire_swift_toolchain\n");
});

View File

@@ -113,6 +113,62 @@ describe("package-mac-dist plist validation", () => {
expect(script).not.toContain('canonical_sparkle_build "$VERSION" 2>/dev/null || true');
});
it("checks Swift before Sparkle metadata or dependency bootstrap work", () => {
const script = readFileSync(scriptPath, "utf8");
const swiftIndex = script.indexOf("\nrequire_swift_toolchain\n");
const versionIndex = script.indexOf('if [[ -z "$APP_VERSION_INPUT" ]]');
const appBuildIndex = script.indexOf(
'if [[ -z "${APP_BUILD:-}" && "$BUILD_CONFIG" == "release" ]]',
);
const packageAppIndex = script.indexOf('"$ROOT_DIR/scripts/package-mac-app.sh"');
const preSwiftBlock = script.slice(0, swiftIndex);
expect(script).toContain('source "$ROOT_DIR/scripts/lib/swift-toolchain.sh"');
expect(swiftIndex).toBeGreaterThanOrEqual(0);
expect(versionIndex).toBeGreaterThan(swiftIndex);
expect(appBuildIndex).toBeGreaterThan(versionIndex);
expect(packageAppIndex).toBeGreaterThan(appBuildIndex);
expect(preSwiftBlock).not.toContain("node -p");
});
it("fails on old Swift before reading package metadata", () => {
const toolsDir = mkdtempSync(path.join(tmpdir(), "openclaw-dist-swift-tools-"));
tempDirs.push(toolsDir);
writeFileSync(
path.join(toolsDir, "swift"),
[
"#!/usr/bin/env bash",
"echo 'swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)'",
"",
].join("\n"),
"utf8",
);
chmodSync(path.join(toolsDir, "swift"), 0o755);
writeFileSync(
path.join(toolsDir, "node"),
[
"#!/usr/bin/env bash",
"echo 'node should not run before Swift preflight' >&2",
"exit 42",
"",
].join("\n"),
"utf8",
);
chmodSync(path.join(toolsDir, "node"), 0o755);
const result = runHelper(`
set -euo pipefail
PATH=${JSON.stringify(`${toolsDir}:/usr/bin:/bin`)}
BUILD_CONFIG=release bash ${scriptPath}
`);
expect(result.status).toBe(1);
expect(result.stderr).toContain("OpenClaw macOS app packaging requires Swift tools 6.2+");
expect(result.stderr).toContain("Current Swift is 6.0");
expect(result.stderr).not.toContain("node should not run before Swift preflight");
});
it("prefers repo Corepack pnpm over a global pnpm shim", () => {
const helperBlock = getPackageManagerHelperBlock();
const tempRoot = mkdtempSync(path.join(tmpdir(), "openclaw-dist-pnpm-root-"));

View File

@@ -1468,6 +1468,10 @@ describe("scripts/test-projects changed-target routing", () => {
["scripts/make_appcast.sh", ["test/scripts/make-appcast.test.ts"]],
["scripts/package-mac-app.sh", ["test/scripts/package-mac-app.test.ts"]],
["scripts/package-mac-dist.sh", ["test/scripts/package-mac-dist.test.ts"]],
[
"scripts/lib/swift-toolchain.sh",
["test/scripts/package-mac-app.test.ts", "test/scripts/package-mac-dist.test.ts"],
],
["scripts/e2e/bun-global-install-smoke.sh", ["test/scripts/test-install-sh-docker.test.ts"]],
[
"scripts/sparkle-build.ts",
@@ -1962,6 +1966,10 @@ describe("scripts/test-projects changed-target routing", () => {
"test/scripts/package-mac-dist.test.ts",
],
],
[
"scripts/lib/swift-toolchain.sh",
["test/scripts/package-mac-app.test.ts", "test/scripts/package-mac-dist.test.ts"],
],
[
"scripts/lib/npm-publish-plan.mjs",
[