运行stack ghci报错:模块‘main:Course’在同一文件重复定义
运行
stack ghci时提示模块main:Course重复定义的原因及解决办法 问题场景
在开发fp-course项目时,执行stack ghci后终端输出如下错误:
... [51 of 51] Compiling Test.Loader ( src/Test/Loader.hs, interpreted ) Ok, 51 modules loaded. Loaded GHCi configuration from /home/lpied/haskell/fp-course/.ghci <no location info>: error: module ‘main:Course’ is defined in multiple files: src/Course.hs src/Course.hs Failed, 51 modules loaded. Loaded GHCi configuration from /tmp/haskell-stack-ghci/75d10d48/ghci-script
可能原因
.ghci配置与Stack自动加载冲突:项目根目录的.ghci文件中可能包含加载src/Course.hs的指令(比如:load src/Course.hs),而Stack启动ghci时已自动加载该模块,导致重复加载。- 构建配置文件重复声明源文件:
package.yaml或.cabal文件里,src/Course.hs被同时添加到多个组件(比如library和executable)的源文件列表中,GHC会判定模块被多次定义。 - 构建缓存异常:
.stack-work目录的缓存文件损坏,导致Stack错误地重复识别了源文件。
解决步骤
- 检查项目根目录的
.ghci文件,移除或注释掉重复加载src/Course.hs的语句,重新运行stack ghci。 - 打开
package.yaml(或.cabal)文件,确认src/Course.hs仅在一个组件的源文件列表中出现,删除重复声明。 - 若以上方法无效,删除项目根目录下的
.stack-work目录,执行stack build重新构建项目后再启动stack ghci。
内容的提问来源于stack exchange,提问作者levant pied
相关产品推荐
相关产品推荐

