You are absolutely right, that happens. Something becoming more common is "slopsquatting" where hallucinated library names are created with ill intent.
That's why code reviews exist.
That's why code reviews exist.
Comments
You are bringing up valid concerns, and there are measures that can be put in place to address what you have mentioned.
That's when it gets bounced in a code review. In a responsible org, code doesn't go in production unless it's reviewed, and the reviewer understands it.