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

使用.devcontainer Docker镜像开发Haskell的工具路径问题求助

解决DevContainer中Haskell插件无法识别已安装工具的问题

我通过.devcontainer配置团队统一开发环境,但配置后Haskell插件提示工具路径存在问题。以下是我的Dockerfile内容:

FROM ubuntu:focal
## Inspiration from https://stackoverflow.com/questions/67680726/installing-haskells-cabal-or-ghcup-inside-a-dockerfile-wont-work
ENV TZ=Europe/Berlin
ARG USERNAME=vscode


ENV USERNAME=${USERNAME} \
    USER_UID=1000 \
    USER_GID=1000 \
    DEBIAN_FRONTEND=noninteractive \
    LANG=C.UTF8 \
    WDIR=/home/${USERNAME}


# install dependencies
RUN \
    apt-get update -y && \
    apt-get install -y --no-install-recommends \
    curl \
    libnuma-dev \
    zlib1g-dev \
    libgmp-dev \
    libgmp10 \
    git \
    wget \
    lsb-release \
    software-properties-common \
    gnupg2 \
    apt-transport-https \
    gcc \
    autoconf \
    automake \
    build-essential

# install gpg keys
ARG GPG_KEY=BA3CBA3FFE22B574
RUN gpg --batch --keyserver keys.openpgp.org --recv-keys $GPG_KEY

# install ghcup
RUN \
    curl https://downloads.haskell.org/~ghcup/x86_64-linux-ghcup > /usr/bin/ghcup && \
    chmod +x /usr/bin/ghcup && \
    ghcup config set gpg-setting GPGLax


ARG GHC=8.10.7
ARG CABAL=3.10.1.0
ARG HLS=2.0.0.1
ARG STACK=2.11.1

# install GHC and cabal

RUN ghcup -v install ghc --isolate /usr/local --force ${GHC}
RUN ghcup -v install cabal --isolate /usr/local/bin --force ${CABAL}
RUN ghcup -v install stack --isolate /usr/local/bin --force ${STACK}
RUN ghcup -v install hls --isolate /usr/local/bin --force ${HLS}

RUN groupadd --gid $USER_GID $USERNAME && \
    useradd -ms /bin/bash -K MAIL_DIR=/dev/null --uid $USER_UID --gid $USER_GID -m $USERNAME && \
    mkdir -p /etc/sudoers.d && \
    echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME && \
    chmod 0440 /etc/sudoers.d/$USERNAME

USER ${USER_UID}:${USER_GID}

WORKDIR ${WDIR}

ENV DEBIAN_FRONTEND=dialog

ENTRYPOINT ["/bin/bash"]

Haskell插件报错信息如下:

2023-07-29 05:28:48.3350000 [client] ERROR Error executing 'ghcup --no-verbose whereis hls 2.0.0.1' with error code 30
2023-07-29 05:28:48.3350000 [client] ERROR stderr: [1m[ Error ][0m []8;;https://errors.haskell.org/messages/GHCup-00130\GHCup-00130]8;;\] The version 2.0.0.1 of the tool hls is not installed.

2023-07-29 05:28:48.3520000 [client] ERROR Error executing 'ghcup --no-verbose whereis cabal 3.10.1.0' with error code 30
2023-07-29 05:28:48.3520000 [client] ERROR stderr: [1m[ Error ][0m []8;;https://errors.haskell.org/messages/GHCup-00130\GHCup-00130]8;;\] The version 3.10.1.0 of the tool cabal is not installed.

2023-07-29 05:28:48.3690000 [client] ERROR Error executing 'ghcup --no-verbose whereis stack 2.11.1' with error code 30
2023-07-29 05:28:48.3690000 [client] ERROR stderr: [1m[ Error ][0m []8;;https://errors.haskell.org/messages/GHCup-00130\GHCup-00130]8;;\] The version 2.11.1 of the tool stack is not installed.

但在VS Code终端中,所有工具已正确安装且在PATH中:

vscode@c983ad0b50ef:/workspaces/Ampersand$ which stack
/usr/local/bin/stack
vscode@c983ad0b50ef:/workspaces/Ampersand$ whoami
vscode
vscode@c983ad0b50ef:/workspaces/Ampersand$ echo $PATH
/vscode/vscode-server/bin/linux-x64/2ccd690cbff1569e4a83d7c43d45101f817401dc/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
vscode@c983ad0b50ef:/workspaces/Ampersand$ 

解决建议

  • 问题根源:使用--isolate参数安装工具时,ghcup不会将这些安装记录到自身的元数据中,导致Haskell插件通过ghcup whereis命令无法找到对应版本的安装记录,即便工具实际存在且可执行。

  • 方案一:移除--isolate参数
    修改Dockerfile中的ghcup安装命令,去掉--isolate参数,让ghcup正常追踪安装版本:

    RUN ghcup -v install ghc ${GHC}
    RUN ghcup -v install cabal ${CABAL}
    RUN ghcup -v install stack ${STACK}
    RUN ghcup -v install hls ${HLS}
    

    同时可以添加ghcup set命令设置默认版本,确保插件能识别:

    RUN ghcup set ghc ${GHC}
    RUN ghcup set cabal ${CABAL}
    RUN ghcup set stack ${STACK}
    RUN ghcup set hls ${HLS}
    
  • 方案二:手动配置Haskell插件路径
    如果需要保留--isolate参数,可以在.devcontainer/devcontainer.json中指定工具的绝对路径,跳过ghcup检测:

    "settings": {
      "haskell.ghcPath": "/usr/local/bin/ghc",
      "haskell.cabalPath": "/usr/local/bin/cabal",
      "haskell.hlsPath": "/usr/local/bin/haskell-language-server",
      "haskell.stackPath": "/usr/local/bin/stack"
    }
    
  • 额外优化:切换到vscode用户前,确保工具目录的权限正确,避免后续使用出现权限问题:

    RUN chown -R ${USER_UID}:${USER_GID} /usr/local
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 00:12:04