From bdbe6b8ff69e5551ecaff9229e80880b7cc2c925 Mon Sep 17 00:00:00 2001
From: GuillaumedeVolpiano <xavier@wheredoibegin.fr>
Date: Mon, 2 Jun 2025 16:55:03 +0200
Subject: [PATCH] cabal-doctest

doctest pulls in the out-of-scope packages if they are installed on the
system. The best workaround is to use migrate to cabal-doctest.

---
 Setup.hs            | 20 ++++++++++++++++++++
 examples/doctest.hs | 35 ++++++++++++++++++++++++++++++-----
 generic-lens.cabal  | 11 ++++++++++-
 3 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/Setup.hs b/Setup.hs
index 9a994af..e112a6d 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,2 +1,22 @@
+{-# LANGUAGE CPP #-}
+module Main (main) where
+
+#ifndef MIN_VERSION_cabal_doctest
+#define MIN_VERSION_cabal_doctest(x,y,z) 0
+#endif
+
+#if MIN_VERSION_cabal_doctest(1,0,0)
+
+import Distribution.Extra.Doctest ( defaultMainWithDoctests )
+
+main :: IO ()
+main = defaultMainWithDoctests "doctests"
+
+#else
+
 import Distribution.Simple
+
+main :: IO ()
 main = defaultMain
+
+#endif
diff --git a/examples/doctest.hs b/examples/doctest.hs
index 5c50730..0298732 100644
--- a/examples/doctest.hs
+++ b/examples/doctest.hs
@@ -1,6 +1,31 @@
 import Test.DocTest
-main
-  = doctest
-      [ "src"
-      , "examples"
-      ]
+
+import GHC.IO.Encoding (setLocaleEncoding)
+import System.Directory (getCurrentDirectory, makeAbsolute)
+import System.FilePath.Posix ((</>))
+import System.IO (utf8)
+import Build_doctests (pkgs)
+ 
+main :: IO ()
+main = do
+    setLocaleEncoding utf8
+    pwd    <- getCurrentDirectory
+    prefix <- makeAbsolute pwd
+
+    let customFlags =
+            [ "-package-env=-"
+            , "-hide-all-packages"
+            , "-no-global-package-db"
+            , "-no-user-package-db"
+            , "-package-db=" ++ prefix </> "dist/package.conf.inplace"
+            , "-package-db=" ++ prefix </> "package.conf.d"
+            ]
+
+    let modules =
+            [ "src"
+            , "examples"
+            ]
+
+    let args = customFlags ++ pkgs ++ modules
+
+    doctest args
diff --git a/generic-lens.cabal b/generic-lens.cabal
index 9dbdd73..217fcb2 100644
--- a/generic-lens.cabal
+++ b/generic-lens.cabal
@@ -13,7 +13,7 @@ license-file:         LICENSE
 author:               Csongor Kiss
 maintainer:           kiss.csongor.kiss@gmail.com
 category:             Generics, Records, Lens
-build-type:           Simple
+build-type:           Custom

 tested-with:
 
@@ -20,6 +20,12 @@ extra-source-files:   examples/StarWars.hs
                     , examples/Examples.hs
                     , ChangeLog.md
 
+custom-setup
+    setup-depends:
+        base
+      , Cabal
+      , cabal-doctest
+
 library
   exposed-modules:    Data.Generics.Wrapped
                     , Data.Generics.Product
@@ -103,5 +109,8 @@ test-suite doctests
   main-is:            doctest.hs
   build-depends:      base >4 && <5
                     , doctest
+                    , directory
+                    , filepath
+                    , base-compat
                     , lens
   hs-source-dirs:     examples
-- 
2.49.0

