尝试使用postgresql-simple在GHCI中创建数据库连接时,我得到GHCI段错误。
我正在使用GHC 8.0.1,OSX 10.11.16。我没有系统GHC,只有堆栈安装的系统。
如果我在项目中执行以下操作,则会出现段错误:
$ stack ghci
Loaded GHCi configuration from /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci62972/ghci-script
gchi> :set -XOverloadedStrings
gchi> import Database.PostgreSQL.Simple
gchi> let connstr = "host=localhost port=5432 user=myusername password=mypass dbname=local"
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault stack ghci
我认为可能值得查看它提到的文件,所以这里有内容:
> cat /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci62972/ghci-script
:load "/Users/erewok/projects/haskell/simpleservantblog/app/Main.hs" "Api" "Api.Post" "Api.User" "Config" "Html.Home" "Models.Author" "Models.Post"
:module + Api Api.Post Api.User Config Html.Home Models.Author Models.Post
这是从项目加载的所有模块的列表(编译并运行正常)。
以下是从ghci
开始到seg fault的所有内容的完整输出:
$ stack ghci
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Using main module: 1. Package `simpleservantblog' component exe:simpleservantblog-exe with main-is file: /Users/erewok/projects/haskell/simpleservantblog/app/Main.hs
Configuring GHCi with the following packages: simpleservantblog
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /Users/erewok/.ghci
[1 of 8] Compiling Models.Author ( /Users/erewok/projects/haskell/simpleservantblog/src/Models/Author.hs, interpreted )
[2 of 8] Compiling Models.Post ( /Users/erewok/projects/haskell/simpleservantblog/src/Models/Post.hs, interpreted )
[3 of 8] Compiling Html.Home ( /Users/erewok/projects/haskell/simpleservantblog/src/Html/Home.hs, interpreted )
[4 of 8] Compiling Api.User ( /Users/erewok/projects/haskell/simpleservantblog/src/Api/User.hs, interpreted )
[5 of 8] Compiling Api.Post ( /Users/erewok/projects/haskell/simpleservantblog/src/Api/Post.hs, interpreted )
[6 of 8] Compiling Config ( /Users/erewok/projects/haskell/simpleservantblog/src/Config.hs, interpreted )
[7 of 8] Compiling Api ( /Users/erewok/projects/haskell/simpleservantblog/src/Api.hs, interpreted )
[8 of 8] Compiling Main ( /Users/erewok/projects/haskell/simpleservantblog/app/Main.hs, interpreted )
Ok, modules loaded: Api, Config, Api.Post, Api.User, Html.Home, Models.Post, Models.Author, Main.
Loaded GHCi configuration from /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/ghci64266/ghci-script
gchi> :set -XOverloadedStrings
gchi> import Database.PostgreSQL.Simple
gchi> let connstr = "host=localhost port=5432 user=myusername password=mypass dbname=local"
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault stack ghci
更多信息
Postgresql版本:9.5,使用EnterpriseDB installer
安装发布我的 stack.yaml 的请求(位于下方)让我想起了trying to install postgresql-libpq时遇到的问题。这可能是相关的。
Stack.yaml
# This file was automatically generated by stack init
# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration/
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: nightly-2016-08-25
# Local packages, usually specified by relative directory name
packages:
- '.'
- location:
git: https://www.github.com/mattjbray/elm-export
commit: 8868c1f09597f44c2e18e014cd9fbcf8320c3fea
extra-dep: True
- location:
git: https://www.github.com/mattjbray/servant-elm
commit: e13c8def8127ea339e9801d804638854947193e8
extra-dep: True
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps:
- wai-make-assets-0.1.1
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
extra-lib-dirs: [/usr/local/opt/libiconv/lib, /usr/local/lib, /usr/lib]
还使用解析器lts-6.17对GHC 7.10.3进行了段错误
gchi> conn <- connectPostgreSQL connstr
zsh: segmentation fault stack ghci --resolver lts-6.17
我的想法
我认为我使用的postgresql-libpq
版本是使用{strong> Postgresql 9.4 编译的libiconv.2.dylib
构建的,但我尝试连接的数据库实际上是 Postgresql 9.5 。这可能是我问题的原因。
我的灵感来自于这个想法
以下是我尝试进一步调试和/或修复此问题的一些步骤。我首先将Postgresql 9.5 *dylib
文件链接到我的/usr/local/lib
目录中,以替换与Postgresql 9.4版本链接的旧文件。
$ cd /usr/local/lib
$ sudo ln -s -f /Library/PostgreSQL/9.5/lib/*dylib .
之后,我尝试强制stack
以这种方式重建postgresql-libpq
和postgresql-simple
:
$ stack exec -- ghc-pkg unregister --force postgresql-simple
$ stack exec -- ghc-pkg unregister --force postgresql-libpq
$ rm -rf ~/.stack/precompiled/x86_64-osx/ghc-8.0.1/1.24.0.0/postgresql-libpq-0.9.1.1/
$ rm -rf ~/.stack/precompiled/x86_64-osx/ghc-8.0.1/1.24.0.0/postgresql-simple-0.5.2.*
$ stack build
此时,它答应我将构建和配置已删除的软件包。但是,当我运行上述内容时仍然会发生分段错误。
答案 0 :(得分:0)
在尝试为我的问题生成堆栈跟踪时,我发现我可以通过以下方式启动ghci:
$ stack ghci --ghci-options -fexternal-interpreter
然后,无论出于何种原因,段错误都不会出现,一切都按预期工作。我不知道是否有其他人会发现这有用。
对我来说似乎很奇怪的是,在编译和运行代码时我没有遇到任何问题。我只在GHCI有一个问题。
无论如何,在我发现更多信息或某人有更好的建议之前,我会继续这样做,以便继续在REPL中测试我想要的内容。