Summary
You can overwrite a macOS app bundle in /Applications from Xcode, but the app fails to launch with error -47 until you log out or reboot. The issue is caused by a stale file lock or cache held by Launch Services or Finder after the initial deployment.
Root Cause
- Xcode copies the built bundle to
/Applicationsand starts the app for testing usingxcrun. - The original bundle remains locked by Launch Services (its icon cache and file integrity data).
- Subsequent overwrites replace the files but the lock/cache reference still points to the old bundle, causing system validation to fail with error -47 (
../MacOS : file already in use).
Why This Happens in Real Systems
- LaunchServices maintains an in-memory database of application metadata; it is not refreshed after a bundle is overwritten.
- Finder and background processes keep temporary read‑only access to the previously opened bundle.
- These persistent references survive Xcode restarts but not a full user‑session or OS reboot.
Real-World Impact
- Deployment delays: Manual logouts or reboots interrupt continuous integration pipelines.
- False positives: Users may think the app is corrupted when in fact it is a stale cache issue.
- Security risk: Launch Services may prevent launching updated binaries that carry new permissions or entitlements.
Example or Code (if necessary and relevant)
No executable code is required for this post‑mortem.
How Senior Engineers Fix It
- Force a Launch Services cache refresh
launchctl stop com.apple.launchservicesd launchctl start com.apple.launchservicesd - Clear Finder’s preview cache
rm -rf ~/Library/Caches/com.apple.finder - Remove stale app metadata
rm -rf /var/db/lsregister/*/ - Automate in CI
- Add the above commands to the post‑build script or use
xcrun simctlto reset the system.
- Add the above commands to the post‑build script or use
- Verify no processes hold the bundle
- Use
lsof | grep /Applications/MyApp.appto check for lingering file handles.
- Use
These steps ensure that the system treats the new bundle as fresh and allows immediate launch.
Why Juniors Miss It
- They often assume Xcode’s deploy step fully replaces an application.
- They overlook the existence of Launch Services and Finder cache as separate from the file system.
- They typically rely on manual logs or reboots instead of querying the system for locks (
lsof,opensnoop). - They tend to debug only the build output, not the OS layer that manages application records.
By understanding and addressing the OS caching mechanisms, senior engineers prevent the “-47” error and streamline the deployment workflow.