The issue is on this line of code:
https://github.com/bitcoin/bitcoin/blob/8b49040854be2e26b66366aeae1cba4716f93d93/src/main.cpp#L3477
Instead of checking for the commitment at the start of an output, it should be placed at the end of the very last output. Then midstate compression can be used to reduce the size of the coinbase tx for proofs that require access to the witness commitment.