I have just switched hosts for my domain, including my email. My new host supports IMAP, where my old one did not, so I wanted to make the most of this to transfer all my mail folders to the server. I set up the account on my work MacBook Pro, and everything was fine. I made a directory on the server, then filled in that directory as the 'IMAP Path Prefix' in Mail's preferences for the account, and I could successfully create mailboxes and move messages in.
When I set up the account on my MacBook at home, however, Mail was completely ignoring the IMAP Path Prefix I set, and insisted on listing the entire contents of my home directory, including all the invisible files (.bash_history and so on) and, even worse, the whole public_html tree where my Web site is stored. Looking at a trace of the IMAP communication between Mail and the server, it was clearly a problem on my local machine rather than an error on the server, because Mail was asking for mailbox listings without mentioning the path prefix.
Since everything was still working fine on my work Mac, I thought that there must be some subtle difference in the preferences between the two computers, so I used BBEdit to compare the com.apple.mail.plist files from both machines. In amongst the many arbitrary differences that will always show up when comparing preference files from different computers (such as minor differences in the window sizes or column widths), I noticed the following key:
<key>IMAPServerPrefixesMirrorFilesystem</key>
<true/>
This setting was present in my work preferences file, but absent altogether from my home machine's preferences. Hoping that this key might be the culprit, I added this entry to the preferences file, restarted Mail, and suddenly everything worked properly -- it showed only the mailboxes in the actual mail folder on the server, not all of the other bits and pieces that had been showing up before.
defaults write com.apple.mail IMAPServerPrefixesMirrorFilesystem -bool true
[robg adds: I haven't tested this one.]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20081209021330682