如何将依赖项集成到Haskell中的现有项目中? [英] How to integrate dependency into existing project in Haskell?
问题描述
我目前正在尝试开始使用Haskell,因为我想使用 Pandoc 的部分代码库用于其他项目.由于我是Haskell的新手,因此我需要适当的IDE功能,例如代码完成和跳转到定义 AND 类型信息和悬停文档.我选择了带有 Haskell扩展名的VSCode.现在出现了我的问题:Pandoc依赖于 pandoc-types ,它是代码的组成部分,我需要了解和修改.但是使用 ghc-option
"$ everything":-haddock
(根据
I am currently trying to get started with Haskell because I want to use parts of the code base of Pandoc for a different project. Since I am new to Haskell I need proper IDE features like code completion and jump to definition AND type information and documentation on hover. I chose VSCode with the Haskell extension for the job.
Now comes my problem: Pandoc depends on pandoc-types which is an integral part of the code, which I need to understand and modify. But using the ghc-option
"$everything": -haddock
(which should be the right way to achieve my goal according to this) does not seem to give me proper type information and documentation on hover. Since I copied the entire repo and do not intend to pull or push from the original repos I would like to add the code from pandoc-types
to the existing Haskell code in the main pandoc
repo.
因此,我尝试的一部分工作是下载 pandoc-types
,将 .hs
文件移动到 pandoc
中的相应目录中,并添加模块删除 .cabal
文件,同时从 .cabal
文件和 stack.yaml中删除
.但是我在构建时遇到了兼容性错误: pandoc-< version>
依赖性
So part of what I have tried was downloading pandoc-types
moving the .hs
files into the according dir in pandoc
, adding the modules to the .cabal
file while removing the pandoc-<version>
dependency from the .cabal
file and the stack.yaml
. But all I got where compatibility errors when building:
➜ 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.
如何将依赖项的存储库更改为代码库的一部分.我尝试了几种不同的方法,但似乎没有任何效果.我对 GHC
, stack
和 cabal
甚至Haskell本身并不十分熟悉.还是有另一种方法可以使类型信息和悬停文件工作?特别是作为Haskell初学者,我确实需要此功能来正确掌握代码库.
How can I change a repo from a dependency be part of the code base. I have tried a few different things, but nothing seemed to work out. I am not really familiar with GHC
, stack
and cabal
or even Haskell itself for that matter. Or is there another way to get the type information and documentation on hover working?
Especially as a Haskell beginner I really need this functionality to properly grasp the code base.
也许也相关:
两个回购似乎都在构建过程中生成 Paths _ *.hs
文件.据我了解,它们还需要像提到的
Both repos seem to generate Paths_*.hs
files in the building process. As far as I understand it, they also need to be copied into the src/
dir like mentioned here.
推荐答案
根据所使用的工具,有不同的处理方法.
Depending on the tool one uses there are different ways to go about it.
如果使用了 stack
:
If stack
is used:
按照此问题接受的答案在这里允许我通过 stack build
将 pandoc-types
作为本地依赖项来编译代码.
Following the accepted answer in this question here allows me to compile the code via stack build
with pandoc-types
as a local dependency.
如果使用 cabal
:
If cabal
is used:
与上述解决方案一样,需要将本地依赖项添加到存储库的根文件夹中.此外,应将对依赖项 cabal
文件的引用添加到 packages:
部分中的 cabal.project
文件中,如下所示(这告诉cabal:还要编译此文件夹的内容):
As with the solution above one needs to add the local dependency into the root folder of the repo. Furthermore one should add a reference to the dependencies cabal
file to the cabal.project
file in the packages:
section as follows (which tells cabal to also compile the contents of this folder):
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
<项目名称> .cabal
中的依赖项也需要删除版本限制.因此,文件已更改为:
Also the dependencies in the <projectname>.cabal
need the version restrictions removed. So the file is changed from this:
library
build-depends: pandoc-types >= 1.22 && < 1.23
...对此:
library
build-depends: pandoc-types
现在,我的代码使用 cabal build
进行编译.
Now my code compiles with cabal build
.
但是,我的问题仍然存在.当遵循这两种方法时,VSCode中的Haskell扩展仍无法正确自动完成.使用 stack
方法会给出警告,例如 do-notation语句丢弃了类型为...
的结果,并且出现了诸如 Cannot推断出...之类的错误,这些错误是由使用引起的的...
.实际上,第一个警告应该已经用 pandoc.cabal
ghc-options 中的 -fno-warn-unused-do-bind
标志取消了>文件(假设这是扩展名读取的内容,以打印警告/错误).因此,我不知道是什么原因导致了这些错误.在构建过程中从Hackage下载回购库时,它们不存在.关于这个问题,我可能需要在堆栈溢出问题上再问一个问题.
However one part of my problem remains. When following both approaches the Haskell extension in VSCode still does not properly autocomplete. Using the stack
approach gives warnings like A do-notation statement discarded a result of type ...
and errors like Could not deduce ... arising from a use of ...
. The first warning should actually already be suppressed with the -fno-warn-unused-do-bind
flag in ghc-options
within the pandoc.cabal
file (assuming this is what the extension reads in order to print warnings/errors). So I don't know what is responsible for these errors. They are not present when the repo was downloaded from Hackage during the build process. I will probably need to ask another question on stack overflow regarding this issue.
无论如何,由于标题中的问题得到了回答,我希望这会在将来的某个时候对某人有所帮助.
Anyway since the question in headline is answered, I hope this helps someone at some point in the future.
这篇关于如何将依赖项集成到Haskell中的现有项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!