Skip to content

Commit 4ba8f4d

Browse files
committed
Regenerate if a binding file is missing
1 parent a1d4fe7 commit 4ba8f4d

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

lib/Stack2nix.hs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,27 @@ packages2nix args pkgs =
161161
return (fromString pkg, fromString pkg $= mkPath False nix)
162162
(DVCS (Git url rev) _ subdirs) ->
163163
do hits <- forM subdirs $ \subdir -> liftIO $ cacheHits (argCacheFile args) url rev subdir
164+
let generateBindings =
165+
fetch (cabalFromPath url rev subdirs)
166+
(Source url rev UnknownHash) >>= \case
167+
(Just (DerivationSource{..}, genBindings)) -> genBindings derivHash
168+
_ -> return []
164169
if any null hits
165170
then
166171
-- If any of the subdirs were missing we need to fetch the files and
167172
-- generate the bindings.
168-
fetch (cabalFromPath url rev subdirs)
169-
(Source url rev UnknownHash) >>= \case
170-
(Just (DerivationSource{..}, genBindings)) -> genBindings derivHash
171-
_ -> return []
172-
else
173-
-- If the subdirs are all in the cache then the bindings should already be
174-
-- generated too.
175-
forM (concat hits) $ \( pkg, nix ) ->
176-
return (fromString pkg, fromString pkg $= mkPath False nix)
173+
generateBindings
174+
else do
175+
let allHits = concat hits
176+
(and <$> forM allHits (\( _, nix ) -> doesFileExist (argOutputDir args </> nix))) >>= \case
177+
False ->
178+
-- One or more of the generated binding files are missing
179+
generateBindings
180+
True ->
181+
-- If the subdirs are all in the cache then the bindings should already be
182+
-- generated too.
183+
forM allHits $ \( pkg, nix ) ->
184+
return (fromString pkg, fromString pkg $= mkPath False nix)
177185
_ -> return []
178186
where relPath = shortRelativePath (argOutputDir args) (dropFileName (argStackYaml args))
179187
cabalFromPath
@@ -205,7 +213,10 @@ packages2nix args pkgs =
205213
createDirectoryIfMissing True (takeDirectory nixFile)
206214
writeDoc nixFile =<<
207215
prettyNix <$> cabal2nix True (argDetailLevel args) src cabalFile
208-
appendCache (argCacheFile args) url rev subdir sha256 pkg nix
216+
-- Only update the cache if there is not already a record
217+
cacheHits (argCacheFile args) url rev subdir >>= \case
218+
[hit] | hit == (pkg, nix) -> return ()
219+
_ -> appendCache (argCacheFile args) url rev subdir sha256 pkg nix
209220
return (fromString pkg, fromString pkg $= mkPath False nix)
210221

211222
defaultNixContents :: String

0 commit comments

Comments
 (0)