问题
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.
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.
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.
Maybe also relevant:
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.
回答1:
Avoid the complications of trying to unite what has always been split. Why not just keep the pandoc-types source code in a separate (local) library project --which you can also modify and refer to from your main project-- and load it in a separate editor instance with its own context? You can switch between editors where applicable, when browsing through the source code.
回答2:
Depending on the tool one uses there are different ways to go about it.
If stack
is used:
Following the accepted answer in this question here allows me to compile the code via stack build
with pandoc-types
as a local dependency.
If cabal
is used:
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
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
... to this:
library
build-depends: pandoc-types
Now my code compiles with cabal build
.
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.
来源:https://stackoverflow.com/questions/64546722/how-to-integrate-dependency-into-existing-project-in-haskell