Skip to content

Commit 86c4525

Browse files
9999yearsMikolaj
authored andcommitted
Line-buffer stdout and stderr
I though this was the default, but apparently not! This seems to fix output ordering issues. Noticed here: #10573 (comment)
1 parent 80a656b commit 86c4525

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

cabal-validate/src/Main.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Data.Text.Lazy as T (toStrict)
1414
import qualified Data.Text.Lazy.Encoding as T (decodeUtf8)
1515
import Data.Version (makeVersion, showVersion)
1616
import System.FilePath ((</>))
17+
import System.IO (BufferMode (LineBuffering), hSetBuffering, stderr, stdout)
1718
import System.Process.Typed (proc, readProcessStdout_)
1819

1920
import Cli (Compiler (..), HackageTests (..), Opts (..), parseOpts, whenVerbose)
@@ -24,6 +25,18 @@ import Step (Step (..), displayStep)
2425
-- | Entry-point for @cabal-validate@.
2526
main :: IO ()
2627
main = do
28+
-- You'd _think_ that line-buffering for stdout and stderr would be the
29+
-- default behavior, and the documentation makes gestures at it, but it
30+
-- appears to not be the case!
31+
--
32+
-- > For most implementations, physical files will normally be
33+
-- > block-buffered and terminals will normally be line-buffered.
34+
--
35+
-- However, on GitHub Actions and on my machine (macOS M1), adding these
36+
-- lines makes output appear in the correct order!
37+
hSetBuffering stdout LineBuffering
38+
hSetBuffering stderr LineBuffering
39+
2740
opts <- parseOpts
2841
printConfig opts
2942
printToolVersions opts
@@ -103,11 +116,11 @@ cabalListBinArgs opts = "list-bin" : cabalArgs opts
103116
cabalListBin :: Opts -> String -> IO FilePath
104117
cabalListBin opts target = do
105118
let args = cabalListBinArgs opts ++ [target]
106-
stdout <-
119+
stdout' <-
107120
readProcessStdout_ $
108121
proc (cabal opts) args
109122

110-
pure (T.unpack $ T.strip $ T.toStrict $ T.decodeUtf8 stdout)
123+
pure (T.unpack $ T.strip $ T.toStrict $ T.decodeUtf8 stdout')
111124

112125
-- | Get the RTS arguments for invoking test suites.
113126
--

0 commit comments

Comments
 (0)