If you want to not have a line break, you shouldn't rely on arbitrary behavior. You should use non-breaking characters like non-breaking spaces and word joiners.
That's an interesting practicality but I don't think it's the cause of the rule: The rule probably long predates automated line breaking. Also, I think automatic line breaking will break compound words at the hyphen; it doesn't require spaces (which is also obvious from a software development point of view: the logic is relatively simple either way):
Lorem ipsum dolor sit amet, consectetur adipiscing double-
decker lorem ipsum dolor sit amet, consectetur ...Why yes, I did list the opposite behavior as an advantage of each. Because that, too, is up to individual preference. :-)
I've definitely noticed the behavior you describe on some layout engines, too, and it's another reason why I personally prefer "foo – bar" style.
I've come to the conclusion it boils down to which style manual one follows. I've taken a careful look at numbers of high-end books which no doubt have been carefully typeset and I've found EM dashes with and without spaces.
It seems there is no definitive rule but I might be wrong.