There are tools that will analyze a codebase and identify where chunks of varying size seem to come from. Mostly to determine if the code is encumbered by problematic licenses, but also to detect where the programmers may have borrowed code from.
If memory serves, some of these companies also have closed source codebases in their database, enabling them to detect if unpublished code has been re-used.
The times I've used this in due diligence it has rarely been a deal-breaker when we do find large chunks of code that may be problematic. For instance due to licensing terms that are not acceptable. You just make a note of it and have them rewrite the code before the transaction can take place. (Or you figure out if you can accomodate the license terms).
It's as if Microsoft were banking on the fact that most violations will be unnoticed
Because they shared the code under a license, and they have the right to complain if people use that code but don't follow the license.
For example, what happens if Github Copilot spits a copy of some copyrighted code verbatim? Is laundering open source code through a machine learning model a loophole for not having to follow the license?
Often following the license is as simple as giving credit to the original author.