adjustFileName :: GHC.FastString -> GHC.FastString -> ErrorInfo -> IO ErrorInfo
adjustFileName fname fpath info = case info ^. location of
  (GHC.UnhelpfulSpan _) -> return $ inOtherFile id
  (GHC.RealSrcSpan span') -> if GHC.srcSpanFile span' == fname then return $ info & location .~ fileSrcSpan span' else do
    otherFile <- canonicalizePath $ GHC.unpackFS $ GHC.srcSpanFile span'
    return $ inOtherFile $ (Outputable.$$) $ Outputable.text $ otherFile ++ ": "
  where inOtherFile f = info & messageChanges . mapped %~ f
                             & location .~ firstLineSpan
        firstLineSpan = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath 1 1) (GHC.mkRealSrcLoc fpath 1 1)
        fileSrcSpan span' = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath lineStart colStart) (GHC.mkRealSrcLoc fpath lineEnd colEnd)
          where colStart = GHC.srcSpanStartCol span'
                colEnd = GHC.srcSpanEndCol span'
                lineStart = GHC.srcSpanStartLine span'
                lineEnd = GHC.srcSpanEndLine span'

#if __GLASGOW_HASKELL__ >= 706
adjustFileName :: GHC.FastString -> GHC.FastString -> ErrorInfo -> IO ErrorInfo
adjustFileName fname fpath info = case info ^. location of
  (GHC.UnhelpfulSpan _) -> return $ inOtherFile id
  (GHC.RealSrcSpan span') -> if GHC.srcSpanFile span' == fname then return $ info & location .~ fileSrcSpan span' else do
    otherFile <- canonicalizePath $ GHC.unpackFS $ GHC.srcSpanFile span'
    return $ inOtherFile $ (Outputable.$$) $ Outputable.text $ otherFile ++ ": "
  where inOtherFile f = info & messageChanges . mapped %~ f
                             & location .~ firstLineSpan
        firstLineSpan = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath 1 1) (GHC.mkRealSrcLoc fpath 1 1)
        fileSrcSpan span' = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath lineStart colStart) (GHC.mkRealSrcLoc fpath lineEnd colEnd)
          where colStart = GHC.srcSpanStartCol span'
                colEnd = GHC.srcSpanEndCol span'
                lineStart = GHC.srcSpanStartLine span'
                lineEnd = GHC.srcSpanEndLine span'

#if __GLASGOW_HASKELL__ >= 706
adjustFileName fname fpath info = case info ^. location of
  (GHC.UnhelpfulSpan _) -> return $ inOtherFile id
  (GHC.RealSrcSpan span') -> if GHC.srcSpanFile span' == fname then return $ info & location .~ fileSrcSpan span' else do
    otherFile <- canonicalizePath $ GHC.unpackFS $ GHC.srcSpanFile span'
    return $ inOtherFile $ (Outputable.$$) $ Outputable.text $ otherFile ++ ": "
  where inOtherFile f = info & messageChanges . mapped %~ f
                             & location .~ firstLineSpan
        firstLineSpan = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath 1 1) (GHC.mkRealSrcLoc fpath 1 1)
        fileSrcSpan span' = GHC.RealSrcSpan $ GHC.mkRealSrcSpan (GHC.mkRealSrcLoc fpath lineStart colStart) (GHC.mkRealSrcLoc fpath lineEnd colEnd)
          where colStart = GHC.srcSpanStartCol span'
                colEnd = GHC.srcSpanEndCol span'
                lineStart = GHC.srcSpanStartLine span'
                lineEnd = GHC.srcSpanEndLine span'

#if __GLASGOW_HASKELL__ >= 706