diff --git a/ChangeLog.md b/ChangeLog.md index b9f6a1103d..4f461e9607 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -29,7 +29,7 @@ Bug fixes: [pantry#33](https://github.com/commercialhaskell/pantry/issues/33) * When building the sanity check for a new GHC install, make sure to clear `GHC_PACKAGE_PATH`. - +* Specifying GHC RTS flags in the `stack.yaml` no longer fails with an error. [#5568](https://github.com/commercialhaskell/stack/pull/5568) ## v2.7.1 diff --git a/src/Stack/Types/Build.hs b/src/Stack/Types/Build.hs index e92c2bf988..e4a6c684ec 100644 --- a/src/Stack/Types/Build.hs +++ b/src/Stack/Types/Build.hs @@ -629,7 +629,7 @@ configureOptsNoDir econfig bco deps isLocal package = concat flagNameString name) (Map.toList flags) , map T.unpack $ packageCabalConfigOpts package - , concatMap (\x -> [compilerOptionsCabalFlag wc, T.unpack x]) (packageGhcOptions package) + , processGhcOptions (packageGhcOptions package) , map ("--extra-include-dirs=" ++) (configExtraIncludeDirs config) , map ("--extra-lib-dirs=" ++) (configExtraLibDirs config) , maybe [] (\customGcc -> ["--with-gcc=" ++ toFilePath customGcc]) (configOverrideGccPath config) @@ -637,6 +637,45 @@ configureOptsNoDir econfig bco deps isLocal package = concat , ["--ghc-option=-fhide-source-paths" | hideSourcePaths cv] ] where + -- This function parses the GHC options that are providing in the + -- stack.yaml file. In order to handle RTS arguments correctly, we need + -- to provide the RTS arguments as a single argument. + processGhcOptions :: [Text] -> [String] + processGhcOptions args = + let + (preRtsArgs, mid) = + break ("+RTS" ==) args + (rtsArgs, end) = + break ("-RTS" ==) mid + fullRtsArgs = + case rtsArgs of + [] -> + -- This means that we didn't have any RTS args - no + -- `+RTS` - and therefore no need for a `-RTS`. + [] + _ -> + -- In this case, we have some RTS args. `break` + -- puts the `"-RTS"` string in the `snd` list, so + -- we want to append it on the end of `rtsArgs` + -- here. + -- + -- We're not checking that `-RTS` is the first + -- element of `end`. This is because the GHC RTS + -- allows you to omit a trailing -RTS if that's the + -- last of the arguments. This permits a GHC + -- options in stack.yaml that matches what you + -- might pass directly to GHC. + [T.unwords $ rtsArgs ++ ["-RTS"]] + -- We drop the first element from `end`, because it is always + -- either `"-RTS"` (and we don't want that as a separate + -- argument) or the list is empty (and `drop _ [] = []`). + postRtsArgs = + drop 1 end + newArgs = + concat [preRtsArgs, fullRtsArgs, postRtsArgs] + in + concatMap (\x -> [compilerOptionsCabalFlag wc, T.unpack x]) newArgs + wc = view (actualCompilerVersionL.to whichCompiler) econfig cv = view (actualCompilerVersionL.to getGhcVersion) econfig diff --git a/test/integration/tests/5180-ghc-rts-flags/Main.hs b/test/integration/tests/5180-ghc-rts-flags/Main.hs new file mode 100644 index 0000000000..e13dcd9ae4 --- /dev/null +++ b/test/integration/tests/5180-ghc-rts-flags/Main.hs @@ -0,0 +1,6 @@ +import StackTest + +main :: IO () +main = do + stack ["clean"] + stack ["build"] diff --git a/test/integration/tests/5180-ghc-rts-flags/files/files.cabal b/test/integration/tests/5180-ghc-rts-flags/files/files.cabal new file mode 100644 index 0000000000..8f21c6b6bd --- /dev/null +++ b/test/integration/tests/5180-ghc-rts-flags/files/files.cabal @@ -0,0 +1,11 @@ +name: files +version: 0.1.0.0 +build-type: Simple +cabal-version: >=1.10 + +library + hs-source-dirs: src + exposed-modules: Lib + build-depends: base >= 4.7 && < 5, mtl + default-language: Haskell2010 + diff --git a/test/integration/tests/5180-ghc-rts-flags/files/src/Lib.hs b/test/integration/tests/5180-ghc-rts-flags/files/src/Lib.hs new file mode 100644 index 0000000000..7685c7c4bf --- /dev/null +++ b/test/integration/tests/5180-ghc-rts-flags/files/src/Lib.hs @@ -0,0 +1,4 @@ +module Lib where + +main :: IO () +main = putStrLn "hello world" diff --git a/test/integration/tests/5180-ghc-rts-flags/files/stack.yaml b/test/integration/tests/5180-ghc-rts-flags/files/stack.yaml new file mode 100644 index 0000000000..30e82f3a00 --- /dev/null +++ b/test/integration/tests/5180-ghc-rts-flags/files/stack.yaml @@ -0,0 +1,6 @@ +resolver: lts-14.27 +packages: +- . + +ghc-options: + "$locals": -j8 +RTS -s -A128M