doctests pulls in the out-of-scope packages if they are installed on the
system. The best workaround is to use cabal-doctest.
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,2 +1,20 @@
+{-# 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
--- a/doctests.hs
+++ b/doctests.hs
@@ -1,5 +1,16 @@
 module Main where
+import System.IO
+import System.Directory
+import System.FilePath
 import Test.DocTest
+import Build_doctests (pkgs)
 
 main :: IO ()
-main = doctest $ words "--preserve-it src/"
+main = do
+  pwd <- System.Directory.getCurrentDirectory
+  prefix <- System.Directory.makeAbsolute pwd
+  let src = prefix </> "src"
+      customFlags = [ "-package-env=-", "-hide-all-packages", "-no-user-package-db", "-package-db=" ++ prefix </> "dist/package.conf.inplace"]
+      origArgs = ["--preserve-it"]
+      args = customFlags ++ pkgs ++ origArgs
+  doctest args
--- a/tree-traversals.cabal
+++ b/tree-traversals.cabal
@@ -36,7 +36,7 @@
 author:              Noah Luck Easterly
 maintainer:          noah.easterly@gmail.com
 category:            Data
-build-type:          Simple
+build-type:          Custom
 extra-source-files:  ChangeLog.md
                   ,  README.md
 
@@ -54,6 +54,12 @@
   default: False
   manual: True
 
+custom-setup
+  setup-depends:
+      base
+    , Cabal
+    , cabal-doctest
+
 library
   exposed-modules: Control.Applicative.Phases
                  , Data.BinaryTree
@@ -71,6 +77,11 @@
   type: exitcode-stdio-1.0
   main-is: doctests.hs
   build-depends: base >=4.10
+               , base-compat
+               , cabal-doctest
+               , directory
+               , filepath
+               , QuickCheck >= 2.14 && < 2.15
                , doctest >=0.13
                , containers
                , mtl >=2.2.1
