From f9df468bec96a1cfe0a266bddc9d5b61ec7ca57f Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Tue, 16 Nov 2021 20:12:29 +0700 Subject: [PATCH] Add `Paths_` module to `autogen-modules` when `cabal-version >= 2` --- CHANGELOG.md | 5 +++- src/Hpack/Config.hs | 42 ++++++++++++++++++++++++-- test/EndToEndSpec.hs | 70 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eda4643a..883d6e14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Changes in 0.34.6 + - Add `Paths_` module to `autogen-modules` when `cabal-version >= 2` + ## Changes in 0.34.5 - Compatibility with `aeson-2.*` @@ -11,7 +14,7 @@ - Reject empty `then` / `else` sections (see #362) - Omit conditionals that are always `false` from generated `.cabal` file (see #404) - - Infer correct `cabal-version` when `Path_` is used with `RebindableSyntax` + - Infer correct `cabal-version` when `Paths_` is used with `RebindableSyntax` and `OverloadedStrings` or `OverloadedLists` (see #400) - Do not use indentation from any existing `.cabal` file if it is invalid (e.g. `0`) (fixes #252) diff --git a/src/Hpack/Config.hs b/src/Hpack/Config.hs index f5679894..e64427b4 100644 --- a/src/Hpack/Config.hs +++ b/src/Hpack/Config.hs @@ -676,15 +676,49 @@ verbatimValueToString = \ case VerbatimBool b -> show b VerbatimNull -> "" -determineCabalVersion :: Maybe (License SPDX.License) -> Package -> (Package, String) +addPathsModuleToGeneratedModules :: Package -> Version -> Package +addPathsModuleToGeneratedModules pkg cabalVersion + | cabalVersion < makeVersion [2] = pkg + | otherwise = pkg { + packageLibrary = fmap mapLibrary <$> packageLibrary pkg + , packageInternalLibraries = fmap mapLibrary <$> packageInternalLibraries pkg + , packageExecutables = fmap mapExecutable <$> packageExecutables pkg + , packageTests = fmap mapExecutable <$> packageTests pkg + , packageBenchmarks = fmap mapExecutable <$> packageBenchmarks pkg + } + where + pathsModule = pathsModuleFromPackageName (packageName pkg) + + mapLibrary :: Library -> Library + mapLibrary lib + | pathsModule `elem` getLibraryModules lib = lib { + libraryGeneratedModules = if pathsModule `elem` generatedModules then generatedModules else pathsModule : generatedModules + } + | otherwise = lib + where + generatedModules = libraryGeneratedModules lib + + mapExecutable :: Executable -> Executable + mapExecutable executable + | pathsModule `elem` executableOtherModules executable = executable { + executableGeneratedModules = if pathsModule `elem` generatedModules then generatedModules else pathsModule : generatedModules + } + | otherwise = executable + where + generatedModules = executableGeneratedModules executable + +determineCabalVersion :: Maybe (License SPDX.License) -> Package -> (Package, String, Maybe Version) determineCabalVersion inferredLicense pkg@Package{..} = ( pkg { packageVerbatim = deleteVerbatimField "cabal-version" packageVerbatim , packageLicense = formatLicense <$> license } - , "cabal-version: " ++ fromMaybe inferredCabalVersion verbatimCabalVersion ++ "\n\n" + , "cabal-version: " ++ effectiveCabalVersion ++ "\n\n" + , parseVersion effectiveCabalVersion ) where + effectiveCabalVersion = fromMaybe inferredCabalVersion verbatimCabalVersion + license = fmap prettyShow <$> (parsedLicense <|> inferredLicense) parsedLicense = parseLicense <$> packageLicense @@ -1184,7 +1218,9 @@ toPackage_ dir (Product g PackageConfig{..}) = do tell nameWarnings tell (formatMissingSourceDirs missingSourceDirs) - return (determineCabalVersion inferredLicense pkg) + + let (pkg_, renderedCabalVersion, cabalVersion) = determineCabalVersion inferredLicense pkg + return (maybe pkg_ (addPathsModuleToGeneratedModules pkg_) cabalVersion, renderedCabalVersion) where nameWarnings :: [String] packageName_ :: String diff --git a/test/EndToEndSpec.hs b/test/EndToEndSpec.hs index 6ebabd59..5a55c67a 100644 --- a/test/EndToEndSpec.hs +++ b/test/EndToEndSpec.hs @@ -79,6 +79,47 @@ spec = around_ (inTempDirectoryNamed "foo") $ do Paths_foo |] + context "when cabal-version is >= 2" $ do + it "adds Paths_ to autogen-modules" $ do + [i| + verbatim: + cabal-version: 2.0 + library: {} + |] `shouldRenderTo` (library [i| + other-modules: + Paths_foo + autogen-modules: + Paths_foo + |]) { packageCabalVersion = "2.0" } + + context "when Paths_ module is listed explicitly under generated-other-modules" $ do + it "adds Paths_ to autogen-modules only once" $ do + [i| + verbatim: + cabal-version: 2.0 + library: + generated-other-modules: Paths_foo + |] `shouldRenderTo` (library [i| + other-modules: + Paths_foo + autogen-modules: + Paths_foo + |]) { packageCabalVersion = "2.0" } + + context "when Paths_ module is listed explicitly under generated-exposed-modules" $ do + it "adds Paths_ to autogen-modules only once" $ do + [i| + verbatim: + cabal-version: 2.0 + library: + generated-exposed-modules: Paths_foo + |] `shouldRenderTo` (library [i| + exposed-modules: + Paths_foo + autogen-modules: + Paths_foo + |]) { packageCabalVersion = "2.0" } + context "when Paths_ is mentioned in a conditional that is always false" $ do it "does not add Paths_" $ do [i| @@ -100,6 +141,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do OverloadedStrings other-modules: Paths_foo + autogen-modules: + Paths_foo |]) {packageCabalVersion = "2.2"} context "when Paths_ is mentioned in a conditional that is always false" $ do @@ -455,6 +498,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do library other-modules: Paths_foo + autogen-modules: + Paths_foo cxx-options: -Wall default-language: Haskell2010 |]) {packageCabalVersion = "2.2"} @@ -469,6 +514,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do library other-modules: Paths_foo + autogen-modules: + Paths_foo cxx-options: -Wall default-language: Haskell2010 |]) {packageCabalVersion = "2.2"} @@ -671,6 +718,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do system-build-tools: g++ >= 5.4.0 |] `shouldRenderTo` (executable_ "foo" [i| + autogen-modules: + Paths_foo build-tools: g++ >=5.4.0 |]) {packageCabalVersion = "2.0"} @@ -690,6 +739,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do executable: dependencies: foo:bar |] `shouldRenderTo` (executable_ "foo" [i| + autogen-modules: + Paths_foo build-depends: foo:bar |]) {packageCabalVersion = "3.0"} @@ -800,6 +851,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do executable: cxx-options: -Wall |] `shouldRenderTo` (executable_ "foo" [i| + autogen-modules: + Paths_foo cxx-options: -Wall |]) {packageCabalVersion = "2.2"} @@ -815,6 +868,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do condition: True cxx-options: -Wall |] `shouldRenderTo` (executable_ "foo" [i| + autogen-modules: + Paths_foo if true if true if true @@ -829,6 +884,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do - foo.cc - cxxbits/*.cc |] `shouldRenderTo` (executable_ "foo" [i| + autogen-modules: + Paths_foo cxx-sources: foo.cc cxxbits/bar.cc @@ -979,6 +1036,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do library: signatures: Foo |] `shouldRenderTo` (library_ [i| + autogen-modules: + Paths_foo signatures: Foo |]) {packageCabalVersion = "2.0"} @@ -1008,6 +1067,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do |] `shouldRenderTo` (library [i| other-modules: Paths_foo + autogen-modules: + Paths_foo build-depends: foo mixins: @@ -1026,6 +1087,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do Foo other-modules: Paths_foo + autogen-modules: + Paths_foo hs-source-dirs: src |] @@ -1055,6 +1118,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do visibility: public other-modules: Paths_foo + autogen-modules: + Paths_foo |]) {packageCabalVersion = "3.0"} context "when inferring modules" $ do @@ -1239,6 +1304,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do Paths_foo Bar autogen-modules: + Paths_foo Foo Bar |]) {packageCabalVersion = "2.0"} @@ -1260,6 +1326,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do Paths_foo Other autogen-modules: + Paths_foo Exposed Other |]) {packageCabalVersion = "2.0"} @@ -1277,6 +1344,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do |] `shouldRenderTo` (library [i| other-modules: Paths_foo + autogen-modules: + Paths_foo hs-source-dirs: src if os(windows) @@ -1340,6 +1409,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do Paths_foo Foo autogen-modules: + Paths_foo Foo |]) {packageCabalVersion = "2.0"}