Visitar URL original
Fix: Ignore incorrect hashes and save new hash to cache by german1608 · Pull Request #1793 · dhall-lang/dhall-haskell · GitHub
Skip to content

Fix: Ignore incorrect hashes and save new hash to cache - #1793

Merged
mergify[bot] merged 14 commits into
masterfrom
fix/ignore-incorrect-caches
May 17, 2020
Merged

mergify[bot] merged 14 commits into
masterfrom
fix/ignore-incorrect-caches

Conversation

@german1608

@german1608 german1608 commented May 14, 2020 •

Copy link
Copy Markdown
Collaborator

Fixes #1790

# First time download
$ echo 'https://prelude.dhall-lang.org/v16.0.0/JSON/Type sha256:40edbc9371979426df63e064333b02689b969c4cfbbccfa481216d2d1a6e9759' | dhall
∀(JSON : Type) →
∀ ( json
  : { array : List JSON → JSON
    , bool : Bool → JSON
    , double : Double → JSON
    , integer : Integer → JSON
    , null : JSON
    , object : List { mapKey : Text, mapValue : JSON } → JSON
    , string : Text → JSON
    }
  ) →
  JSON

# Corrupting cache
$ echo 'bla' > ~/.cache/dhall/122040edbc9371979426df63e064333b02689b969c4cfbbccfa481216d2d1a6e9759

# Second time download detects hashes changes and raises a warning
$ echo 'https://prelude.dhall-lang.org/v16.0.0/JSON/Type sha256:40edbc9371979426df63e064333b02689b969c4cfbbccfa481216d2d1a6e9759' | dhall

Warning: Import integrity check failed

Expected hash:

↳ 40edbc9371979426df63e064333b02689b969c4cfbbccfa481216d2d1a6e9759

Actual hash:

↳ 00e3261a6e0d79c329445acd540fb2b07187a0dcf6017065c8814010283ac67f

Interpreter has automatically fixed cached import
∀(JSON : Type) →
∀ ( json
  : { array : List JSON → JSON
    , bool : Bool → JSON
    , double : Double → JSON
    , integer : Integer → JSON
    , null : JSON
    , object : List { mapKey : Text, mapValue : JSON } → JSON
    , string : Text → JSON
    }
  ) →
  JSON

What I don't know is how to write tests for this yet

@sjakobi

sjakobi commented May 14, 2020

Copy link
Copy Markdown
Collaborator

The test I'm referencing in #1791 should cover this.

I believe we just need to update this test scaffolding

let setCache =
Turtle.export "XDG_CACHE_HOME" "dhall-lang/tests/import/cache"
let unsetCache = Turtle.unset "XDG_CACHE_HOME"
let load =
State.evalStateT (Test.Util.loadWith actualExpr) (Import.emptyStatus directoryString)
let runTest = do
if Turtle.filename (Turtle.fromText path) `elem`
[ "hashFromCacheA.dhall"
, "unit/asLocation/HashA.dhall"
]
then do
setCache
_ <- load
unsetCache
else do
_ <- load
return ()

It would be nice if we could actually follow the upstream instructions more closely here instead of conditionally setting the XDG_CACHE_HOME env var, but there might be some non-obvious trickiness involved…

BTW, it's a bit difficult to review PRs with so many style changes. Maybe you could commit those separately so it's easier to find the actual changes to the program logic.

@german1608

german1608 commented May 14, 2020 •

Copy link
Copy Markdown
Collaborator Author

BTW, it's a bit difficult to review PRs with so many style changes. Maybe you could commit those separately so it's easier to find the actual changes to the program logic.

Yes sorry! I tend to fix those kind of issues on the way but I agree, that adds a lot of noise to PRs. Should I change this PR or I can left it as it currently is?

@german1608

Copy link
Copy Markdown
Collaborator Author

I'm gonna change those and set this PR for ready for review

@german1608
german1608 force-pushed the fix/ignore-incorrect-caches branch from 6aac812 to 55de74e Compare May 14, 2020 13:13
Comment thread dhall/src/Dhall/Import.hs Resolved
Comment thread dhall/src/Dhall/Import.hs Resolved
Comment thread dhall/src/Dhall/Import.hs Resolved
@german1608
german1608 force-pushed the fix/ignore-incorrect-caches branch from c6ad661 to 6c05abe Compare May 14, 2020 23:09

@Gabriella439 Gabriella439 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great! I think the only thing that's missing is to add "unit/IgnorePoisonedCacheA.dhall" to this list:

let expectedFailures = []

Comment thread dhall/src/Dhall/Import.hs Resolved
@german1608
german1608 marked this pull request as ready for review May 15, 2020 03:43
@german1608

Copy link
Copy Markdown
Collaborator Author

I added the test as @Gabriel439 suggested in this comment without actually understanding how that code works. Not the way I'd like to, but prefer to spend time understanding how it works while you review the PR.

@sjakobi I fixed imports style but not other fixes as remove redundants "do" or parenthesis removes because there were so many and I got messy doing so, but I'll try on my next non-style PRs to avoid doing so.

@Gabriella439

Copy link
Copy Markdown
Collaborator

@german1608: Whoops, I got confused linked to the wrong place to add the test. The place to actually add the test is here:

