Skip to main content
coreydaley.dev

Why I moved my Swift packages out of a submodule and back into the app

Purrchiver’s Swift packages, which I call Kits, started out in the app repo: the archive engine, encryption, disk images, crash reporting, sandbox file access. In June I moved them into a repository of their own and pulled them back into the app as a git submodule, so Secret Store could use them too. Reusable code in its own repo, versioned on its own. It looked like good hygiene.

In October that submodule broke every fresh clone of the app. Fixing it made me look at what the separate repo was actually buying, and it wasn’t much. So the Kits went back in.

What the split was for

The point of the separate repo was sharing. By October, here’s what each app actually used:

KitSwift linesUsed by
Archive engine13,551Purrchiver
Crash reporter1,316Purrchiver, Secret Store
Encryption918Purrchiver
Disk images880Purrchiver
Session secrets321Purrchiver
Sandbox file access142Purrchiver

One Kit, the crash reporter, was shared. Everything else existed for one app. (Secret Store also used an Apple Archive Kit that Purrchiver never did.) The separate repo was mostly a second home for Purrchiver’s own code.

What it cost day to day

A submodule is a pointer. The app repo doesn’t contain the Kits; it records one commit of the Kits repo to check out. So every change to a Kit was two changes:

  1. Commit and push the Kit change in the Kits repo.
  2. Commit the new pointer in the app repo, and push that.

The order matters. Push the app first and its pointer names a commit the server has never seen.

That’s a small tax, paid every time. It also splits one logical change into two commits in two repos, so the app’s history says “bump Kits” where it should say what changed and why.

How it broke

At the start of October I pushed the app’s main branch with the Kits pointer at a commit I had never pushed to the Kits repo. And it had nowhere to go. The same cleanup, removing references to a self-hosted Git server I’d retired, had already landed upstream as a different commit. Pushing mine would have meant force-pushing over that one, and rebasing it would have reduced it to nothing.

From then on, every fresh clone of the app failed while fetching the submodule:

fatal: remote error: upload-pack: not our ref …

Nothing was lost. The commit existed, on my Mac. But the app’s main branch now depended on it, and nobody else could get it. CI, a new Mac, or anyone cloning the repo would have hit the same wall.

Git has a guard for exactly this: with push.recurseSubmodules set to check, it refuses to push the app while its pointer names a commit no remote has. I hadn’t set it. It would have stopped this push, but it wouldn’t have changed the question that came next.

The immediate fix was to point the submodule at a commit that does exist upstream: the Kits repo’s head, plus the two README lines the upstream cleanup had missed. That also pulled in a couple of unrelated upstream changes, so I built the app and ran every Kit’s tests at the new pin before pushing. Main was cloneable again before anything else changed.

Then the real question. Why was this a separate repo at all?

Moving the Kits back in

I replaced the submodule with an ordinary Kits/ directory in the app repo, holding the same files. A Kit change and the app change that needs it now land in one commit. There’s no pointer to bump and nothing to push in the right order.

A pristine copy first. The first commit is a byte-for-byte copy of the Kits repo at one commit: same paths, file modes, symlinks and contents. Git makes that checkable. A directory’s tree hash depends only on what’s in it, so the app’s Kits/ tree hash has to equal the Kits repo’s root tree hash at that commit:

git rev-parse <vendoring-commit>:Kits   # in the app repo
git rev-parse <kits-commit>^{tree}      # in the Kits repo

If those match, nothing changed in transit. The cleanups, like dropping the Secret Store-only Kit and updating docs, came in the next commit, so they aren’t buried in a 356,000-line diff of vendored code.

A .gitignore that would have bitten later. The app ignored *.xcframework. The encryption Kit vendors a prebuilt age library as an .xcframework, so git add silently skipped it. On my Mac everything still built, because the file was already there. A fresh clone would have failed to link. One exception for that path fixed it, and it’s exactly the kind of mistake an exact-copy check exists to catch: a missing file means a different tree hash. I still built from a fresh clone of GitHub afterwards, because that’s what anyone else gets.

Where the history went

The Kits’ history is now in three places. Their first two weeks are in the app repo, from before the June split. June to October is in the Kits repo. October onward is back in the app repo. So git log on a Kit in the app repo jumps straight from early June to October.

I could have imported the Kits’ old commits with git subtree. But in those commits the files live at ArchiveKit/…, not Kits/ArchiveKit/…, so git log -- Kits/ArchiveKit still wouldn’t show them. (git blame and git log --follow on a single file would.) Making directory-level history work means rewriting every imported commit to add the Kits/ prefix, and since the snapshot was already on main, merging that in now would mean rewriting main and force-pushing it.

So I traded imported history for a copy I could verify with one hash. The Kits repo still exists, and a README in Kits/ says where each era of a file’s history lives, with the command for each.

How I checked it

  • A fresh clone from GitHub, without --recurse-submodules, builds. That includes linking the encryption Kit against the vendored age library.
  • Every Kit’s own test suite passes from a clean build.
  • The full app test plan ran on my build Mac. It had ten failures, StoreKit entitlement tests and a handful of UI tests, so I ran the same plan on the commit before any of this work started. The same ten failed, with the same messages. Nothing here caused them; they’re tracked on their own.

That last step is the one I’d recommend most. A red test run after a big structural change tells you nothing until you know what red looked like before.

What it costs

Vendoring isn’t free either.

The shared Kit is now two copies. Secret Store still uses the Kits repo as a submodule, for the crash reporter and the Apple Archive Kit. Purrchiver has its own copy of the crash reporter. From here they evolve separately, and a fix to one has to be made to the other by hand. The README says so: a fix to the crash reporter’s redaction belongs in both copies. That note is doing real work. Nothing else will remind me.

The app repo carries more. Most of those 356,000 lines are vendored C and C++ (libarchive and UnRAR), plus a 12 MB prebuilt library. Anyone who built the app was already downloading all of it through the submodule. The difference is that every future update to that binary now lives in the app repo’s history.

For two apps that share one small package, that’s the right trade. If both apps shared most of the Kits, or a third app came along, the separate repo would earn its keep again, and I’d want a versioned Swift package with tags rather than a submodule pointer.

The general lesson

A submodule makes sense when the code inside it really is someone else’s, or really is shared and released on its own schedule. When it’s your code, used by one app, it mostly adds a second commit, an ordering rule, and a history full of “bump Kits.”

If you vendor, copy first and change second, prove the copy is exact, build from a fresh clone, and write down where the old history went.

Have you split code out to share it and then pulled it back in? What told you it was time?

Related posts