From 0337a05eb74e140f3f7a8f4ca91aae1212028dff Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Wed, 28 Sep 2016 16:13:04 -0700 Subject: [PATCH] Prettyprint unlisted module warnings #2650 --- src/Stack/Build/Execute.hs | 46 +++++++++++++++++++++++++------------- src/Stack/Package.hs | 2 -- src/Stack/PrettyPrint.hs | 8 +++++-- src/Stack/Types/Package.hs | 44 +----------------------------------- 4 files changed, 38 insertions(+), 62 deletions(-) diff --git a/src/Stack/Build/Execute.hs b/src/Stack/Build/Execute.hs index 8c87d08487..e16c6fa426 100644 --- a/src/Stack/Build/Execute.hs +++ b/src/Stack/Build/Execute.hs @@ -52,6 +52,7 @@ import Data.Monoid ((<>)) import Data.Set (Set) import qualified Data.Set as Set import Data.Streaming.Process hiding (callProcess, env) +import Data.String import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeUtf8) @@ -61,6 +62,7 @@ import Data.Tuple import qualified Distribution.PackageDescription as C import Distribution.System (OS (Windows), Platform (Platform)) +import qualified Distribution.Text as C import Language.Haskell.TH as TH (location) import Network.HTTP.Client.Conduit (HasHttpManager) import Path @@ -79,16 +81,17 @@ import Stack.Fetch as Fetch import Stack.GhcPkg import Stack.Package import Stack.PackageDump -import Stack.Types.GhcPkgId -import Stack.Types.PackageIdentifier -import Stack.Types.PackageName -import Stack.Types.Version -import Stack.Types.Config +import Stack.PrettyPrint import Stack.Types.Build -import Stack.Types.Package import Stack.Types.Compiler +import Stack.Types.Config +import Stack.Types.GhcPkgId import Stack.Types.Internal +import Stack.Types.Package +import Stack.Types.PackageIdentifier +import Stack.Types.PackageName import Stack.Types.StackT +import Stack.Types.Version import qualified System.Directory as D import System.Environment (getExecutablePath) import System.Exit (ExitCode (ExitSuccess)) @@ -1145,15 +1148,28 @@ singleBuild runInBase ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} in -- FIXME: only output these if they're in the build plan. preBuildTime <- modTime <$> liftIO getCurrentTime - let postBuildCheck succeeded = do - warnings <- checkForUnlistedFiles taskType preBuildTime pkgDir - let hasUnlistedModule = any $ \case - UnlistedModulesWarning{} -> True - _ -> False - when (not (null warnings)) $ $logInfo "" - when (not succeeded && hasUnlistedModule warnings) $ do - $logInfo "If the above build failed with a linker error or .so/DLL error, addressing these may help:" - mapM_ ($logWarn . ("Warning: " <>) . T.pack . show) warnings + let postBuildCheck _succeeded = do + mlocalWarnings <- case taskType of + TTLocal lp -> do + warnings <- checkForUnlistedFiles taskType preBuildTime pkgDir + return (Just (lpCabalFile lp, warnings)) + _ -> return Nothing + -- NOTE: once + -- https://github.com/commercialhaskell/stack/issues/2649 + -- is resolved, we will want to partition the warnings + -- based on variety, and output in different lists. + let showModuleWarning (UnlistedModulesWarning mcomp modules) = + "- In" <+> + maybe "the library component" (\c -> fromString c <+> "component") mcomp <> + ":" <> line <> + indent 4 (mconcat $ intersperse line $ map (goodGreen . fromString . C.display) modules) + forM_ mlocalWarnings $ \(cabalfp, warnings) -> do + when (not (null warnings)) $ $prettyWarn $ + "The following modules should be added to exposed-modules or other-modules in" <+> + display cabalfp <> ":" <> line <> + indent 4 (mconcat $ map showModuleWarning warnings) <> + line <> line <> + "Missing modules in the cabal file are likely to cause undefined reference errors from the linker, along with other problems." () <- announce ("build" <> annSuffix) config <- asks getConfig diff --git a/src/Stack/Package.hs b/src/Stack/Package.hs index 68a258252d..40e392932f 100644 --- a/src/Stack/Package.hs +++ b/src/Stack/Package.hs @@ -931,12 +931,10 @@ resolveFilesAndDeps component dirs names0 exts = do let unlistedModules = foundModules `S.difference` S.fromList (mapMaybe dotCabalModule names0) - cabalfp <- asks fst return $ if S.null unlistedModules then [] else [ UnlistedModulesWarning - cabalfp component (S.toList unlistedModules)] warnMissing _missingModules = do diff --git a/src/Stack/PrettyPrint.hs b/src/Stack/PrettyPrint.hs index e7d3830ec5..62214d74ef 100644 --- a/src/Stack/PrettyPrint.hs +++ b/src/Stack/PrettyPrint.hs @@ -26,6 +26,7 @@ module Stack.PrettyPrint import Control.Monad.Logger import Control.Monad.Reader +import Data.Monoid import Data.String (fromString) import qualified Data.Text as T import Language.Haskell.TH @@ -58,16 +59,19 @@ prettyInfo = do prettyWarn :: Q Exp prettyWarn = do loc <- location - [e| monadLoggerLog loc "" LevelWarn <=< displayAnsiIfPossible |] + [e| monadLoggerLog loc "" LevelWarn <=< displayAnsiIfPossible . (line <>) . (warningYellow "Warning:" <+>) |] prettyError :: Q Exp prettyError = do loc <- location - [e| monadLoggerLog loc "" LevelError <=< displayAnsiIfPossible |] + [e| monadLoggerLog loc "" LevelError <=< displayAnsiIfPossible . (line <>) . (errorRed "Error:" <+>) |] errorRed :: AnsiDoc -> AnsiDoc errorRed = dullred +warningYellow :: AnsiDoc -> AnsiDoc +warningYellow = yellow + goodGreen :: AnsiDoc -> AnsiDoc goodGreen = green diff --git a/src/Stack/Types/Package.hs b/src/Stack/Types/Package.hs index 686e464570..e96cb56f62 100644 --- a/src/Stack/Types/Package.hs +++ b/src/Stack/Types/Package.hs @@ -38,7 +38,6 @@ import Distribution.ModuleName (ModuleName) import Distribution.Package hiding (Package,PackageName,packageName,packageVersion,PackageIdentifier) import Distribution.PackageDescription (TestSuiteInterface) import Distribution.System (Platform (..)) -import Distribution.Text (display) import GHC.Generics (Generic) import Path as FL import Prelude @@ -161,7 +160,7 @@ instance Show GetPackageFiles where -- | Warning generated when reading a package data PackageWarning - = UnlistedModulesWarning (Path Abs File) (Maybe String) [ModuleName] + = UnlistedModulesWarning (Maybe String) [ModuleName] -- ^ Modules found that are not listed in cabal file -- TODO: bring this back - see @@ -171,47 +170,6 @@ data PackageWarning -- ^ Modules not found in file system, which are listed in cabal file -} -instance Show PackageWarning where - show (UnlistedModulesWarning cabalfp component [unlistedModule]) = - concat - [ "module not listed in " - , toFilePath (filename cabalfp) - , case component of - Nothing -> " for library" - Just c -> " for '" ++ c ++ "'" - , " component (add to other-modules): " - , display unlistedModule] - show (UnlistedModulesWarning cabalfp component unlistedModules) = - concat - [ "modules not listed in " - , toFilePath (filename cabalfp) - , case component of - Nothing -> " for library" - Just c -> " for '" ++ c ++ "'" - , " component (add to other-modules):\n " - , intercalate "\n " (map display unlistedModules)] - {- - show (MissingModulesWarning cabalfp component [missingModule]) = - concat - [ "module listed in " - , toFilePath (filename cabalfp) - , case component of - Nothing -> " for library" - Just c -> " for '" ++ c ++ "'" - , " component not found in filesystem: " - , display missingModule] - show (MissingModulesWarning cabalfp component missingModules) = - concat - [ "modules listed in " - , toFilePath (filename cabalfp) - , case component of - Nothing -> " for library" - Just c -> " for '" ++ c ++ "'" - , " component not found in filesystem:\n " - , intercalate "\n " (map display missingModules)] - -} - - -- | Package build configuration data PackageConfig = PackageConfig {packageConfigEnableTests :: !Bool -- ^ Are tests enabled?