Skip to content

Commit 1f87c0f

Browse files
Fix #8645: no assertion warning on special modes (help, version, etc.) (#8647)
* Fix #8654: no assertion warning on special modes (help, version, etc.) This patch moves the `warnIfAssertionsAreEnabled` until after the main command line argument parsing, and then only prints it on regular operation, i.e. not when just the help or version of cabal was demanded, or the command line could not be parsed. Further, the warning is printed on stderr. The previous attempt to use `warn` does not work as expected, as the verbosity aperatshnik isn't in place yet, so the warning cannot be suppressed by -v0. Drive-by-shooting: straighten the convoluted control flow involving `maybeScriptAndArgs`. This IO computation was invoked "brutto" before, when it is actually only needed in *one* branch of the case distinction. * PR #8647: changelog, fix text of warning Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent d165b30 commit 1f87c0f

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

cabal-install/main/Main.hs

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{-# LANGUAGE CPP, ScopedTypeVariables #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE LambdaCase #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
24

35
-----------------------------------------------------------------------------
46
-- |
@@ -149,7 +151,7 @@ import Distribution.Simple.Program.Db (reconfigurePrograms)
149151
import qualified Distribution.Simple.Setup as Cabal
150152
import Distribution.Simple.Utils
151153
( cabalVersion, die', dieNoVerbosity, info, notice, topHandler
152-
, findPackageDesc, tryFindPackageDesc, createDirectoryIfMissingVerbose, warn )
154+
, findPackageDesc, tryFindPackageDesc, createDirectoryIfMissingVerbose )
153155
import Distribution.Text
154156
( display )
155157
import Distribution.Verbosity as Verbosity
@@ -162,7 +164,7 @@ import System.Environment (getArgs, getProgName)
162164
import System.FilePath ( dropExtension, splitExtension
163165
, takeExtension, (</>), (<.>) )
164166
import System.IO ( BufferMode(LineBuffering), hSetBuffering
165-
, stderr, stdout )
167+
, hPutStrLn, stderr, stdout )
166168
import System.Directory ( doesFileExist, getCurrentDirectory
167169
, withCurrentDirectory)
168170
import Data.Monoid (Any(..))
@@ -178,9 +180,6 @@ main = do
178180
-- This is especially important for CI and build systems.
179181
hSetBuffering stdout LineBuffering
180182

181-
-- Check whether assertions are enabled and print a warning in that case.
182-
warnIfAssertionsAreEnabled
183-
184183
-- If the locale encoding for CLI doesn't support all Unicode characters,
185184
-- printing to it may fail unless we relax the handling of encoding errors
186185
-- when writing to stderr and stdout.
@@ -189,20 +188,20 @@ main = do
189188
(args0, args1) <- break (== "--") <$> getArgs
190189
mainWorker =<< (++ args1) <$> expandResponse args0
191190

191+
-- | Check whether assertions are enabled and print a warning in that case.
192192
warnIfAssertionsAreEnabled :: IO ()
193193
warnIfAssertionsAreEnabled =
194194
assert False (return ()) `catch`
195-
(\(_e :: AssertionFailed) -> warn normal assertionsEnabledMsg)
195+
(\(_e :: AssertionFailed) -> hPutStrLn stderr assertionsEnabledMsg)
196+
-- Andreas, 2022-12-30, issue #8654:
197+
-- The verbosity machinery is not in place at this point (option -v not parsed),
198+
-- so instead of using function @warn@, we print straight to stderr.
196199
where
197200
assertionsEnabledMsg =
198-
"this is a debug build of cabal-install with assertions enabled."
201+
"Warning: this is a debug build of cabal-install with assertions enabled."
199202

200203
mainWorker :: [String] -> IO ()
201204
mainWorker args = do
202-
maybeScriptAndArgs <- case args of
203-
[] -> return Nothing
204-
(h:tl) -> (\b -> if b then Just (h:|tl) else Nothing) <$> CmdRun.validScript h
205-
206205
topHandler $
207206
case commandsRun (globalCommand commands) commands args of
208207
CommandHelp help -> printGlobalHelp help
@@ -216,9 +215,22 @@ mainWorker args = do
216215
-> printNumericVersion
217216
CommandHelp help -> printCommandHelp help
218217
CommandList opts -> printOptionsList opts
219-
CommandErrors errs -> maybe (printErrors errs) go maybeScriptAndArgs where
220-
go (script:|scriptArgs) = CmdRun.handleShebang script scriptArgs
221-
CommandReadyToGo action -> action globalFlags
218+
219+
CommandErrors errs -> do
220+
-- Check whether cabal is called from a script, like #!/path/to/cabal.
221+
case args of
222+
[] -> printErrors errs
223+
script : scriptArgs -> CmdRun.validScript script >>= \case
224+
False -> printErrors errs
225+
True -> do
226+
-- In main operation (not help, version etc.) print warning if assertions are on.
227+
warnIfAssertionsAreEnabled
228+
CmdRun.handleShebang script scriptArgs
229+
230+
CommandReadyToGo action -> do
231+
-- In main operation (not help, version etc.) print warning if assertions are on.
232+
warnIfAssertionsAreEnabled
233+
action globalFlags
222234

223235
where
224236
printCommandHelp help = do

changelog.d/pr-8647

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
synopsis: No up-front warning that assertions are on for special commands (help, version, numeric-version)
2+
packages: cabal-install
3+
prs: #8647
4+
issues: #8645
5+
6+
description: {
7+
8+
- When compiled with ghc option `-fno-ignore-assert`, `cabal-install` will issue a generic warning that assertions are on.
9+
This warning will no longer be emitted for special modes of operation like `cabal --numeric-version`, `--version` and help,
10+
and also not if `cabal` could not parse its command line.
11+
Further, the warning now goes to `stderr` rather than `stdout`.
12+
13+
}

0 commit comments

Comments
 (0)