Paul makes a distinction between 'works' and 'works of the law' He was writing to certain heretics referred to a 'judazizers'
You need to do a bit of study to understand the difference.
This looks a lot like special pleading to me.