evantianx / Bloooooooog

Place to record what I thought and learned
0 stars 0 forks source link

Haskell 之 VSCode 环境搭建 #93

Open evantianx opened 6 years ago

evantianx commented 6 years ago

安装 Haskell Tool Stack

Haskell Tool Stack

brew install haskell-stack

这样就可以开始用 stack 来构建项目:

stack new hs-demo
cd hs-demo
stack setup

The stack setup will download the compiler if necessary in an isolated location (default ~/.stack) that won't interfere with any system-level installations. (For information on installation paths, please use the stack path command.).

更换 stack 镜像源为清华镜像

hackage 镜像使用帮助

修改 ~/.stack/config.yaml, 加上:

package-indices:
- name: Tsinghua
  download-prefix: http://mirrors.tuna.tsinghua.edu.cn/hackage/package/
  http: http://mirrors.tuna.tsinghua.edu.cn/hackage/00-index.tar.gz

安装 VSCode 相关插件

语法高亮

Haskell Syntax Highlighting

Linting 插件

haskell-linter

安装完之后会提示错误:

因为我们还没有安装 hlint:

stack install hlint

然后在 VSCode 中配置 hlint 路径:

{
  "haskell.hlint.executablePath": "/Users/administrator/.local/bin/hlint"
}

Haskell ghc-mod

赋予我们类型信息,函数定义,跳转到定义等完善的开发体验

Haskell ghc-mod

同样需要安装 ghc-mod

stack install ghc-mod

这里可能会报错:

 ~ stack install ghc-mod

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

In the dependencies for ghc-mod-5.8.0.0:
    Cabal-2.0.1.1 from stack configuration does not match >=1.18 && <1.25
                  (latest matching version is 1.24.2.0)
    base-4.10.1.0 from stack configuration does not match >=4.6.0.1 && <4.10
                  (latest matching version is 4.9.1.0)
    cabal-helper must match <0.8 && >=0.7.3.0, but the stack configuration has
                 no specified version (latest matching version is 0.7.3.0)
    extra-1.6.2 from stack configuration does not match <1.6 && >=1.4 (latest
                matching version is 1.5.3)
    ghc-8.2.2 from stack configuration does not match >=7.6 && <8.2
    monad-journal-0.8.1 from stack configuration does not match <0.8 && >=0.4
                        (latest matching version is 0.7.2)
    optparse-applicative-0.14.0.0 from stack configuration does not
                                  match >=0.13.0.0 && <0.14 (latest matching
                                  version is 0.13.2.0)
needed since ghc-mod is a build target.

Some potential ways to resolve this:

  * Recommended action: try adding the following to your extra-deps
    in /home/user/.stack/global-project/stack.yaml:

- Cabal-1.24.2.0
- base-4.9.1.0
- cabal-helper-0.7.3.0
- extra-1.5.3
- monad-journal-0.7.2
- optparse-applicative-0.13.2.0

  * Set 'allow-newer: true' to ignore all version constraints and build anyway.

  * You may also want to try using the 'stack solver' command.

Plan construction failed.

原因在于 ghc-mod 不支持当前的 GHC 版本,所以需要降级 GHC 然后再安装。

~/.stack/global-project/stack.yaml 中修改字段:

resolver: lts-9.21

之后同样需要在 VSCode 中修改路径,方法如上,不再赘述。