That suggests you have a corrupt pkgdb. rebuild-tree writes the files from scratch, so there should not (in theory!) be the possibility that you end up with references to packages that no longer exist.I've tried to help avoid these, so if you have a pkg_install that includes this commit:https://github.com/NetBSD/pkgsrc/commit/968bd9bb773ddd3d4558d45dbc3c329a98c504a0 that can at least rule that problem out.
I see pkg_install was just updated on many machines as a result of pkg_rolling-replace with main.c version 1.71, so perhaps this will fix things in the future, once I install the pkg_ files (I'm not a fan of changing my search path to have installed binaries supercede OS binaries).
It's usually caused by specific pkg-<version> DEPENDS matches, which is why I don't like them and would recommend avoiding them at all costs, and why it's usually vim that causes these problems.
I'll look in to it more if it happens again, particularly pkg_admin rebuild.
Thanks! John