alanz / ghc-exactprint

GHC version of haskell-src-exts exactPrint
BSD 3-Clause "New" or "Revised" License
70 stars 32 forks source link

Parse/Print roundtrip loses shebang unless file path is in the form of "dir/file.hs" #92

Closed zliu41 closed 4 years ago

zliu41 commented 4 years ago

First reported in https://github.com/ndmitchell/hlint/issues/1122 by @pbrisbin

Suppose the content of Foo.hs is

#!/usr/bin/env stack
{- stack --resolver lts-16.10 script -}
module Main (main) where
main :: IO ()
main = putStrLn "hi"

In the following program, only the first way of parsing Foo.hs preserves the shebang.

import Language.Haskell.GHC.ExactPrint.Parsers
import Language.Haskell.GHC.ExactPrint.Print

main :: IO ()
main = do
  Right (as, m) <- parseModule "test/Foo.hs"   -- preserves shebang
  Right (as, m) <- parseModule "./test/Foo.hs" -- loses shebang
  Right (as, m) <- parseModule "Foo.hs"        -- loses shebang
  Right (as, m) <- parseModule "./Foo.hs"      -- loses shebang
  Right (as, m) <- parseModule "/tmp/Foo.hs"   -- loses shebang
  putStrLn $ exactPrint m as