Skip to content

Can't access __HADDOCK_VERSION__ #10818

Open

Description

@LeventErkok

Following the discussion in #9177

@FinleyMcIlwaine I want to have access to __HADDOCK_VERSION__; alas, using the mechanism described in #9177 doesn't seem to work.

To be specific, I created a project.cabal file, with contents:

packages: .

package *
  documentation: true
  haddock-version-cpp: True

but this doesn't seem to make __HADDOCK_VERSION__ defined as a macro in source files. Any advice?

Activity

  1. FinleyMcIlwaine commented on Mar 8, 2025

    @FinleyMcIlwaine
    Contributor

    Hi @LeventErkok. The discussion in that PR is long and it looks like my original PR description became stale as the patch progressed. Based on this comment by @alt-romes it looks like the haddock-version-cpp option was never introduced and the decision was made to remove the __HADDOCK_VERSION__ macro altogether. Apologies for the inconvenience. What is your use-case for the macro?

  2. LeventErkok commented on Mar 8, 2025

    @LeventErkok
    Author

    For my particular case, I'd like to hide doc-test setup segments from generated Haddock output. Without having a way to tell whether we're running under haddock or not, this is impossible to do.

    Based on clash-lang/clash-compiler#2902, I think Clash has similar needs. @DigitalBrains1 might want to comment further.

    At the end of the day, it seems like it'd be useful to have a mechanism for users to determine if haddock is the starting point. I understand the motivation for avoiding rebuilds, but if the user chooses to do so, there should be a way; to maintain old behavior if nothing else.

  3. mpickering commented on Mar 11, 2025

    @mpickering
    Collaborator

    I think the proper mechanism to fix this would be to make "haddock" into a way (the same as static, dynamic etc)

  4. alt-romes commented on Mar 14, 2025

    @alt-romes
    Collaborator

    @LeventErkok hi, it's unfortunate that this has broken your workflow. Let's try to work out a fix.

    I would appreciate it if you could post an example of what exactly you are trying to achieve with doc-test so that I can understand your use case better.

    My understanding is that re-introducing HADDOCK_VERSION is a step backwards, even though I wouldn't be opposed to re-introducing it temporarily to ease migration.

    In particular, perhaps the right question is "why does doc-test force the doc-tests to be part of the haddock comments?". It's maybe a limitation of the tool to require doc-tests to be in the haddocks rather than attached in a different way to a function -- it makes sense to want doctests either on or off haddocks.

    Or, perhaps, it is more that you want to selectively disable parts of a haddock comment; but that begs the question why did you write it as part of a haddock in the first place.

    Thank you for bringing this up.

  5. DigitalBrains1 commented on Mar 14, 2025

    @DigitalBrains1

    I will also explain what it is we need (although maybe it's better to do that somewhere else?), but I did not get around to it yet.

    If the intent is simply to tell "we are generating Haddock" from "we are compiling a package", maybe the following workaround is okay?

    flag for-haddock
      description:
        Define this flag during Haddock generation
      default: False
      manual: True
    
    library
      if flag(for-haddock)
        CPP-Options: -DFOR_HADDOCK
    

    and then if you invoke it as

    $ cabal haddock -ffor-haddock
    

    the macro will be defined so you can tune your Haskell source files to do the right thing for Haddock, whereas during normal compilation you would of course not set this flag and get the behaviour you want for normal code generation.

    Of course, if your package is on Stackage, the documentation on the Stackage webpage will not generate the correct version, and you'll also need to manually upload documentation to Hackage to get it right there.

  6. LeventErkok commented on Mar 14, 2025

    @LeventErkok
    Author

    @DigitalBrains1 Your workaround works like a charm and handles my use case! In fact, perhaps this should be the recommended method as the user is explicitly opting in to the recompilation cost.

    @alt-romes I'm happy to close this ticket; perhaps except to mention @DigitalBrains1's workaround as the recommended solution in some cabal documentation?

  7. DigitalBrains1 commented on Mar 25, 2025

    @DigitalBrains1

    I've just created issue #10852 where I list why we need control over the Haddock generation, so the Cabal devs know what weird hoops users actually make their tool jump through ;-).

  8. lyokha commented on Feb 19, 2026

    @lyokha
    Collaborator

    Sadly, the approach with a Cabal flag has problems:

    1. You must always remember this flag to build docs,
    2. you must upload documentation on Hackage manually (I didn't find a way to use custom flags for building docs on Hackage),
    3. (most annoying) a user who builds a code that depends on such a package with --enable-documentation must know this flag as well to simply not let the build fail (I use --optghc=-DNGX_CSTUB to trick Haddock not to link to undefined symbols, and thus this is my case).

    These issues can be resolved by using Cabal's Custom build. For this, put file Setup.hs in the root directory of the project.

    Setup.hs

    import Distribution.Simple
    import Distribution.Simple.Setup
    
    import Data.List
    
    userHooks :: UserHooks
    userHooks =
        simpleUserHooks { haddockHook = \desc lbi hooks flags -> do
                            let (oghc, ocstub) = ("ghc", "-DNGX_CSTUB")
                                hadflag = haddockProgramArgs flags
                                (h, t) = break ((oghc ==) . fst) hadflag
                                hadflag' =
                                    case uncons t of
                                        Nothing ->
                                            (oghc, [ocstub]) : hadflag
                                        Just ((_, args), t') ->
                                            h ++ (oghc, ocstub : args) : t'
                                flags' = flags { haddockProgramArgs = hadflag' }
                            haddockHook simpleUserHooks desc lbi hooks flags'
                        }
    
    main :: IO ()
    main = defaultMainWithHooks userHooks

    Here, -DNGX_CSTUB plays the same role as -DFOR_HADDOCK.

    Cabal file (a fragment)

    build-type:            Custom
    
    custom-setup
      setup-depends:       base >= 4.8 && < 5
                         , Cabal >= 3.0.0.0 && < 4

    Note though, that the Custom build approach is being currently replaced by Cabal's Setup hooks. Unfortunately, I didn't find a way to customize GHC options for Haddock like this with Setup hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions