Mac系统Haskell 8.2.2编译try.hs报错:无法找到文件求助
Hey there, let's troubleshoot why GHC 8.2.2 can't locate your try.hs file even though it shows up in directory listings. I’ve run into similar quirky macOS file issues before, so let’s break this down step by step.
Your Current Situation (Recap)
First, let's lay out what you've shared, formatted for clarity:
Initial Commands & Error
$ ls try.hs try.hs $ ghc -o try try.hs <no location info>: error: can't find file: try.hs
Directory Details
Running ls -l confirms the file exists (but it's empty, size 0 bytes):
total 0 -rw-rw-r--@ 1 <> <> 0 Mar 23 15:54 try.hs
Running ls -l@ shows macOS-specific extended attributes attached to the file:
total 0 -rw-rw-r--@ 1 <> <> 0 Mar 23 15:54 try.hs com.apple.TextEncoding 15 com.apple.metadata:_kMDItemUserTags 42 com.apple.metadata:kMDLabel_z4p7jqbpj7dblx5lt33gtc742u 105
Likely Causes & Fixes
1. Hidden Non-Printable Characters in the Filename
This is the most common culprit on macOS. Sometimes when creating files via copy-paste or certain text editors, filenames pick up invisible characters (like trailing spaces, non-breaking spaces, or Unicode control characters). Even though ls shows try.hs, the actual filename has extra characters that GHC doesn't recognize.
How to Verify:
Run either of these commands to reveal hidden characters:
# Shows non-printable characters as backslash-escaped codes ls -b try.hs # Prints the filename with all special characters escaped printf "%q\n" try.hs
If you see anything extra (like try.hs\ for a trailing space), that's the problem.
Fix:
Use tab completion to make sure you're referencing the actual filename, then rename it to a clean version:
# Type "try" then press Tab to auto-complete the full (hidden-character-included) filename mv try<tab> try.hs
2. macOS Extended Attributes Interfering
The @ symbol in your ls -l output means the file has macOS-specific metadata (like the tags shown in ls -l@). While rare, this metadata can sometimes throw off command-line tools like GHC.
Fix:
Clear all extended attributes from the file with this command:
xattr -c try.hs
Then try compiling again with ghc -o try try.hs.
3. Empty File Edge Case (Quick Check)
Your file is currently empty (0 bytes). GHC should still recognize an empty .hs file (it would throw a parse error instead of "can't find file"), but it's worth adding a simple test snippet to rule out any odd behavior:
# Add this line to try.hs and save main = putStrLn "Hello, Haskell!"
Then re-run the compile command.
4. Double-Check Your Working Directory (Sanity Check)
Even though ls shows the file, confirm you're in the exact directory where try.hs lives:
pwd
If this doesn't match the directory where you created the file, navigate to the correct folder with cd /path/to/your/file before compiling.
Quick Side Note
GHC 8.2.2 is a pretty old version (released back in 2017). While it shouldn't cause this specific file-not-found error, you might run into other compatibility issues later on. If you're just starting out with Haskell, consider upgrading to a newer stable version via a Haskell version manager—though this isn't necessary to fix your current problem.
内容的提问来源于stack exchange,提问作者hhwwww

