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.

diff -ur a/Setup.hs b/Setup.hs
--- a/Setup.hs	2001-09-09 03:46:40.000000000 +0200
+++ b/Setup.hs	2025-02-15 12:18:35.424730809 +0100
@@ -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 "dhall-doctests"
+
+#else
+
 import Distribution.Simple
+
+main :: IO ()
 main = defaultMain
+
+#endif
diff -ur a/dhall.cabal b/dhall.cabal
--- a/dhall.cabal	2001-09-09 03:46:40.000000000 +0200
+++ b/dhall.cabal	2025-02-15 11:34:52.906095586 +0100
@@ -1,7 +1,7 @@
 Cabal-Version: 2.4
 Name: dhall
 Version: 1.42.2
-Build-Type: Simple
+Build-Type: Custom
 License: BSD-3-Clause
 License-File: LICENSE
 Copyright: 2017 Gabriella Gonzalez
@@ -274,6 +274,12 @@
 
     GHC-Options: -Wall -Wcompat -Wincomplete-uni-patterns -optP-Wno-unicode-homoglyph
 
+custom-setup
+  setup-depends:
+      base
+    , Cabal
+    , cabal-doctest
+
 Library
     Import: common
     Hs-Source-Dirs: src
@@ -460,13 +466,16 @@
         turtle                    >= 1.6      && < 1.7 ,
     Default-Language: Haskell2010
 
-Test-Suite doctest
+Test-Suite dhall-doctests
     Import: common
     Type: exitcode-stdio-1.0
     Hs-Source-Dirs: doctest
     Main-Is: Main.hs
     Build-Depends:
         mockery                 < 0.4 ,
+        base-compat                   ,
+        QuickCheck >= 2.14 && < 2.15  ,
+        special-values          < 0.2 ,
         doctest   >= 0.7.0
     if os(windows)
       -- https://github.com/dhall-lang/dhall-haskell/issues/2237
Only in b: dhall.cabal.orig
Only in b: dist-newstyle
diff -ur a/doctest/Main.hs b/doctest/Main.hs
--- a/doctest/Main.hs	2001-09-09 03:46:40.000000000 +0200
+++ b/doctest/Main.hs	2025-02-15 11:36:37.598707726 +0100
@@ -10,6 +10,7 @@
 import qualified System.IO
 import qualified Test.DocTest
 import qualified Test.Mockery.Directory
+import Build_doctests (pkgs)
 
 main :: IO ()
 main = do
@@ -18,24 +19,13 @@
     pwd <- System.Directory.getCurrentDirectory
     prefix <- System.Directory.makeAbsolute pwd
     let src = prefix </> "src"
-
-    System.Environment.setEnv "XDG_CACHE_HOME" (pwd </> ".cache")
-
-    Test.Mockery.Directory.inTempDirectory $ do
-        writeFile "makeBools.dhall" "λ(n : Bool) → [ n && True, n && False, n || True, n || False ]"
-        writeFile "bool1" "True"
-        writeFile "bool2" "False"
-        writeFile "both" "./bool1 && ./bool2"
-        writeFile "file2" "./file1"
-        writeFile "file1" "./file2"
-        writeFile "simon.dhall" $ unlines
-          [ "let Name = Text"
-          , "let Font = < Arial | `Comic Sans` | Helvetica | `Times New Roman` >"
-          , "let Person = { name : Name, favoriteFont : Font }"
-          , "in  { name = \"Simon\", favoriteFont = Font.`Comic Sans` } : Person"
-          ]
-
-        Test.DocTest.doctest $
+        customFlags =
+            [ "-package-env=-"
+            , "-hide-all-packages"
+            , "-no-user-package-db"
+            , "-package-db=" ++ prefix </> "dist/package.conf.inplace"
+            ]
+        origArgs =
             [ "-DWITH_HTTP"
             , "-DUSE_HTTP_CLIENT_TLS"
             , "--fast"
@@ -57,3 +47,23 @@
             , src </> "Dhall/Tags.hs"
             , src </> "Dhall/Tutorial.hs"
             ]
+
+    let args = customFlags ++ pkgs ++ origArgs
+
+    System.Environment.setEnv "XDG_CACHE_HOME" (pwd </> ".cache")
+
+    Test.Mockery.Directory.inTempDirectory $ do
+        writeFile "makeBools.dhall" "λ(n : Bool) → [ n && True, n && False, n || True, n || False ]"
+        writeFile "bool1" "True"
+        writeFile "bool2" "False"
+        writeFile "both" "./bool1 && ./bool2"
+        writeFile "file2" "./file1"
+        writeFile "file1" "./file2"
+        writeFile "simon.dhall" $ unlines
+          [ "let Name = Text"
+          , "let Font = < Arial | `Comic Sans` | Helvetica | `Times New Roman` >"
+          , "let Person = { name : Name, favoriteFont : Font }"
+          , "in  { name = \"Simon\", favoriteFont = Font.`Comic Sans` } : Person"
+          ]
+
+        Test.DocTest.doctest args
Only in b/doctest: Main.hs.orig
