Fix "runghc foo" where the program is foo.hs or foo.lhs
[ghc-hetmet.git] / utils / runghc / runghc.hs
index 911d0f8..1ce358e 100644 (file)
@@ -57,8 +57,8 @@ getGhcLoc args = case args of
                  ('-' : 'f' : ghc) : args' -> f ghc args'
                  -- If you need the first GHC flag to be a -f flag then
                  -- you can pass -- first
-                 "--" : args -> (Nothing, args)
-                 args        -> (Nothing, args)
+                 "--" : args' -> (Nothing, args')
+                 _            -> (Nothing, args)
     where f ghc args' = -- If there is another -f flag later on then
                         -- that overrules the one that we've already
                         -- found
@@ -82,9 +82,16 @@ doIt ghc args = do
                  hClose h
                  doIt ghc (ghc_args ++ [filename])
         filename : prog_args -> do
-            let xflag = if takeExtension filename == ".lhs"
-                        then []
-                        else ["-x", "hs"]
+            -- If the file exists, and is not a .lhs file, then we
+            -- want to treat it as a .hs file.
+            --
+            -- If the file doesn't exist then GHC is going to look for
+            -- filename.hs and filename.lhs, and use the appropriate
+            -- type.
+            exists <- doesFileExist filename
+            let xflag = if exists && (takeExtension filename /= ".lhs")
+                        then ["-x", "hs"]
+                        else []
                 c1 = ":set prog " ++ show filename
                 c2 = ":main " ++ show prog_args
             res <- rawSystem ghc (["-ignore-dot-ghci"] ++