You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode Haskell扩展无法切换GHC版本,HLS无自动补全求助

VSCode Haskell扩展HLS报错:无textDocument/didOpen处理器,无法提供自动补全

问题详情

通过GHCup管理Haskell工具链,指定使用GHC 9.4.8,但VSCode中的Haskell Language Server(HLS)持续报错LSP: no handler for: "textDocument/didOpen",且无法提供自动补全、代码提示等核心功能。

工作区配置

{
  "haskell.manageHLS": "GHCup",
  "haskell.serverEnvironment": {
    "PATH": "${HOME}/.ghcup/bin:$PATH"
  },
  "haskell.toolchain": {
    "ghc": "9.4.8",
    "hls": "2.5.0.0",
    "cabal": "recommended"
  }
}

扩展输出日志

2024-01-18T03:15:08.330017Z | Error | LSP: no handler for: "textDocument/didOpen"
2024-01-18T03:15:08.330316Z | Debug | LSP: set new config: {
  "checkProject": true,
  "formattingProvider": "brittany",
  "ghcupExecutablePath": "",
  "indentationRules": { "enabled": true },
  "logFile": "",
  "manageHLS": "GHCup",
  "maxCompletions": 40,
  "metadataURL": "",
  "openDocumentationInHackage": true,
  "openSourceInHackage": true,
  "plugin": {
    "alternateNumberFormat": { "globalOn": true },
    "cabal": {
      "codeActionsOn": true,
      "completionOn": true,
      "diagnosticsOn": true
    },
    "callHierarchy": { "globalOn": true },
    "changeTypeSignature": { "globalOn": true },
    "class": { "codeActionsOn": true, "codeLensOn": true },
    "eval": {
      "config": { "diff": true, "exception": false },
      "globalOn": true
    },
    "explicit-fields": { "globalOn": true },
    "explicit-fixity": { "globalOn": true },
    "fourmolu": { "config": { "external": false } },
    "gadt": { "globalOn": true },
    "ghcide-code-actions-bindings": { "globalOn": true },
    "ghcide-code-actions-fill-holes": { "globalOn": true },
    "ghcide-code-actions-imports-exports": { "globalOn": true },
    "ghcide-code-actions-type-signatures": { "globalOn": true },
    "ghcide-completions": {
      "config": { "autoExtendOn": true, "snippetsOn": true },
      "globalOn": true
    },
    "ghcide-hover-and-symbols": { "hoverOn": true, "symbolsOn": true },
    "ghcide-type-lenses": { "config": { "mode": "always" }, "globalOn": true },
    "hlint": {
      "codeActionsOn": true,
      "config": { "flags": [  ] },
      "diagnosticsOn": true
    },
    "importLens": { "codeActionsOn": true, "codeLensOn": true },
    "moduleName": { "globalOn": true },
    "ormolu": { "config": { "external": false } },
    "overloaded-record-dot": { "globalOn": true },
    "pragmas-completion": { "globalOn": true },
    "pragmas-disable": { "globalOn": true },
    "pragmas-suggest": { "globalOn": true },
    "qualifyImportedNames": { "globalOn": true },
    "rename": { "config": { "crossModule": false }, "globalOn": true },
    "retrie": { "globalOn": true },
    "semanticTokens": {
      "config": {
        "classMethodToken": "method",
        "classToken": "class",
        "dataConstructorToken": "enumMember",
        "functionToken": "function",
        "patternSynonymToken": "macro",
        "recordFieldToken": "property",
        "typeConstructorToken": "enum",
        "typeFamilyToken": "interface",
        "typeSynonymToken": "type",
        "typeVariableToken": "typeParameter",
        "variableToken": "variable"
      },
      "globalOn": false
    },
    "splice": { "globalOn": true },
    "stan": { "globalOn": false }
  },
  "promptBeforeDownloads": "true",
  "releasesDownloadStoragePath": "",
  "releasesURL": "",
  "serverEnvironment": { "PATH": "${HOME}/.ghcup/bin:$PATH" },
  "serverExecutablePath": "",
  "serverExtraArgs": "",
  "toolchain": { "cabal": "recommended", "ghc": "9.4.8", "hls": "2.5.0.0" },
  "trace": { "client": "debug", "server": "messages" },
  "upgradeGHCup": true
}

解决建议

  • 匹配HLS与GHC版本兼容性
    手动指定的HLS 2.5.0.0可能与GHC 9.4.8不兼容。用GHCup自动安装适配当前GHC版本的HLS:
    在终端执行:

    ghcup install hls --ghc 9.4.8
    

    之后修改VSCode工作区配置,将hls版本改为recommended,让GHCup自动匹配:

    "haskell.toolchain": {
      "ghc": "9.4.8",
      "hls": "recommended",
      "cabal": "recommended"
    }
    
  • 清理缓存并重启LSP

    1. 删除项目根目录下的.hie文件夹和.ghc.environment.*文件
    2. 在VSCode中按Ctrl+Shift+P(Mac是Cmd+Shift+P),选择Haskell: Restart Haskell LSP Server
  • 验证PATH配置有效性
    避免使用变量替换,直接写入绝对路径(替换为你的实际用户目录):

    "haskell.serverEnvironment": {
      "PATH": "/home/your-username/.ghcup/bin:$PATH"
    }
    

    同时在终端执行echo $PATH确认.ghcup/bin已在系统PATH中。

  • 初始化项目环境
    如果是Cabal项目,在终端执行cabal build生成必要的编译环境文件,确保HLS能正确识别项目结构。

内容的提问来源于stack exchange,提问作者whyuenacccc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:24:53