Dependency upgrade errors with pkgsrc on macOS

For some years now Joyent have been quietly hosting binary pkgsrc packages for macOS. It doesn't seem to have as high a profile as homebrew/fink/macports, but if you're familiar with BSD-style ports (and particularly if you need to compile things with custom options) you'll probably find it more comfortable.

Anyway, it works well in my experience, with the only hiccup being that very occasionally during a package upgrade I will get an error like the following:

# pkgin update
# pkgin full-upgrade
pkg_install warnings: 0, errors: 1
pkg_install error log can be found in /var/db/pkgin/pkg_install-err.log

This happens when its upgrading some dependency (it's never the same package, in my experience), and if you look at the last lines in the error log you'll see something like this:

---Jul 24 15:42:24: upgrading tk-
---Jul 24 15:42:25: upgrading tiff-4.0.10nb1...
---Jul 24 15:42:26: upgrading perl-5.28.2...
pkg_add: Can't open +CONTENTS of depending package p5-List-SomeUtils-XS-0.58
pkg_add: 1 package addition failed
---Jul 24 15:42:26: upgrading netpbm-10.79.04nb2...
---Jul 24 15:42:28: upgrading libwebp-1.0.3...
---Jul 24 15:42:28: upgrading libimagequant-2.12.3...
---Jul 24 15:42:29: upgrading libXi-1.7.10...
---Jul 24 15:42:29: upgrading jasper-2.0.16...
---Jul 24 15:42:29: upgrading MesaLib-11.2.2nb11...

The "Can't open +CONTENTS of depending package" error from pkg_add suggests that the dependency database has somehow ended up with a reference to a dependency (or dependency version) that isn't installed. I'm not really sure why this happens, but whenever it does happen it can be a tricky thing to search for (the package name changes each time).

So, for posterity and search indexes, this fixes it for me:

# pkgin clean
# pkg_admin rebuild-tree
# pkgin full-upgrade
calculating dependencies...done.

1 package to upgrade:

0 to refresh, 1 to upgrade, 0 to install
18M to download, -328K to install

proceed ? [Y/n] y
perl-5.28.2.tgz                           100%   18MB 891.3KB/s   00:21
upgrading perl-5.28.2...
pkg_install warnings: 0, errors: 0
reading local summary...
processing local summary...