如何将依赖项集成到Haskell中的现有项目中?

时间:2020-10-27 00:21:44

标签: haskell ghc cabal haskell-stack

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

因此,我尝试的一部分工作是下载pandoc-types,将.hs文件移动到pandoc中的相应目录中,将模块添加到.cabal文件中,同时删除来自pandoc-<version>文件和.cabal的{​​{1}}依赖性。但是我在构建时遇到了兼容性错误:

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. GHCstack甚至Haskell自己都不是很熟悉。还是有另一种方法来使类型信息悬停文件工作? 特别是作为Haskell初学者,我确实需要此功能来正确掌握代码库。

也许也相关:

两个回购似乎都在构建过程中生成cabal文件。据我了解,它们也需要复制到here中的Paths_*.hs目录中。

2 个答案:

答案 0 :(得分:0)

避免试图团结始终分裂的复杂性。为什么不只将pandoc类型的源代码保存在一个单独的(本地)库项目中(您也可以从主项目中进行修改和引用)并将其加载到具有其自身上下文的单独的编辑器实例中?浏览源代码时,可以在适当的情况下在编辑器之间切换。

答案 1 :(得分:0)

根据所使用的工具,有不同的处理方法。

如果使用stack

在此问题here之后,我可以通过stack buildpandoc-types作为本地依赖项来编译代码。

如果使用cabal

与上述解决方案一样,需要将本地依赖项添加到存储库的根文件夹中。此外,应在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文件中ghc-options中的pandoc.cabal标志所抑制(假设这是扩展名读取的内容,以便打印警告/错误)。因此,我不知道是什么导致这些错误。在构建过程中从Hackage下载回购库时,它们不存在。关于这个问题,我可能需要在堆栈溢出问题上再问一个问题。

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