Fix relative imports for dhall freeze --inplace (#619)

Fixes https://github.com/dhall-lang/dhall-haskell/issues/617

This changes `dhall freeze` to use the directory of the supplied file path as
the root import instead of using ".".  This fixes the behavior of relative
imports when the root file is not located in the current working directory.
This commit is contained in:
Gabriel Gonzalez 2018-10-04 05:56:10 -07:00 committed by GitHub
parent a83856431e
commit a22aa79d19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,16 +27,18 @@ import qualified Data.Text.IO
import qualified Dhall.Core
import qualified Dhall.Import
import qualified Dhall.TypeCheck
import qualified System.FilePath
import qualified System.IO
readInput :: Maybe FilePath -> IO Text
readInput = maybe Data.Text.IO.getContents Data.Text.IO.readFile
-- | Retrieve an `Import` and update the hash to match the latest contents
hashImport :: ProtocolVersion -> Import -> IO Import
hashImport _protocolVersion import_ = do
let status =
set protocolVersion _protocolVersion (Dhall.Import.emptyStatus ".")
hashImport
:: FilePath
-- ^ Current working directory
-> ProtocolVersion
-> Import
-> IO Import
hashImport directory _protocolVersion import_ = do
let status = set protocolVersion _protocolVersion (Dhall.Import.emptyStatus directory)
expression <- State.evalStateT (Dhall.Import.loadWith (Embed import_)) status
@ -90,9 +92,19 @@ freeze
-> ProtocolVersion
-> IO ()
freeze inplace _protocolVersion = do
text <- readInput inplace
(text, directory) <- case inplace of
Nothing -> do
text <- Data.Text.IO.getContents
return (text, ".")
Just file -> do
text <- Data.Text.IO.readFile file
return (text, System.FilePath.takeDirectory file)
(header, parsedExpression) <- parseExpr srcInfo text
frozenExpression <- traverse (hashImport _protocolVersion) parsedExpression
frozenExpression <- traverse (hashImport directory _protocolVersion) parsedExpression
writeExpr inplace (header, frozenExpression)
where
srcInfo = fromMaybe "(stdin)" inplace