[Git][ghc/ghc][wip/romes/fix-6] Improve error message when reading invalid `.target` files

Rodrigo Mesquita (@alt-romes) gitlab at gitlab.haskell.org
Thu Nov 16 16:13:53 UTC 2023



Rodrigo Mesquita pushed to branch wip/romes/fix-6 at Glasgow Haskell Compiler / GHC


Commits:
bf20564d by Rodrigo Mesquita at 2023-11-16T16:13:44+00: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/bf20564d64d09c1fb49352800c308760ba2d0c05

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bf20564d64d09c1fb49352800c308760ba2d0c05
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/20231116/5a926abb/attachment-0001.html>


More information about the ghc-commits mailing list