From 67161424be93de461ab277544a06b1ea9b83e49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Sat, 18 Oct 2025 23:31:32 +0200 Subject: [PATCH 1/3] Reword common stanzas in the cabal file generated by `cabal init` Following #11231, this commit reworks the common stanzas present in the cabal file generated by `cabal init`. --- .../Distribution/Client/Init/FileCreators.hs | 4 +- .../src/Distribution/Client/Init/Format.hs | 193 +++++++++++------- 2 files changed, 123 insertions(+), 74 deletions(-) diff --git a/cabal-install/src/Distribution/Client/Init/FileCreators.hs b/cabal-install/src/Distribution/Client/Init/FileCreators.hs index 15a03c8a7d0..5072161fcc9 100644 --- a/cabal-install/src/Distribution/Client/Init/FileCreators.hs +++ b/cabal-install/src/Distribution/Client/Init/FileCreators.hs @@ -73,6 +73,8 @@ writeProject (ProjectSettings opts pkgDesc libTarget exeTarget testTarget) let pkgFields = mkPkgDescription opts pkgDesc commonStanza = mkCommonStanza opts + extensionsStanza = mkExtensionsStanza opts + ghcOptionsStanza = mkGhcOptionsStanza opts libStanza <- prepareLibTarget opts libTarget exeStanza <- prepareExeTarget opts exeTarget @@ -80,7 +82,7 @@ writeProject (ProjectSettings opts pkgDesc libTarget exeTarget testTarget) (reusedCabal, cabalContents) <- writeCabalFile opts $ - pkgFields ++ [commonStanza, libStanza, exeStanza, testStanza] + pkgFields ++ [extensionsStanza, ghcOptionsStanza, commonStanza, libStanza, exeStanza, testStanza] when (null $ _pkgSynopsis pkgDesc) $ message opts T.Warning "No synopsis given. You should edit the .cabal file and add one." diff --git a/cabal-install/src/Distribution/Client/Init/Format.hs b/cabal-install/src/Distribution/Client/Init/Format.hs index ced1f74cf81..e1076bd5664 100644 --- a/cabal-install/src/Distribution/Client/Init/Format.hs +++ b/cabal-install/src/Distribution/Client/Init/Format.hs @@ -26,6 +26,9 @@ module Distribution.Client.Init.Format , mkExeStanza , mkTestStanza , mkPkgDescription + , mkExtensionsStanza + , mkGhcOptionsStanza + , mkRtsOptionsStanza ) where import Distribution.CabalSpecVersion @@ -133,72 +136,125 @@ mkCommonStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of [text "warnings"] [field "ghc-options" text "-Wall" [] False opts] +mkGhcOptionsStanza :: WriteOpts -> PrettyField FieldAnnotation +mkGhcOptionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> PrettyEmpty + _ -> + PrettySection + annNoComments + "common" + [text "ghc-options"] + [ field "ghc-options" text "-Wall -Widentities -Wcompat" [] False opts + ] + +mkRtsOptionsStanza :: WriteOpts -> PrettyField FieldAnnotation +mkRtsOptionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> PrettyEmpty + _ -> + PrettySection + annNoComments + "common" + [text "rts-options"] + [ field "ghc-options" text "-rtsopts -threaded \"-with-rtsopts=-N -T\"" [] False opts + ] + +mkExtensionsStanza :: WriteOpts -> PrettyField FieldAnnotation +mkExtensionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> PrettyEmpty + _ -> + PrettySection + annNoComments + "common" + [text "extensions"] + [ field "default-extensions" text "" [] False opts + , field "default-language" text "GHC2021" [] False opts + ] + +insertCommonStanzas :: WriteOpts -> [PrettyField FieldAnnotation] +insertCommonStanzas opts = + case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> [PrettyEmpty] + _ -> + [ field + "import" + (hsep . map text) + ["extensions"] + ["Common language extensions"] + False + opts + , field + "import" + (hsep . map text) + ["ghc-options"] + ["Common compiler warnings and optimisations"] + False + opts + , field + "import" + (hsep . map text) + ["rts-options"] + ["Common RTS options"] + False + opts + ] + mkLibStanza :: WriteOpts -> LibTarget -> PrettyField FieldAnnotation mkLibStanza opts (LibTarget srcDirs lang expMods otherMods exts deps tools) = PrettySection annNoComments (toUTF8BS "library") [] - [ case specHasCommonStanzas $ _optCabalSpec opts of - NoCommonStanzas -> PrettyEmpty - _ -> - field - "import" - (hsep . map text) - ["warnings"] - ["Import common warning flags."] - False - opts - , field - "exposed-modules" - formatExposedModules - (toList expMods) - ["Modules exported by the library."] - True - opts - , field - "other-modules" - formatOtherModules - otherMods - ["Modules included in this library but not exported."] - True - opts - , field - "other-extensions" - formatOtherExtensions - exts - ["LANGUAGE extensions used by modules in this package."] - True - opts - , field - "build-depends" - formatDependencyList - deps - ["Other library packages from which modules are imported."] - True - opts - , field - "hs-source-dirs" - formatHsSourceDirs - (makeSymbolicPath <$> srcDirs) - ["Directories containing source files."] - True - opts - , field - (buildToolTag opts) - formatDependencyList - tools - ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] - False - opts - , field - "default-language" - id - lang - ["Base language which the package is written in."] - True - opts - ] + (insertCommonStanzas opts ++ [ + field + "exposed-modules" + formatExposedModules + (toList expMods) + ["Modules exported by the library."] + True + opts + , field + "other-modules" + formatOtherModules + otherMods + ["Modules included in this library but not exported."] + True + opts + , field + "other-extensions" + formatOtherExtensions + exts + ["LANGUAGE extensions used by modules in this package."] + True + opts + , field + "build-depends" + formatDependencyList + deps + ["Other library packages from which modules are imported."] + True + opts + , field + "hs-source-dirs" + formatHsSourceDirs + (makeSymbolicPath <$> srcDirs) + ["Directories containing source files."] + True + opts + , field + (buildToolTag opts) + formatDependencyList + tools + ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] + False + opts + , field + "default-language" + id + lang + ["Base language which the package is written in."] + True + opts + ]) mkExeStanza :: WriteOpts -> ExeTarget -> PrettyField FieldAnnotation mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = @@ -206,17 +262,8 @@ mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = annNoComments (toUTF8BS "executable") [exeName] - [ case specHasCommonStanzas $ _optCabalSpec opts of - NoCommonStanzas -> PrettyEmpty - _ -> - field - "import" - (hsep . map text) - ["warnings"] - ["Import common warning flags."] - False - opts - , field + (insertCommonStanzas opts ++ [ + field "main-is" unsafeFromHs exeMain @@ -265,7 +312,7 @@ mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = ["Base language which the package is written in."] True opts - ] + ]) where exeName = pretty $ _optPkgName opts @@ -275,7 +322,7 @@ mkTestStanza opts (TestTarget testMain dirs lang otherMods exts deps tools) = annNoComments (toUTF8BS "test-suite") [suiteName] - [ case specHasCommonStanzas $ _optCabalSpec opts of + (insertCommonStanzas ++ [ case specHasCommonStanzas $ _optCabalSpec opts of NoCommonStanzas -> PrettyEmpty _ -> field From e933e260c3bf45d4eb8a0e701b43a1104775a322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Tue, 28 Oct 2025 11:04:14 +0100 Subject: [PATCH 2/3] Accept golden tests --- .gitignore | 2 + .../src/Distribution/Client/Init/Format.hs | 346 +++++++++--------- .../cabal-lib-and-exe-no-comments.golden | 11 +- .../cabal-lib-and-exe-with-comments.golden | 29 +- .../golden/cabal/cabal-lib-no-comments.golden | 7 +- .../cabal/cabal-lib-with-comments.golden | 19 +- .../cabal/cabal-test-suite-no-comments.golden | 3 + .../cabal-test-suite-with-comments.golden | 9 + .../exe/exe-build-tools-with-comments.golden | 10 +- .../golden/exe/exe-minimal-no-comments.golden | 4 +- .../init/golden/exe/exe-no-comments.golden | 4 +- .../exe-simple-minimal-with-comments.golden | 4 +- .../init/golden/exe/exe-with-comments.golden | 10 +- .../lib/lib-build-tools-with-comments.golden | 10 +- .../golden/lib/lib-minimal-no-comments.golden | 4 +- .../init/golden/lib/lib-no-comments.golden | 4 +- .../lib-simple-minimal-with-comments.golden | 4 +- .../golden/lib/lib-simple-no-comments.golden | 4 +- .../init/golden/lib/lib-with-comments.golden | 10 +- .../test/standalone-test-no-comments.golden | 3 + .../test/standalone-test-with-comments.golden | 9 + .../test-build-tools-with-comments.golden | 9 + .../test/test-minimal-no-comments.golden | 3 + .../init/golden/test/test-no-comments.golden | 3 + .../test-simple-minimal-with-comments.golden | 3 + .../golden/test/test-with-comments.golden | 9 + 26 files changed, 338 insertions(+), 195 deletions(-) diff --git a/.gitignore b/.gitignore index 628bbd917e7..4d1334d64e3 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,5 @@ bench.html # ignore the downloaded binary files scripts/release/binary-downloads/ + +tests/fixtures diff --git a/cabal-install/src/Distribution/Client/Init/Format.hs b/cabal-install/src/Distribution/Client/Init/Format.hs index e1076bd5664..b342a6ec4a9 100644 --- a/cabal-install/src/Distribution/Client/Init/Format.hs +++ b/cabal-install/src/Distribution/Client/Init/Format.hs @@ -174,14 +174,14 @@ insertCommonStanzas :: WriteOpts -> [PrettyField FieldAnnotation] insertCommonStanzas opts = case specHasCommonStanzas $ _optCabalSpec opts of NoCommonStanzas -> [PrettyEmpty] - _ -> + _ -> [ field "import" (hsep . map text) ["extensions"] ["Common language extensions"] False - opts + opts , field "import" (hsep . map text) @@ -204,57 +204,58 @@ mkLibStanza opts (LibTarget srcDirs lang expMods otherMods exts deps tools) = annNoComments (toUTF8BS "library") [] - (insertCommonStanzas opts ++ [ - field - "exposed-modules" - formatExposedModules - (toList expMods) - ["Modules exported by the library."] - True - opts - , field - "other-modules" - formatOtherModules - otherMods - ["Modules included in this library but not exported."] - True - opts - , field - "other-extensions" - formatOtherExtensions - exts - ["LANGUAGE extensions used by modules in this package."] - True - opts - , field - "build-depends" - formatDependencyList - deps - ["Other library packages from which modules are imported."] - True - opts - , field - "hs-source-dirs" - formatHsSourceDirs - (makeSymbolicPath <$> srcDirs) - ["Directories containing source files."] - True - opts - , field - (buildToolTag opts) - formatDependencyList - tools - ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] - False - opts - , field - "default-language" - id - lang - ["Base language which the package is written in."] - True - opts - ]) + ( insertCommonStanzas opts + ++ [ field + "exposed-modules" + formatExposedModules + (toList expMods) + ["Modules exported by the library."] + True + opts + , field + "other-modules" + formatOtherModules + otherMods + ["Modules included in this library but not exported."] + True + opts + , field + "other-extensions" + formatOtherExtensions + exts + ["LANGUAGE extensions used by modules in this package."] + True + opts + , field + "build-depends" + formatDependencyList + deps + ["Other library packages from which modules are imported."] + True + opts + , field + "hs-source-dirs" + formatHsSourceDirs + (makeSymbolicPath <$> srcDirs) + ["Directories containing source files."] + True + opts + , field + (buildToolTag opts) + formatDependencyList + tools + ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] + False + opts + , field + "default-language" + id + lang + ["Base language which the package is written in."] + True + opts + ] + ) mkExeStanza :: WriteOpts -> ExeTarget -> PrettyField FieldAnnotation mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = @@ -262,57 +263,58 @@ mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = annNoComments (toUTF8BS "executable") [exeName] - (insertCommonStanzas opts ++ [ - field - "main-is" - unsafeFromHs - exeMain - [".hs or .lhs file containing the Main module."] - True - opts - , field - "other-modules" - formatOtherModules - otherMods - ["Modules included in this executable, other than Main."] - True - opts - , field - "other-extensions" - formatOtherExtensions - exts - ["LANGUAGE extensions used by modules in this package."] - True - opts - , field - "build-depends" - formatDependencyList - deps - ["Other library packages from which modules are imported."] - True - opts - , field - "hs-source-dirs" - formatHsSourceDirs - (makeSymbolicPath <$> appDirs) - ["Directories containing source files."] - True - opts - , field - (buildToolTag opts) - formatDependencyList - tools - ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] - False - opts - , field - "default-language" - id - lang - ["Base language which the package is written in."] - True - opts - ]) + ( insertCommonStanzas opts + ++ [ field + "main-is" + unsafeFromHs + exeMain + [".hs or .lhs file containing the Main module."] + True + opts + , field + "other-modules" + formatOtherModules + otherMods + ["Modules included in this executable, other than Main."] + True + opts + , field + "other-extensions" + formatOtherExtensions + exts + ["LANGUAGE extensions used by modules in this package."] + True + opts + , field + "build-depends" + formatDependencyList + deps + ["Other library packages from which modules are imported."] + True + opts + , field + "hs-source-dirs" + formatHsSourceDirs + (makeSymbolicPath <$> appDirs) + ["Directories containing source files."] + True + opts + , field + (buildToolTag opts) + formatDependencyList + tools + ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] + False + opts + , field + "default-language" + id + lang + ["Base language which the package is written in."] + True + opts + ] + ) where exeName = pretty $ _optPkgName opts @@ -322,73 +324,75 @@ mkTestStanza opts (TestTarget testMain dirs lang otherMods exts deps tools) = annNoComments (toUTF8BS "test-suite") [suiteName] - (insertCommonStanzas ++ [ case specHasCommonStanzas $ _optCabalSpec opts of - NoCommonStanzas -> PrettyEmpty - _ -> - field - "import" - (hsep . map text) - ["warnings"] - ["Import common warning flags."] - False - opts - , field - "default-language" - id - lang - ["Base language which the package is written in."] - True - opts - , field - "other-modules" - formatOtherModules - otherMods - ["Modules included in this executable, other than Main."] - True - opts - , field - "other-extensions" - formatOtherExtensions - exts - ["LANGUAGE extensions used by modules in this package."] - True - opts - , field - "type" - text - "exitcode-stdio-1.0" - ["The interface type and version of the test suite."] - True - opts - , field - "hs-source-dirs" - formatHsSourceDirs - (makeSymbolicPath <$> dirs) - ["Directories containing source files."] - True - opts - , field - "main-is" - unsafeFromHs - testMain - ["The entrypoint to the test suite."] - True - opts - , field - "build-depends" - formatDependencyList - deps - ["Test dependencies."] - True - opts - , field - (buildToolTag opts) - formatDependencyList - tools - ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] - False - opts - ] + ( insertCommonStanzas opts + ++ [ case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> PrettyEmpty + _ -> + field + "import" + (hsep . map text) + ["warnings"] + ["Import common warning flags."] + False + opts + , field + "default-language" + id + lang + ["Base language which the package is written in."] + True + opts + , field + "other-modules" + formatOtherModules + otherMods + ["Modules included in this executable, other than Main."] + True + opts + , field + "other-extensions" + formatOtherExtensions + exts + ["LANGUAGE extensions used by modules in this package."] + True + opts + , field + "type" + text + "exitcode-stdio-1.0" + ["The interface type and version of the test suite."] + True + opts + , field + "hs-source-dirs" + formatHsSourceDirs + (makeSymbolicPath <$> dirs) + ["Directories containing source files."] + True + opts + , field + "main-is" + unsafeFromHs + testMain + ["The entrypoint to the test suite."] + True + opts + , field + "build-depends" + formatDependencyList + deps + ["Test dependencies."] + True + opts + , field + (buildToolTag opts) + formatDependencyList + tools + ["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."] + False + opts + ] + ) where suiteName = text $ unPackageName (_optPkgName opts) ++ "-test" diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-no-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-no-comments.golden index 1f2910867aa..9be65edb19f 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-no-comments.golden @@ -18,7 +18,9 @@ common warnings ghc-options: -Wall library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib -- other-modules: -- other-extensions: @@ -27,7 +29,9 @@ library default-language: Haskell98 executable y - import: warnings + import: extensions + import: ghc-options + import: rts-options main-is: Main.hs -- other-modules: -- other-extensions: @@ -39,6 +43,9 @@ executable y default-language: Haskell2010 test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 -- other-modules: diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-with-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-with-comments.golden index f2a7f5b946c..104742f299f 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-and-exe-with-comments.golden @@ -58,8 +58,14 @@ common warnings ghc-options: -Wall library - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- Modules exported by the library. exposed-modules: MyLib @@ -80,8 +86,14 @@ library default-language: Haskell98 executable y - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- .hs or .lhs file containing the Main module. main-is: Main.hs @@ -104,6 +116,15 @@ executable y default-language: Haskell2010 test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-no-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-no-comments.golden index d4a9ae182ae..2b12e1d4453 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-no-comments.golden @@ -18,7 +18,9 @@ common warnings ghc-options: -Wall library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib -- other-modules: -- other-extensions: @@ -27,6 +29,9 @@ library default-language: Haskell98 test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 -- other-modules: diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-with-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-with-comments.golden index ca37d882854..0ca18eeb566 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-lib-with-comments.golden @@ -58,8 +58,14 @@ common warnings ghc-options: -Wall library - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- Modules exported by the library. exposed-modules: MyLib @@ -80,6 +86,15 @@ library default-language: Haskell98 test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-no-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-no-comments.golden index ab5329bfab5..c364c8f2024 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-no-comments.golden @@ -18,6 +18,9 @@ common warnings ghc-options: -Wall test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 -- other-modules: diff --git a/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-with-comments.golden b/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-with-comments.golden index 068bf8812c4..0627ee74328 100644 --- a/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/cabal/cabal-test-suite-with-comments.golden @@ -58,6 +58,15 @@ common warnings ghc-options: -Wall test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings diff --git a/cabal-install/tests/fixtures/init/golden/exe/exe-build-tools-with-comments.golden b/cabal-install/tests/fixtures/init/golden/exe/exe-build-tools-with-comments.golden index ea22096155a..c40a621a8d7 100644 --- a/cabal-install/tests/fixtures/init/golden/exe/exe-build-tools-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/exe/exe-build-tools-with-comments.golden @@ -1,6 +1,12 @@ executable y - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- .hs or .lhs file containing the Main module. main-is: Main.hs diff --git a/cabal-install/tests/fixtures/init/golden/exe/exe-minimal-no-comments.golden b/cabal-install/tests/fixtures/init/golden/exe/exe-minimal-no-comments.golden index 4d520a4580c..55b4162506b 100644 --- a/cabal-install/tests/fixtures/init/golden/exe/exe-minimal-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/exe/exe-minimal-no-comments.golden @@ -1,5 +1,7 @@ executable y - import: warnings + import: extensions + import: ghc-options + import: rts-options main-is: Main.hs build-depends: base hs-source-dirs: exe diff --git a/cabal-install/tests/fixtures/init/golden/exe/exe-no-comments.golden b/cabal-install/tests/fixtures/init/golden/exe/exe-no-comments.golden index 3f5464cdaf7..c78885ed46c 100644 --- a/cabal-install/tests/fixtures/init/golden/exe/exe-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/exe/exe-no-comments.golden @@ -1,5 +1,7 @@ executable y - import: warnings + import: extensions + import: ghc-options + import: rts-options main-is: Main.hs -- other-modules: -- other-extensions: diff --git a/cabal-install/tests/fixtures/init/golden/exe/exe-simple-minimal-with-comments.golden b/cabal-install/tests/fixtures/init/golden/exe/exe-simple-minimal-with-comments.golden index 4d520a4580c..55b4162506b 100644 --- a/cabal-install/tests/fixtures/init/golden/exe/exe-simple-minimal-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/exe/exe-simple-minimal-with-comments.golden @@ -1,5 +1,7 @@ executable y - import: warnings + import: extensions + import: ghc-options + import: rts-options main-is: Main.hs build-depends: base hs-source-dirs: exe diff --git a/cabal-install/tests/fixtures/init/golden/exe/exe-with-comments.golden b/cabal-install/tests/fixtures/init/golden/exe/exe-with-comments.golden index 2669151b85d..592ba7ec105 100644 --- a/cabal-install/tests/fixtures/init/golden/exe/exe-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/exe/exe-with-comments.golden @@ -1,6 +1,12 @@ executable y - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- .hs or .lhs file containing the Main module. main-is: Main.hs diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-build-tools-with-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-build-tools-with-comments.golden index 609bbe48098..1f2290220f7 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-build-tools-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-build-tools-with-comments.golden @@ -1,6 +1,12 @@ library - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- Modules exported by the library. exposed-modules: MyLib diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-minimal-no-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-minimal-no-comments.golden index d1aa9941110..a1d59ab63e4 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-minimal-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-minimal-no-comments.golden @@ -1,5 +1,7 @@ library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib build-depends: base hs-source-dirs: src diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-no-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-no-comments.golden index 6d596b9a8ed..719138f2ac8 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-no-comments.golden @@ -1,5 +1,7 @@ library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib -- other-modules: -- other-extensions: diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-simple-minimal-with-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-simple-minimal-with-comments.golden index d1aa9941110..a1d59ab63e4 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-simple-minimal-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-simple-minimal-with-comments.golden @@ -1,5 +1,7 @@ library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib build-depends: base hs-source-dirs: src diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-simple-no-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-simple-no-comments.golden index 6d596b9a8ed..719138f2ac8 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-simple-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-simple-no-comments.golden @@ -1,5 +1,7 @@ library - import: warnings + import: extensions + import: ghc-options + import: rts-options exposed-modules: MyLib -- other-modules: -- other-extensions: diff --git a/cabal-install/tests/fixtures/init/golden/lib/lib-with-comments.golden b/cabal-install/tests/fixtures/init/golden/lib/lib-with-comments.golden index 3df8e499d8d..009a0f02489 100644 --- a/cabal-install/tests/fixtures/init/golden/lib/lib-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/lib/lib-with-comments.golden @@ -1,6 +1,12 @@ library - -- Import common warning flags. - import: warnings + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options -- Modules exported by the library. exposed-modules: MyLib diff --git a/cabal-install/tests/fixtures/init/golden/test/standalone-test-no-comments.golden b/cabal-install/tests/fixtures/init/golden/test/standalone-test-no-comments.golden index b881e0a6a30..b6c007b4b69 100644 --- a/cabal-install/tests/fixtures/init/golden/test/standalone-test-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/standalone-test-no-comments.golden @@ -1,4 +1,7 @@ test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 -- other-modules: diff --git a/cabal-install/tests/fixtures/init/golden/test/standalone-test-with-comments.golden b/cabal-install/tests/fixtures/init/golden/test/standalone-test-with-comments.golden index 02ed0cfd962..2d2391d3736 100644 --- a/cabal-install/tests/fixtures/init/golden/test/standalone-test-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/standalone-test-with-comments.golden @@ -1,4 +1,13 @@ test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings diff --git a/cabal-install/tests/fixtures/init/golden/test/test-build-tools-with-comments.golden b/cabal-install/tests/fixtures/init/golden/test/test-build-tools-with-comments.golden index 105afeb3465..e76c8205096 100644 --- a/cabal-install/tests/fixtures/init/golden/test/test-build-tools-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/test-build-tools-with-comments.golden @@ -1,4 +1,13 @@ test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings diff --git a/cabal-install/tests/fixtures/init/golden/test/test-minimal-no-comments.golden b/cabal-install/tests/fixtures/init/golden/test/test-minimal-no-comments.golden index 2a95da04c2e..364652831c8 100644 --- a/cabal-install/tests/fixtures/init/golden/test/test-minimal-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/test-minimal-no-comments.golden @@ -1,4 +1,7 @@ test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 type: exitcode-stdio-1.0 diff --git a/cabal-install/tests/fixtures/init/golden/test/test-no-comments.golden b/cabal-install/tests/fixtures/init/golden/test/test-no-comments.golden index b881e0a6a30..b6c007b4b69 100644 --- a/cabal-install/tests/fixtures/init/golden/test/test-no-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/test-no-comments.golden @@ -1,4 +1,7 @@ test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 -- other-modules: diff --git a/cabal-install/tests/fixtures/init/golden/test/test-simple-minimal-with-comments.golden b/cabal-install/tests/fixtures/init/golden/test/test-simple-minimal-with-comments.golden index 2a95da04c2e..364652831c8 100644 --- a/cabal-install/tests/fixtures/init/golden/test/test-simple-minimal-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/test-simple-minimal-with-comments.golden @@ -1,4 +1,7 @@ test-suite y-test + import: extensions + import: ghc-options + import: rts-options import: warnings default-language: Haskell2010 type: exitcode-stdio-1.0 diff --git a/cabal-install/tests/fixtures/init/golden/test/test-with-comments.golden b/cabal-install/tests/fixtures/init/golden/test/test-with-comments.golden index 02ed0cfd962..2d2391d3736 100644 --- a/cabal-install/tests/fixtures/init/golden/test/test-with-comments.golden +++ b/cabal-install/tests/fixtures/init/golden/test/test-with-comments.golden @@ -1,4 +1,13 @@ test-suite y-test + -- Common language extensions + import: extensions + + -- Common compiler warnings and optimisations + import: ghc-options + + -- Common RTS options + import: rts-options + -- Import common warning flags. import: warnings From 795c3db1bc440dfa476b89ee66bd098674a3d4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Tue, 28 Oct 2025 13:04:57 +0100 Subject: [PATCH 3/3] Don't insert rts options stanza imports for library --- .../src/Distribution/Client/Init/FileCreators.hs | 3 ++- cabal-install/src/Distribution/Client/Init/Format.hs | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cabal-install/src/Distribution/Client/Init/FileCreators.hs b/cabal-install/src/Distribution/Client/Init/FileCreators.hs index 5072161fcc9..73070ca091d 100644 --- a/cabal-install/src/Distribution/Client/Init/FileCreators.hs +++ b/cabal-install/src/Distribution/Client/Init/FileCreators.hs @@ -75,6 +75,7 @@ writeProject (ProjectSettings opts pkgDesc libTarget exeTarget testTarget) commonStanza = mkCommonStanza opts extensionsStanza = mkExtensionsStanza opts ghcOptionsStanza = mkGhcOptionsStanza opts + rtsOptionsStanza = mkRtsOptionsStanza opts libStanza <- prepareLibTarget opts libTarget exeStanza <- prepareExeTarget opts exeTarget @@ -82,7 +83,7 @@ writeProject (ProjectSettings opts pkgDesc libTarget exeTarget testTarget) (reusedCabal, cabalContents) <- writeCabalFile opts $ - pkgFields ++ [extensionsStanza, ghcOptionsStanza, commonStanza, libStanza, exeStanza, testStanza] + pkgFields ++ [extensionsStanza, ghcOptionsStanza, rtsOptionsStanza, commonStanza, libStanza, exeStanza, testStanza] when (null $ _pkgSynopsis pkgDesc) $ message opts T.Warning "No synopsis given. You should edit the .cabal file and add one." diff --git a/cabal-install/src/Distribution/Client/Init/Format.hs b/cabal-install/src/Distribution/Client/Init/Format.hs index b342a6ec4a9..98ea5a522ab 100644 --- a/cabal-install/src/Distribution/Client/Init/Format.hs +++ b/cabal-install/src/Distribution/Client/Init/Format.hs @@ -189,7 +189,14 @@ insertCommonStanzas opts = ["Common compiler warnings and optimisations"] False opts - , field + ] + +insertRtsOptionsStanza :: WriteOpts -> [PrettyField FieldAnnotation] +insertRtsOptionsStanza opts = + case specHasCommonStanzas $ _optCabalSpec opts of + NoCommonStanzas -> [PrettyEmpty] + _ -> + [ field "import" (hsep . map text) ["rts-options"] @@ -197,7 +204,6 @@ insertCommonStanzas opts = False opts ] - mkLibStanza :: WriteOpts -> LibTarget -> PrettyField FieldAnnotation mkLibStanza opts (LibTarget srcDirs lang expMods otherMods exts deps tools) = PrettySection @@ -264,6 +270,7 @@ mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) = (toUTF8BS "executable") [exeName] ( insertCommonStanzas opts + ++ insertRtsOptionsStanza opts ++ [ field "main-is" unsafeFromHs @@ -325,6 +332,7 @@ mkTestStanza opts (TestTarget testMain dirs lang otherMods exts deps tools) = (toUTF8BS "test-suite") [suiteName] ( insertCommonStanzas opts + ++ insertRtsOptionsStanza opts ++ [ case specHasCommonStanzas $ _optCabalSpec opts of NoCommonStanzas -> PrettyEmpty _ ->