首页 > 解决方案 > 如何将依赖项集成到 Haskell 中的现有项目中?

问题描述

我目前正在尝试开始使用 Haskell,因为我想将Pandoc的部分代码库用于不同的项目。由于我是 Haskell 的新手,我需要适当的 IDE 功能,例如代码完成跳转到定义 类型信息以及hover 上的文档我选择了带有Haskell 扩展的VSCode来完成这项工作。现在我的问题来了:Pandoc 依赖于pandoc-types,它是代码的一个组成部分,我需要理解和修改它。但是使用ghc-option "$everything": -haddock(根据this应该是实现我的目标的正确方法)似乎没有给我正确的类型信息悬停文档。由于我复制了整个仓库并且不打算从原始仓库中提取或推送,我想将代码添加pandoc-types到主仓库中现有的 Haskell 代码中pandoc

所以我尝试的一部分是下载pandoc-types.hs文件移动到相应的目录中pandoc,将模块添加到.cabal文件中,同时pandoc-<version>.cabal文件和stack.yaml. 但是我在构建时遇到的所有兼容性错误:

➜  pandoc git:(master) ✗ stack build

Error: While constructing the build plan, the following exceptions were encountered:

In the dependencies for citeproc-0.1.0.1:
    pandoc-types-1.17.6 from stack configuration does not match >=1.22 && <1.23  (latest matching version is 1.22)
needed due to pandoc-2.11.0.1 -> citeproc-0.1.0.1

In the dependencies for commonmark-pandoc-0.2.0.1:
    pandoc-types-1.17.6 from stack configuration does not match >=1.21 && <1.23  (latest matching version is 1.22)
needed due to pandoc-2.11.0.1 -> commonmark-pandoc-0.2.0.1

In the dependencies for texmath-0.12.0.3:
    pandoc-types-1.17.6 from stack configuration does not match >=1.20 && <1.23  (latest matching version is 1.22)
needed due to pandoc-2.11.0.1 -> texmath-0.12.0.3

Some different approaches to resolving this:

  * Set 'allow-newer: true' in /Users/johannes/.stack/config.yaml to ignore all version constraints and build anyway.

  * Recommended action: try adding the following to your extra-deps in /Users/johannes/Programmieren/GITGOV/Pandocs/pandoc/stack.yaml:

- pandoc-types-1.22@sha256:15512ce011555ee720820f11cac0598317293406da5812337cbb1550d144e3bd,4071

Plan construction failed.

如何将依赖项中的 repo 更改为代码库的一部分。我尝试了一些不同的方法,但似乎没有任何效果。我不太熟悉GHCstack甚至cabal是 Haskell 本身。还是有另一种方法来获取悬停工作的类型信息和文档?特别是作为一个 Haskell 初学者,我真的需要这个功能来正确掌握代码库。

也许也相关:

两个 repos 似乎都Paths_*.hs在构建过程中生成文件。据我了解,它们还需要复制到这里src/提到的目录中。

标签: haskellghccabalhaskell-stack

解决方案


根据使用的工具,有不同的方法来解决它。

如果stack使用:

按照这个问题中接受的答案,stack build可以通过withpandoc-types作为本地依赖项来编译代码。

如果cabal使用:

与上面的解决方案一样,需要将本地依赖项添加到 repo 的根文件夹中。此外,应该将依赖cabal文件的引用添加到cabal.project该部分中的文件中packages:,如下所示(这告诉 cabal 也编译该文件夹的内容):

packages: pandoc-types/pandoc-types.cabal pandoc.cabal

package pandoc
  flags: +embed_data_files -trypandoc
  ghc-options: -j +RTS -A64m -RTS

source-repository-package
    type: git
    location: https://github.com/jgm/citeproc
    tag: 0.1.0.1

<projectname>.cabal还需要删除版本限制中的依赖项。所以文件从这个改变:

library
  build-depends: pandoc-types          >= 1.22     && < 1.23

...对此:

library
  build-depends: pandoc-types

现在我的代码用cabal build.

然而,我的问题的一部分仍然存在。当遵循这两种方法时,VSCode 中的 Haskell 扩展仍然不能正确地自动完成。使用该stack方法会给出类似的警告和类似的A do-notation statement discarded a result of type ...错误Could not deduce ... arising from a use of ...。第一个警告实际上应该已经被文件中的标志所抑制(-fno-warn-unused-do-bind假设这是扩展读取的内容以打印警告/错误)。所以我不知道是什么导致了这些错误。在构建过程中从 Hackage 下载 repo 时,它们不存在。关于这个问题,我可能需要就堆栈溢出提出另一个问题。ghc-optionspandoc.cabal

无论如何,既然标题中的问题得到了回答,我希望这在将来的某个时候对某人有所帮助。


推荐阅读