文章目录
- 需求
- 前置条件
- 总流程概述
- 必要说明
- 安装 GHCup
- 安装工具链
- 工具介绍
- 安装 cabal
- 更新 stack(可选)
- 安装 hsl
- 方案1:手动下载并安装
- 方案2:自动下载并安装
- 更新 ghc(可选)
- 安装第三方库
- 安装 Hlint
- 安装 haskell-debug-adapter
- 安装并配置 VS Code 扩展
- 新建并测试项目
- 后记
- 缘由
- 为什么不采用 WSL2 + VS Code Remote 方案?
- 最后
需求
- 不使用 WSL2
- 使用 VS Code 开发
- 颜色高亮
- 自动补全
- 语法检查
- F5调试
前置条件
- 系统:Windows10 / 11
Windows Server 不确定是否可行
- 熟悉 PowerShell 基本操作,包括但不限于:切换路径、读写环境变量、看懂报错
最好使用新的 PowerShell 7 而不是自带的 PowerShell 5
- 有恰当的网络环境
- 迅雷或其他下载软件
- 所有安装路径必须 全英文、无空格、字数少
总流程概述
- 安装 GHCup
- 安装/更新工具链
- 使用 cabal 安装 haskell-debug-adapter 调试工具
- 使用 cabal 安装 hlint 语法检查工具
- 安装并配置 VS Code 扩展
- 新建并测试项目
必要说明
- 本文不提供任何安装包,仅提供出处
- 下文使用 pwsh 或 Shell 代指 PowerShell
- 若无特殊说明,pwsh 的工作目录应为
~
,即C:\Users\<你的用户名>
安装 GHCup
GHCup 官网:
https://www.haskell.org/ghcup/#
- 开启网络代理
- 启动网络代理软件
- 在 系统级 环境变量中追加两条(将“<端口号>”换为你的实际值)
HTTP_PROXY: http://127.0.0.1:<端口号>
HTTPS_PROXY: http://127.0.0.1:<端口号>
- 复制此命令到 pwsh 中执行:
Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -ArgumentList $true } catch { Write-Error $_ }
- 根据交互式安装提示进行安装路径等配置项的确认,大部分时候你只需要 回车 或者 Y + 回车。
- stack 和 cabal 我的建议是先在此时确认一并安装,我们可以稍后再重装
- 等待安装直到在弹出的 Msys2 命令窗口(也有可能不弹出来,而是就在你当前的 Shell 里执行)中出现 Cabal 的安装进度 卡死在
writing config
或类似字眼处。 - 直接/强制关闭 Shell
- 新开 pwsh,执行
ghcup --version
,结果应如图所示,否则确认:- 使用 Everything 或其他办法检查
ghcup.exe
文件是否存在于你电脑 - 如果存在,它所在的目录是否加入了 系统级 环境变量 PATH 中
- 使用 Everything 或其他办法检查
安装工具链
工具介绍
工具列表:
- ghc:编译器
- ghcup:工具的包管理器
- stack:第三方库包管理器
- cabal:第三方库包管理器
- hsl:提供 vs code 语法支持
上述工具必须全部正确安装!
安装 cabal
- 会用 GHCup 基本命令
-
查看安装的工具列表:
ghcup list
此命令可以查看哪些安装了,哪些在使用,哪些可以装但没装
I
代表Installed
S
代表Selected
X
代表Not Installed
-
安装工具:
ghcup install <工具名> <版本号>
-
卸载工具:
ghcup rm <工具名> <版本号>
-
选定要使用的版本:
ghcup set <工具名> <版本号>
特别注意安装和卸载要的是
Version
,而不是Tags
-
- 卸载任何安装的 cabal (因为刚才安装到一半卡住强退,所以损坏了)
- ***关闭网络代理!!***
开启网络代理会导致安装卡死
- 用 install 命令安装最新版 cabal
- 将
cabal.exe
所在目录加入环境变量 PATH 中 - 执行
cabal --version
效果如下
- 开启网络代理
更新 stack(可选)
- 卸载所有版本 stack
- 安装最新版本 stack
安装 hsl
方案1:手动下载并安装
- 使用下载软件下载安装包:
https://downloads.haskell.org/~ghcup/unofficial-bindists/haskell-language-server/2.7.0.0/haskell-language-server-2.7.0.0-x86_64-mingw64.zip
- 移动/复制安装包到
<ghcup安装目录>\cache\
下,如D:\program\ghcup\cache
- 启用 ghcup 的缓存检测:
ghcup config set cache true
- pwsh 执行
ghcup install hls 2.7.0.0
方案2:自动下载并安装
- pwsh 执行
ghcup install hls latest
更新 ghc(可选)
- 和 stack 类似
安装第三方库
安装 Hlint
cabal install hlint
如果出现问题可以尝试按照下方 haskell-debug-adapter 步骤安装
安装 haskell-debug-adapter
- 找到
msys2.exe
所在目录,将其加入环境变量 PATH - !!极其重要!! 找到
sh.exe
所在目录,在 pwsh 中执行$env:PATH += ";<目录路径>"
,如$env:PATH += ";D:\program\ghcup\msys64\usr\bin"
(设定临时环境变量) - 执行
cabal install ghci-dap haskell-debug-adapter
- 找到 exe 路径,如
D:\program\cabal\bin\haskell-debug-adapter.exe
,将其目录加入环境变量 PATH 中。
安装并配置 VS Code 扩展
- 安装如下扩展:
- 设置 hlint 可执行程序路径
新建并测试项目
完整流程参考:https://marketplace.visualstudio.com/items?itemName=phoityne.phoityne-vscode
建议使用 cabal 方案
创建完成后项目结构应该为:
其中 <项目名>.cabal
应为:(请替换其中部分内容为你自己的)
cabal-version: 3.0
-- The cabal-version field refers to the version of the .cabal specification,
-- and can be different from the cabal-install (the tool) version and the
-- Cabal (the library) version you are using. As such, the Cabal (the library)
-- version used must be equal or greater than the version stated in this field.
-- Starting from the specification version 2.2, the cabal-version field must be
-- the first thing in the cabal file.
-- Initial package description 'helloworld' generated by
-- 'cabal init'. For further documentation, see:
-- http://haskell.org/cabal/users-guide/
--
-- The name of the package.
name: <项目名称>
-- The package version.
-- See the Haskell package versioning policy (PVP) for standards
-- guiding when and how versions should be incremented.
-- https://pvp.haskell.org
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- The license under which the package is released.
license: MIT
-- The file containing the license text.
license-file: LICENSE
-- The package author(s).
author: <作者>
-- An email address to which users can send suggestions, bug reports, and patches.
maintainer: <作者邮箱>
-- A copyright notice.
-- copyright:
build-type: Simple
-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README.
extra-doc-files: CHANGELOG.md
-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:
common warnings
ghc-options: -Wall
executable helloworld
-- Import common warning flags.
import: warnings
-- .hs or .lhs file containing the Main module.
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
-- Other library packages from which modules are imported.
build-depends: base ^>=4.19.1.0
-- Directories containing source files.
hs-source-dirs: app
-- Base language which the package is written in.
default-language: GHC2021
调试效果如图:
后记
缘由
这其实是笔者第二次尝试在 Windows 11 环境下搭建 Haskell 开发环境,上次尝试大约是2023年,因踩坑太多而愤然退坑。如今又想仔细学习函数式编程,不得不重拾旧事。
为什么不采用 WSL2 + VS Code Remote 方案?
如果你乐意,可以参考:
https://koki-nakamura22.github.io/blog/posts/haskell/setup-haskell-dev-env-on-wsl2/
本次搭建环境之处的确有此想法,并付诸行动。笔者已经安装好的 WSL2 环境有:
- ubuntu
- openeuler
在谷歌到上述教程后,尝试了在 ubuntu 上安装,结果不仅环境没装上,而且 apt
和 dkpg
都炸了。笔者对 linux 并不熟悉,对纯命令行界面甚感痛苦,于是本都打开的 openeuler 又关掉了。最终笔者决定采用 Windows 原生方案。
最后
从一路踩坑、血泪教训足可以体会到
- 小众语言真的不在乎 Windows
- 一些包管理工具居然真的没考虑过适配网络代理
此文转写依赖回忆,若有问题欢迎指出。
完稿于 2024/4/6 02:03