Generating documentation for my own code with Haddock and stack

别来无恙 提交于 2020-12-29 02:36:39

问题


I have annotated my code in Haddock style and would like to generate browse-able documentation. Since I am also using stack, I want to integrate the documentation generation into the workflow. However, I have not yet been able to generate anything useful.

I can run

stack haddock

and it will generate documentation in the style I want (to be found deep inside ~/.stack/), but it only seems to generate documentation for the packages I depend on, rather than for my own code.

When I run

stack haddock --help

I get the impression that I can use the additional argument --haddock to generate documentation for my own project, and --no-haddock-deps to leave out the documentation for my dependencies. However, when I run

stack haddock --haddock --no-haddock-deps

nothing seems to happen. If I stack clean first it will recompile all my code but no output is generated seeming to relate in any way to documentation.

As an intermediate solution I have also tried running Haddock by itself, i.e.

haddock my-source.hs

but then I get an error that it cannot find a module the file depends on (which is installed locally by stack). This gives me the impression that documentation generation will have to go through stack somehow. I have looked for, but not really found any explanations related to configuring my .cabal and stack.yaml files for documentation.

TL;DR

How can I use stack and Haddock to generate documentation for the code in my own package?


回答1:


According to this ticket on the stack issue tracker, Stack can currently only build documentation for libraries, but not executables.

Cabal can be configured to work with the stack databases with this command:

cabal configure --package-db=clear --package-db=global --package-db=$(stack path --snapshot-pkg-db) --package-db=$(stack path --local-pkg-db)

after which you can run cabal haddock --executables to generate the documentation.

By the way, stack haddock is only a shortcut for stack build --haddock, so there is no need to write stack haddock --haddock.




回答2:


https://www.reddit.com/r/haskell/comments/5ugm9s/how_to_generate_haddock_docs_for_nonlibrary_code/ddtwqzc/

The following solution only works when individual files are specified:

stack exec -- haddock --html src/Example.hs src/Main.hs --hyperlinked-source --odir=dist/docs


来源:https://stackoverflow.com/questions/32712775/generating-documentation-for-my-own-code-with-haddock-and-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!