Sometimes after executing git checkout branch
or git reset another-branch
(while Xcode is open) I find out that the Package.resolved
file gets mysteriously deleted. I can easily restore it from git or just recreate it from Xcode, but it gets annoying. It seems like Xcode itself deletes it. Any idea why it happens and how to fix it?
As a workaround I have created a script that monitors Package.resolved
file presence and restores it when Xcode fails to resolve it.
Usage:
sh restorePackage.resolved.sh YourProject.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
p.s. add &
at the end of the command to run it in background.
Install fswatch
dependency:
brew install fswatch
restorePackage.resolved.sh
:
#!/bin/bash
# Check if file path is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <path-to-file>"
exit 1
fi
FILEPATH="$1"
DIRECTORY=$(dirname "$FILEPATH")
GIT_DIRECTORY=$(git rev-parse --git-dir)
# Function to check file presence and restore if absent
check_and_restore() {
sleep 1 # Wait for 1 second to check if the file is resolved correctly by Xcode
# Wait if Git is locked
while [ -f "$GIT_DIRECTORY/index.lock" ]; do
echo "Git is currently locked, waiting..."
sleep 1
done
if [ ! -f "$FILEPATH" ]; then
git checkout HEAD -- "$FILEPATH"
echo "File restored from Git."
fi
}
# Monitoring the file
fswatch -0 "$DIRECTORY" | while IFS= read -r -d "" event
do
if [ ! -f "$FILEPATH" ]; then
check_and_restore
fi
done
Don't switch branches or reset or anything like that with Xcode open. Just don't do it. You can make Xcode very unhappy and Weird Stuff (tm, pat. pend.) can result. Xcode's Git integration is extremely poor, and changing stuff on disk (like wit git switch, checkout, or reset) while Xcode has things open just confuses the heck out of Xcode. This is true even if you ordered those changes by way of the Xcode interface!
Instead, simply close the project window, switch branches using the command line, and then open the project window. Then wait while Xcode indexes afresh, and so forth. Yes, this can take a long time. Go get a cup of coffee or something.
One of the things Xcode will do during this preparation after you open the project window is to reconstruct the Packages.resolved file. So this should solve the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With