At the end of Law 16 on goal kicks:
"For any other offence, the kick is retaken."
This is also debatable. If the outcome is a YC for the GK, based on the GK's original kick being an offence, then my take is then this line from Law 16 applies. I can also see an argument that the ball is in play after the GK's kick, therefore no retake. But that would mean that the GK's original kick only became an offence based on the actions of the defender - that seems wrong to me - at least it would seem wrong to then caution the GK - it should be YC for defender instead. If IFAB want the GK to be cautioned in the OP scenario then I think the ball was not in play, YC for GK and retake... but I'm open to offers!
Having followed my own logic... I am now thinking, why would that be any different for outside the box... I'm starting to think it's wrong to caution the GK and it should be the defender... and always IDFK as the ball was in play... I realise this isn't helping...