And if I were to just find this open source MIT licensed project out there I could definitely use it. So, can I also birth into existence a new open source project? The ethical dilemma is the client is paying for my time. Do they have to approve this open source project or at least be given a heads up before it's started? What's the harm in creating something that if it was already out there I could just use and say good news, open source package XYZ by John Smith was created last year and it's perfect for this project. Substitute John Smith for my own name and it was created not last year but last week. Is that unfair to the client?
If you haven't got that kind of agreement in place already, then it'd be polite to discuss it with them first. If they assert ownership of your code with out you having agreed tot hat beforehand, you probably want to know that now anyway, before you do any more work for them.
There's nothing wrong with such an arrangement but its best to be explicitly clear about these terms, and them having copyright or an exclusion on your use of the code you develop is worth more compensation.
In the larger build vs buy investment process those contributions reduce gross expenditure aggregated across all clients. Many clients understand this.
However, open sourcing a library often involves considerable work that is not material to the client's interest- documentation for a generic audience, test cases that cover surface not relevant to the client, or things like ticket management/triage. None of that can be charged to the client.
And some clients, even though they understand the ecosystem benefit, are not going to agree to participate. They may have their own IP processes that require review and approval to ensure proprietary interests are not compromised, and that is just too expensive.