[ "hashFromCacheA.dhall"
, "unit/asLocation/HashA.dhall"
]

@german1608

Copy link
Copy Markdown
Collaborator Author

@Gabriel439 don't worry! I'll update it

@sjakobi sjakobi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a few comments.

I like this imports style BTW! :)

Comment thread dhall/src/Dhall/Import.hs Resolved
Comment thread dhall/src/Dhall/Import.hs Resolved
@german1608

Copy link
Copy Markdown
Collaborator Author

I think this is ready for review

@german1608

Copy link
Copy Markdown
Collaborator Author

@sjakobi will this PR fix #1791 ?

@sjakobi

sjakobi commented May 15, 2020

Copy link
Copy Markdown
Collaborator

@sjakobi will this PR fix #1791 ?

Hopefully! :)

Does running the IgnorePoisoned test dirty the git repo though? If so, we should give the recommendation to treat it as read-only a try. Or alternatively use a temporary directory for the cache!?

Gabriella439
Gabriella439 previously approved these changes May 15, 2020
Comment thread dhall/src/Dhall/Import.hs Resolved
@german1608

Copy link
Copy Markdown
Collaborator Author

@sjakobi will this PR fix #1791 ?

Hopefully! :)

Does running the IgnorePoisoned test dirty the git repo though? If so, we should give the recommendation to treat it as read-only a try. Or alternatively use a temporary directory for the cache!?

I forgot to check that. Let me see

@german1608

Copy link
Copy Markdown
Collaborator Author

@sjakobi there was a bug when checking which files to set and unset cache env variables.

Looks like Turtle.filename returns the filename of a path, so unit/asLocation/HashA.dhall and (new) unit/IgnorePoisonedCacheA.dhall checks didn't do anything since the filename of them are just HashA.dhall and unit/IgnorePoisonedCacheA.dhall.

I've fixed that bug using Turtle.match and Turtle.ends and yes, a new file is created on the cache. I'll try to create a temp dir for each cache run copying from the cache on the dhall-lang submodule

Comment on lines -81 to +98
let runTest = do
if Turtle.filename (Turtle.fromText path) `elem`
[ "hashFromCacheA.dhall"
, "unit/asLocation/HashA.dhall"
]
then do
setCache
_ <- load
let usesCache = [ "hashFromCacheA.dhall"
, "unit/asLocation/HashA.dhall"
, "unit/IgnorePoisonedCacheA.dhall"]

let endsIn path' = not $ null $ Turtle.match (Turtle.ends path') path

let buildNewCache = do
tempdir <- Turtle.mktempdir "/tmp" "dhall-cache"
Turtle.liftIO $ Turtle.cptree originalCache tempdir
return tempdir

let runTest =
if any endsIn usesCache
then Turtle.runManaged $ do
cacheDir <- buildNewCache
setCache $ Turtle.format Turtle.fp cacheDir
_ <- Turtle.liftIO load

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Take a look at this please. This is how I managed to follow import tests suggestion from dhall-lang repo. I created a temp dir for each file test that may write to the cache

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry for my late response, @german1608!

Thanks for fixing this properly! :)

@german1608
german1608 requested a review from Gabriella439 May 15, 2020 17:04
@german1608
german1608 dismissed Gabriella439’s stale review May 15, 2020 17:05

Please check my last comment because its something that I added after your approval

@Gabriella439 Gabriella439 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great! Just one minor comment

Thank you very much for doing this 🙂

let endsIn path' = not $ null $ Turtle.match (Turtle.ends path') path

let buildNewCache = do
tempdir <- Turtle.mktempdir "/tmp" "dhall-cache"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's slightly better if this use the TMPDIR environment variable if set (falling back to "/tmp" if unset), in order to interoperate better with Nix. The reason why is that Nix sets TMPDIR during a build so that the build doesn't need to write outside of the temporary build directory.

@mergify
mergify Bot merged commit 0a8ade0 into master May 17, 2020
@mergify
mergify Bot deleted the fix/ignore-incorrect-caches branch May 17, 2020 16:56
german1608 added a commit that referenced this pull request May 17, 2020
german1608 added a commit that referenced this pull request May 17, 2020
german1608 added a commit that referenced this pull request May 22, 2020
mergify Bot pushed a commit that referenced this pull request May 22, 2020
…#1799)

* document --omissible-lists in module header

* report json path on mismatch types

* regroup imports

* add tasty-silver to dhall-json module

* add test for error message path

* rename test files

* add test for error at root of JSON

* add tests for arrays

* add another test

* fixes #1793

* show JSON path on extra key error

* rename test file

* show json path on unhandled keys

* add test for missingKey error

* fix missingKey.golden

* show json path in missingKey error

* fix nokeyvalmap error message

* split lines

* add tasty-silver to dhall-yaml

* use goldenVsAction

* remove .output.txt files

* add type signature to `loop` function

* rename qualified import names

* update doc comments

* add newline to .json test files

* move test folders to `data/error`

* add generid-deriving.nix

* add golden files to Extra-Source-Files

* add ListLike.nix

* add tasty-silver.nix

* remove ListLike.nix

* Revert "remove ListLike.nix"

This reverts commit d2babf7.

* add newline to unhandledKeysSchema.dhall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dhall should properly ignore incorrect cache content

3 participants