[Git][ghc/ghc][master] Improve error message when reading invalid `.target` files

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Tue Nov 21 23:33:28 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
f158a8d0 by Rodrigo Mesquita at 2023-11-21T18:32:59-05:00
Improve error message when reading invalid `.target` files

A `.target` file generated by ghc-toolchain or by configure can become
invalid if the target representation (`Toolchain.Target`) is changed
while the files are not re-generated by calling `./configure` or
`ghc-toolchain` again. There is also the issue of hadrian caching the
dependencies on `.target` files, which makes parsing fail when reading
reading the cached value if the representation has been updated.

This patch provides a better error message in both situations, moving
away from a terrible `Prelude.read: no parse` error that you would get
otherwise.

Fixes #24199

- - - - -


1 changed file:

- hadrian/src/Hadrian/Oracles/TextFile.hs


Changes:

=====================================
hadrian/src/Hadrian/Oracles/TextFile.hs
=====================================
@@ -163,14 +163,23 @@ textFileOracle = do
         putVerbose $ "| TargetFile oracle: reading " ++ quote file ++ "..."
         mtarget <- readMaybe <$> readFile' file
         case mtarget of
-          Nothing -> error $ "Failed to read a Toolchain.Target from " ++ quote file
+          Nothing -> error $ unlines ["Error parsing a Toolchain.Target from " ++ quote file,
+                                      "Perhaps the `.target` file is out of date.",
+                                      "Try re-running `./configure`."
+                                     ]
+
           Just target -> return (target :: Toolchain.Target)
     void $ addOracleCache $ \(TargetFile file) -> tf file
 
 -- Orphan instances for (ShakeValue Toolchain.Target)
 instance Binary Toolchain.Target where
-  put = put  . show
-  get = read <$> get
+  put = put . show
+  get = fromMaybe (error $ unlines ["Error parsing a toolchain `.target` file from its binary representation in hadrian.",
+                                    "This is likely caused by a stale hadrian/shake cache",
+                                    "which has saved an old `.target` file that can't be parsed",
+                                    "into a more recent `Toolchain.Target`. It is recommended to reset",
+                                    "by running `./hadrian/build clean`."
+                                   ]) . readMaybe <$> get
 
 instance Hashable Toolchain.Target where
   hashWithSalt s = hashWithSalt s . show



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f158a8d08fd21fb0778593de3dd1426650e844d3

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f158a8d08fd21fb0778593de3dd1426650e844d3
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20231121/a6638a90/attachment-0001.html>


More information about the ghc-commits mailing list