Haskell项目构建成功但VS Code提示找不到Data.Base模块如何解决?
解决Haskell模块导入的VS Code插件报错问题
项目结构
└── Project └── src └── Data ├── Base.hs └── Entities.hs
文件代码
Base.hs
module Data.Base (Color(..)) where data Color = White | Black | Other deriving (Show)
Entities.hs
module Data.Entities (Product(..), Shop(..), Order(..), Customer(..), productID, productName, productPrice, productColor) where import Data.Base (Color(..)) type ProductID = Int type ProductName = String type ProductPrice = Double type ProductColor = Color data Product = Product ProductID ProductName ProductPrice ProductColor deriving (Show) productID :: Product -> ProductID productID (Product pid _ _ _) = pid productName :: Product -> ProductName productName (Product _ name _ _) = name productPrice :: Product -> ProductPrice productPrice (Product _ _ price _) = price productColor :: Product -> ProductColor productColor (Product _ _ _ color) = color data Shop = Shop { shopID :: Int , shopName :: String , shopAddress :: String } deriving (Show) data Customer = Customer { customerID :: Int , customerName :: String , customerAddress :: String } deriving (Show) data Order = Order { orderID :: Int , orderNumber :: String } deriving (Show)
报错信息
Could not find module `Data.Base' Use -v (or `:set -v` in ghci) to see a list of the files searched for. | 3 | import Data.Base (Color(..)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
已知项目构建成功,推测是VS Code的Haskell相关插件导致的错误,可按以下方法解决:
配置插件的源码路径
在VS Code工作区的.vscode/settings.json中添加以下配置,指定源码目录和GHC参数:{ "haskell.ghcFlags": ["-i${workspaceFolder}/src"], "haskell.serverEnvironment": { "PATH": "${workspaceFolder}/src:$PATH" } }重启Haskell语言服务
按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Haskell: Restart Haskell Language Server并执行,让服务重新识别项目结构。确认工作区根目录
确保VS Code打开的是Project目录作为工作区,而非src或Data子目录,只有根目录打开才能让插件正确识别模块层级。清除插件缓存
找到VS Code的缓存目录(Windows:%APPDATA%\Code\Cache;Mac:~/Library/Caches/Code;Linux:~/.cache/code),删除Haskell相关缓存文件后重启VS Code。更新Haskell插件
打开VS Code扩展面板,找到Haskell相关插件(如Haskell或Haskell Language Server),更新至最新版本后重启编辑器。
内容的提问来源于stack exchange,提问作者Matvei Kazyukevich
相关产品推荐
相关产品推荐

