【问题标题】:How to integrate dependency into existing project in Haskell?如何将依赖项集成到 Haskell 中的现有项目中?
【发布时间】:2020-10-27 00:21:44
【问题描述】:

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

所以我尝试的一部分是下载pandoc-types 将.hs 文件移动到pandoc 中的相应目录中,将模块添加到.cabal 文件中,同时从@987654335 中删除pandoc-<version> 依赖项@ 文件和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 更改为代码库的一部分。我尝试了一些不同的方法,但似乎没有任何效果。我对GHC、stack 和cabal 甚至Haskell 本身都不熟悉。或者还有其他方法可以让类型信息和悬停时的文档正常工作吗? 特别是作为一个 Haskell 初学者,我真的需要这个功能来正确掌握代码库。

也许也相关:

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

【问题讨论】:

    标签: haskell ghc cabal haskell-stack


    【解决方案1】:

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

    如果使用stack:

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

    如果使用cabal:

    与上面的解决方案一样,需要将本地依赖项添加到 repo 的根文件夹中。此外,还应该在packages: 部分的cabal.project 文件中添加对依赖项cabal 文件的引用,如下所示(告诉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
    

    &lt;projectname&gt;.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 ... 之类的错误。第一个警告实际上应该已经在pandoc.cabal 文件中的ghc-options 中使用-fno-warn-unused-do-bind 标志被抑制(假设这是扩展读取以打印警告/错误的内容)。所以我不知道是什么导致了这些错误。在构建过程中从 Hackage 下载 repo 时,它们不存在。关于这个问题,我可能需要就堆栈溢出提出另一个问题。

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

    【讨论】:

      【解决方案2】:

      避免试图团结一直分裂的东西所带来的麻烦。为什么不将 pandoc-types 源代码保存在单独的(本地)库项目中——您也可以从主项目中修改和引用它——并将其加载到具有自己上下文的单独编辑器实例中?浏览源代码时,您可以在适用的编辑器之间切换。

      【讨论】:

      • 感谢您的回答。这不是一个真正的选择,因为我还需要编辑这个(半)外部回购中的代码。这意味着我需要设置一个新的公共仓库来托管代码的副本。然后我需要将此 repo 副本添加为依赖项。它不会使整个设置变得更容易。
      猜你喜欢
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多