← Back to PRs

#3337: fix(macos-app): patch SwiftPM Bundle.module accessor for app bundle

by skymoore open 2026-01-28 13:18 View on GitHub →
scripts
fix(macos-app): patch SwiftPM Bundle.module accessor for app bundle compatibility Problem SwiftPM's auto-generated resource_bundle_accessor.swift files use Bundle.main.bundleURL.appendingPathComponent(...) to locate package resource bundles. This resolves to .app/Package_Target.bundle at the app root. However: • macOS app bundles put resources in .app/Contents/Resources/ • Code signing prohibits arbitrary files/symlinks at the app root • This causes EXC_BREAKPOINT crashes when packages like Textual try Bundle.module.url(forResource:) (e.g., prism-bundle.js for syntax highlighting) Crash trace: Thread 0 Crashed: 0 libswiftCore.dylib _assertionFailure 1 Clawdbot closure #1 in variable initialization expression of static NSBundle.module 2 Clawdbot CodeTokenizer.init() Solution After the first swift build (which generates accessors), patch all resource_bundle_accessor.swift files: - let mainPath = Bundle.main.bundleURL.appendingPathComponent("textual_Textual.bundle").path + let mainPath = (Bundle.main.resourceURL ?? Bundle.main.bundleURL).appendingPathComponent("textual_Textual.bundle").path • Falls back to resourceURL first (correct for .app/Contents/Resources/), then bundleURL (development builds) • Rebuilds with patched accessors • No upstream changes needed Changes • scripts/package-mac-app.sh: Add double-build + sed patching step • Works for all arches, all packages (Textual, SwiftUIMath, ClawdbotKit, etc.) • Minimal overhead (~1-2s rebuild) Testing 1. BUNDLE_ID=dev.rwx.ai.bot ./scripts/package-mac-app.sh 2. Copy to /Applications/ → launches without crash ✅ 3. Syntax highlighting works (uses Prism.js from Textual bundle) ✅ 4. Codesign verifies ✅ Before/After Development (swift run) • Before: ✅ • After: ✅ Packaged .app • Before: 💥 Crash on startup • After: ✅ Launches + full functionality Codesign • Before: ✅ • After: ✅ Fixes #1709 (https://github.com/clawdbot/clawdbot/issues/1709) (Textual/CodeTokenizer crash). Closes: #1709 Type: Fix Scope: macos-app, swiftpm <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates `scripts/package-mac-app.sh` to patch SwiftPM-generated `resource_bundle_accessor.swift` files so `Bundle.module` resolves resources via `Bundle.main.resourceURL` inside a signed `.app` bundle (instead of `bundleURL` at the app root). The script now performs an initial `swift build` per-arch to generate accessors, rewrites the accessor code via `sed`, then rebuilds and continues packaging by copying resource bundles (including adding SwiftUIMath’s bundle) into `Moltbot.app/Contents/Resources`. <h3>Confidence Score: 3/5</h3> - Mostly safe to merge, but the build-step error handling could mask failures and produce flaky packaged apps. - The change is localized to a packaging script and aligns with macOS bundle resource layout, but the new `swift build ... || true` can let the script proceed without actually generating/patching accessors, making failures harder to diagnose and potentially reintroducing the crash at runtime. - scripts/package-mac-app.sh (new build+patch loop and error handling) <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs