Skip to content

Add --no-cache flag #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dhall-json/src/Dhall/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ import Data.Monoid ((<>), mempty)
import Data.Text (Text)
import Data.Text.Prettyprint.Doc (Pretty)
import Dhall.Core (Binding(..), Expr)
import Dhall.Import (SemanticCacheMode(..))
import Dhall.TypeCheck (X)
import Dhall.Map (Map)
import Dhall.JSON.Util (pattern V)
Expand Down Expand Up @@ -1060,7 +1061,7 @@ codeToValue conversion specialDoubleMode mFilePath code = do
Nothing -> "."
Just fp -> System.FilePath.takeDirectory fp

resolvedExpression <- Dhall.Import.loadRelativeTo rootDirectory parsedExpression
resolvedExpression <- Dhall.Import.loadRelativeTo rootDirectory UseSemanticCache parsedExpression

_ <- Core.throws (Dhall.TypeCheck.typeOf resolvedExpression)

Expand Down
16 changes: 11 additions & 5 deletions dhall/src/Dhall/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module Dhall.Import (
, writeExpressionToSemanticCache
, assertNoImports
, Status
, SemanticCacheMode(..)
, Chained
, chainedImport
, chainedFromLocalHere
Expand Down Expand Up @@ -495,7 +496,10 @@ loadImportWithSemanticCache
loadImportWithSemanticCache
import_@(Chained (Import (ImportHashed (Just semanticHash) _) _)) = do
Status { .. } <- State.get
mCached <- liftIO $ fetchFromSemanticCache semanticHash
mCached <-
case _semanticCacheMode of
UseSemanticCache -> liftIO $ fetchFromSemanticCache semanticHash
IgnoreSemanticCache -> pure Nothing

case mCached of
Just bytesStrict -> do
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit further below we also writeToSemanticCache – IMHO it's not necessary to disable this, but we could.

Expand Down Expand Up @@ -973,13 +977,15 @@ loadWith expr₀ = case expr₀ of

-- | Resolve all imports within an expression
load :: Expr Src Import -> IO (Expr Src X)
load = loadRelativeTo "."
load = loadRelativeTo "." UseSemanticCache

-- | Resolve all imports within an expression, importing relative to the given
-- directory.
loadRelativeTo :: FilePath -> Expr Src Import -> IO (Expr Src X)
loadRelativeTo rootDirectory expression =
State.evalStateT (loadWith expression) (emptyStatus rootDirectory)
loadRelativeTo :: FilePath -> SemanticCacheMode -> Expr Src Import -> IO (Expr Src X)
loadRelativeTo rootDirectory semanticCacheMode expression =
State.evalStateT
(loadWith expression)
(emptyStatus rootDirectory) { _semanticCacheMode = semanticCacheMode }

encodeExpression
:: forall s
Expand Down
8 changes: 6 additions & 2 deletions dhall/src/Dhall/Import/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ data ImportSemantics = ImportSemantics
-- | `parent` imports (i.e. depends on) `child`
data Depends = Depends { parent :: Chained, child :: Chained }

data SemanticCacheMode = IgnoreSemanticCache | UseSemanticCache

-- | State threaded throughout the import process
data Status = Status
{ _stack :: NonEmpty Chained
Expand All @@ -72,6 +74,8 @@ data Status = Status
, _normalizer :: Maybe (ReifiedNormalizer X)

, _startingContext :: Context (Expr Src X)

, _semanticCacheMode :: SemanticCacheMode
}

-- | Initial `Status`, parameterised over the remote resolver, importing
Expand All @@ -85,12 +89,12 @@ emptyStatusWith _remote rootDirectory = Status {..}

_cache = Map.empty

_manager = Nothing

_normalizer = Nothing

_startingContext = Dhall.Context.empty

_semanticCacheMode = UseSemanticCache

prefix = if isRelative rootDirectory
then Here
else Absolute
Expand Down
33 changes: 26 additions & 7 deletions dhall/src/Dhall/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Data.Text.Prettyprint.Doc (Doc, Pretty)
import Data.Version (showVersion)
import Dhall.Core (Expr(Annot), Import, pretty)
import Dhall.Freeze (Intent(..), Scope(..))
import Dhall.Import (Imported(..), Depends(..))
import Dhall.Import (Imported(..), Depends(..), SemanticCacheMode(..))
import Dhall.Parser (Src)
import Dhall.Pretty (Ann, CharacterSet(..), annToAnsiStyle, layoutOpts)
import Dhall.TypeCheck (DetailedTypeError(..), TypeError, X)
Expand Down Expand Up @@ -86,7 +86,12 @@ data Options = Options

-- | The subcommands for the @dhall@ executable
data Mode
= Default { file :: Maybe FilePath, annotate :: Bool, alpha :: Bool }
= Default
{ file :: Maybe FilePath
, annotate :: Bool
, alpha :: Bool
, semanticCacheMode :: SemanticCacheMode
}
| Version
| Resolve { file :: Maybe FilePath, resolveMode :: Maybe ResolveMode }
| Type { file :: Maybe FilePath }
Expand Down Expand Up @@ -189,7 +194,7 @@ parseMode =
"text"
"Render a Dhall expression that evaluates to a Text literal"
(Text <$> optional parseFile)
<|> (Default <$> optional parseFile <*> parseAnnotate <*> parseAlpha)
<|> (Default <$> optional parseFile <*> parseAnnotate <*> parseAlpha <*> parseSemanticCacheMode)
where
argument =
fmap Data.Text.pack
Expand All @@ -215,6 +220,15 @@ parseMode =
<> Options.Applicative.help "Add a type annotation to the output"
)

parseSemanticCacheMode =
Options.Applicative.flag
UseSemanticCache
IgnoreSemanticCache
( Options.Applicative.long "no-cache"
<> Options.Applicative.help
"Handle protected imports as if the cache was empty"
)

parseResolveMode =
Options.Applicative.flag' (Just Dot)
( Options.Applicative.long "dot"
Expand Down Expand Up @@ -367,7 +381,8 @@ command (Options {..}) = do
Default {..} -> do
expression <- getExpression file

resolvedExpression <- Dhall.Import.loadRelativeTo (rootDirectory file) expression
resolvedExpression <-
Dhall.Import.loadRelativeTo (rootDirectory file) semanticCacheMode expression

inferredType <- Dhall.Core.throws (Dhall.TypeCheck.typeOf resolvedExpression)

Expand Down Expand Up @@ -438,7 +453,9 @@ command (Options {..}) = do
Resolve { resolveMode = Nothing, ..} -> do
expression <- getExpression file

resolvedExpression <- Dhall.Import.loadRelativeTo (rootDirectory file) expression
resolvedExpression <-
Dhall.Import.loadRelativeTo (rootDirectory file) UseSemanticCache expression

render System.IO.stdout resolvedExpression

Normalize {..} -> do
Expand All @@ -460,7 +477,8 @@ command (Options {..}) = do
Type {..} -> do
expression <- getExpression file

resolvedExpression <- Dhall.Import.loadRelativeTo (rootDirectory file) expression
resolvedExpression <-
Dhall.Import.loadRelativeTo (rootDirectory file) UseSemanticCache expression

inferredType <- Dhall.Core.throws (Dhall.TypeCheck.typeOf resolvedExpression)

Expand Down Expand Up @@ -572,7 +590,8 @@ command (Options {..}) = do
Text {..} -> do
expression <- getExpression file

resolvedExpression <- Dhall.Import.loadRelativeTo (rootDirectory file) expression
resolvedExpression <-
Dhall.Import.loadRelativeTo (rootDirectory file) UseSemanticCache expression

_ <- Dhall.Core.throws (Dhall.TypeCheck.typeOf (Annot resolvedExpression Dhall.Core.Text))

Expand Down