I just tried this again and it worked this time!
(add-hook 'org-babel-post-tangle-hook #'delete-trailing-whitespace)
(add-hook 'org-babel-post-tangle-hook #'save-buffer :append)
Thanks!
Some back-story on what led me to modify ob-core.el:
I did try changing the hook earlier, but that did not work. So this was my next attempt[1], and that led me to try this approach of preventing white spaces in the first place.
Earlier I was also trying to tangle using ox-publish with this element in org-publish-project-alist:
("foo"
:base-directory "/some/dir"
:publishing-function org-babel-tangle-publish
:publishing-directory "/some/dir")
Probably the base directory and publishing directory being the same caused some issue with saving the buffer after delete-trailing-whitespace. So I ended up having to manually save the tangled file after each publish step.
Or probably I somehow didn't have the hooks right.
After that I switched to calling just the below separately instead of tangling through publish step:
(org-babel-tangle file FILE)
And now with the above hook modifications, the deletion of whitespaces is working exactly as I wanted.
Well, that was an interesting journey.