Swift Package.resolved failing to checkout revision

We are explicitly using Package.resolved to be sure every developer is using the same version.

This works like a charm with Xcode 12 in fast lane but we can't make this work anymore in Xcode 13 although it works wonderfully when Xcode itself (not xcodebuild) — a.k.a. the IDE — resolves.


Command line invocation:
310[16:06:53]: ▸     /Applications/Xcode-13.0.0.app/Contents/Developer/usr/bin/xcodebuild -resolvePackageDependencies -workspace MyApp.xcworkspace -scheme MyApp -disableAutomaticPackageResolution
311[16:06:53]: ▸ User defaults from command line:
312[16:06:53]: ▸     IDEDisableAutomaticPackageResolution = YES
313[16:06:53]: ▸     IDEPackageSupportUseBuiltinSCM = YES
314[16:06:54]: ▸ Resolve Package Graph
315[16:06:56]: ▸ Checking out 5.12.0 of package ‘GRDB.swift’
316[16:06:57]: ▸ Resolved source packages:
317[16:06:57]: ▸   MyApp: (null)
318[16:06:57]: ▸ xcodebuild: error: Could not resolve package dependencies:
319[16:06:57]: ▸   Couldn’t check out revision ‘32b2923e890df320906e64cbd0faca22a8bfda14’:
320[16:06:57]: ▸     fatal: reference is not a tree: 32b2923e890df320906e64cbd0faca22a8bfda14
321[16:06:57]: ▸   cannot update Package.resolved file because automatic resolution is disabled
322[16:06:57]: ▸   fatalError

This happens not only with GRDB but with others as well.

To clarify: The revision exists at https://github.com/groue/GRDB.swift/commit/32b2923e890df320906e64cbd0faca22a8bfda14


I'm not sure whether that's your case or not, but I faced it with Git pre-push hook. The issue was caused by the fact that GIT_DIR was set to the "source" directory, and, as far as I understand, that's something that affects git checkouts that are done during xcodebuild. (It clearly looks like a xcodebuild bug to me, that supposedly does git clone followed by git checkout, with the latter unexpectedly impacted by GIT_DIR).

I'm able to reproduce it by doing something like below (with clean DerivedData):

env GIT_DIR="$PWD/.git" xcodebuild -disableAutomaticPackageResolution ...

The remedy in my case was to unexport GIT_DIR for the invocation of xcodebuild, like below:

env -u GIT_DIR xcodebuild -disableAutomaticPackageResolution ...

Wondering if something similar happens with GIT_DIR in your setup.

Thank you @grigorye! Ran into this and it would have taken me forever to narrow this down to being GIT_DIR... Unsetting it before calling out to fastlane was a fix.