haskell / haddock

Haskell Documentation Tool
www.haskell.org/haddock/
BSD 2-Clause "Simplified" License
361 stars 241 forks source link

included files via CPP break haddock source links #1440

Open hasufell opened 2 years ago

hasufell commented 2 years ago

Prominent example is filepath: https://hackage.haskell.org/package/filepath-1.4.2.1/docs/src/System.FilePath.Posix.html#pathSeparator

Also reported here: https://github.com/haskell/filepath/issues/81

hasufell commented 2 years ago

The only workaround I found is running cpphs manually to expand the files during development: https://github.com/haskell/filepath/pull/95

hasufell commented 2 years ago

@gbaz found out in #hackage that another workaround is this:

  1. rename System/FilePath/Posix.hs to System.FilePath/Posix.cpphs and delete CPP pragma
  2. run cabal haddock

This however, will add some junk to the beginning of the file, such as:

{-# LINE 1 "System/FilePath/Posix.cpphs" #-}
# 0 "System/FilePath/Posix.cpphs"
# 0 "<built-in>"
# 0 "<command-line>"
# 12 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4

# 17 "/usr/include/stdc-predef.h" 3 4

... lots of newlines before the actual code...

This seems to break the link destinations... you'll always end up at the top of the source file.

hasufell commented 2 years ago

Forcing cabal to use cpphs doesn't seem possible: https://github.com/haskell/cabal/issues/4278

hasufell commented 2 years ago

This seems to (somewhat) work:

flag cpphs
  Description: Use cpphs (fixes haddock source links)
  Default:     True
  Manual:      False

library
  ...

  if flag(cpphs)
    ghc-options:        -pgmPcpphs -optP--cpp
    build-tool-depends: cpphs:cpphs