haskell/cabal

Cabal-3: doctest-discover fails for non-boot libraries

erikd opened this issue · 6 comments

erikd commented

I have a simple cabal file:

name: doctest-depends
version: 0.1
build-type: Simple
cabal-version: >= 1.10

library
  default-language: Haskell2010
  exposed-modules: P
  hs-source-dirs: src
  build-depends: base, base-orphans

test-suite doctest
  main-is: doctest.hs
  type: exitcode-stdio-1.0
  default-language: Haskell2010
  build-depends: base, doctest, base-orphans
  build-tool-depends: doctest-discover:doctest-discover

where the module P.hs defines a doctest

module P where
import Base.Orphans ()
-- >>> square 5
-- 25
-- >>> square 4
-- 16
square ::Int -> Int
square x = x * x

and where doctest.hs consists of:

{-# OPTIONS_GHC -F -pgmF doctest-discover #-}

When running cabal configure --enable-tests && cabal test (using cabal-install v3 from git about a week ago) I get:

Test suite doctest: RUNNING...

cabal-testsuite/PackageTests/Doctest/Depends/src/P.hs:6:1: error:
    Could not find module ‘Base.Orphans’
    Use -v to see a list of the files searched for.
  |
2 | import Base.Orphans ()
  | ^^^^^^^^^^^^^^^^^^^^^^

The above project builds and passes the tests when run with stack.

This is not a problem with doctest-discover because if I remove the base-orphans dependency and remove the Base.Orphans import, the test builds and passes as expected.

It therefore seems that the doctest-discover pre-processor is being executed without access to the package DB.

I would like to get some advice on how to fix this. I am currently looking at Distribution.Simple.Test.ExeV10.runTest and looking to pass the package DB location to the test program there.

erikd commented

This is actually a regression.
Using doctest-discover (cloned from github):

cabal sandbox init
cabal build
cabal install --dependencies-only
cabal test

the tests build and run (with a test failure, but that is irrelevant).

Now with cabal 3.0 (from 2019/06/03):

cabal configure --enable-tests
cabal build
cabal test

results in:

Build profile: -w ghc-8.6.5 -O1
In order, the following will be built (use -v for more details):
 - doctest-discover-0.2.0.0 (test:doctests) (ephemeral targets)
Preprocessing test suite 'doctests' for doctest-discover-0.2.0.0..
Building test suite 'doctests' for doctest-discover-0.2.0.0..
Running 1 test suites...
Test suite doctests: RUNNING...

/home/erikd/Git/Haskell/doctest-discover/src/Config.hs:5:1: error:
    Could not find module ‘Data.Aeson’
    Perhaps you meant Data.Version (from base-4.12.0.0)
    Use -v to see a list of the files searched for.
  |
5 | import Data.Aeson ((.:), (.:?), decode, FromJSON(..), Value(..))
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Test suite doctests: FAIL
Test suite logged to:
/home/erikd/Git/Haskell/doctest-discover/dist-newstyle/build/x86_64-linux/ghc-8.6.5/doctest-discover-0.2.0.0/t/doctests/test/doctest-discover-0.2.0.0-doctests.log
0 of 1 test suites (0 of 1 test cases) passed.
cabal: Tests failed for test:doctests from doctest-discover-0.2.0.0.

Note that cabal test in 3.0 is the equivalent of cabal v2-test in 2.4. Did cabal v2-test work before? And, conversely, does cabal v1-test work in 3.0?

erikd commented

Also fails on cabal v2-test with cabal 2.4.1.0.

With cabal 3.0, the following works:

cabal v1-sandbox init
cabal v1-install --dependencies-only
cabal v1-configure --enable-tests
cabal v1-test

Maybe this is related.
#4542

So the following command may work.

cabal configure --enable-tests
cabal build --write-ghc-environment-files=always
cabal test --write-ghc-environment-files=always

In my environment, the error is fixed.
hasktorch/ffi-experimental#62

erikd commented

Hmm, -write-ghc-environment-files=always (which i actually added the cabal.project file) fixed this.

gbaz commented

closing on that basis.