Skip to content

Commit

Permalink
Prettyprint unlisted module warnings #2650
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsloan committed Sep 28, 2016
1 parent ebfec42 commit 0337a05
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 62 deletions.
46 changes: 31 additions & 15 deletions src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Stack/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/Stack/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
44 changes: 1 addition & 43 deletions src/Stack/Types/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down

0 comments on commit 0337a05

Please sign in to comment.