提问人:peer 提问时间:11/12/2023 最后编辑:peer 更新时间:11/12/2023 访问量:41
如何在阴谋集团repl中导入隐藏的包来加载阴谋集团脚本?
How to load cabal script with import of hidden package in cabal repl?
问:
我有一个带有外部依赖项的脚本,我可以用它执行。我想在/中加载此脚本,并尽可能减少副作用。cabal run
ghci
cabal repl
这是我的脚本:
#!/usr/bin/env cabal
{- cabal:
build-depends: base
, split
-}
import Data.List.Split (splitOn)
main :: IO ()
main = do
let orders = splitOn "x" "axbxc"
putStrLn $ head orders
我可以执行它 编译一个具有外部依赖关系的 haskell 脚本,而无需 cabal
$ cabal run script.hs
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/t/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Preprocessing executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
[1 of 1] Compiling Main ( Main.hs, /tmp/cabal-repl.-4510/dist-newstyle/build/x86_64-linux/ghc-9.4.7/fake-package-0/x/script/build/script/script-tmp/Main.o )
[2 of 2] Linking /tmp/cabal-repl.-4510/dist-newstyle/build/x86_64-linux/ghc-9.4.7/fake-package-0/x/script/build/script/script
a
但是如果我在 ghci 中导入它,则外部依赖项存在问题:
$ cabal repl
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/t/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 9.4.7: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /tmp/cabal-repl.-4707/setcwd.ghci
ghci> :load script.hs
[1 of 2] Compiling Main ( script.hs, interpreted )
script.hs:7:1: error:
Could not load module ‘Data.List.Split’
It is a member of the hidden package ‘split-0.2.4’.
Perhaps you need to add ‘split’ to the build-depends in your .cabal file.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
7 | import Data.List.Split (splitOn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
既然我已经将“split”添加到脚本的构建依赖项中,那么我有什么方法可以指向该部分吗?
我想让事情尽可能简单,不要添加更多文件。
我还想避免在系统级别安装库,以避免版本冲突并保持一切可重现。ghci
我尝试过的事情
如何在阴谋集团repl中自动加载导入?
建议表格,但这似乎对我不起作用:*M
ghci> :load *script.hs
[1 of 2] Compiling Main ( script.hs, interpreted )
script.hs:7:1: error:
Could not load module ‘Data.List.Split’
It is a member of the hidden package ‘split-0.2.4’.
Perhaps you need to add ‘split’ to the build-depends in your .cabal file.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
7 | import Data.List.Split (splitOn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
根据 Noughtmare 的建议:
cabal repl script.hs
对我不起作用:
$ cabal --version
cabal-install version 3.6.2.0
compiled using version 3.6.2.0 of the Cabal library
bash-5.1$ cabal repl script.hs
cabal: 'repl' doesn't take any extra arguments when outside a project:
script.hs
但是,这有效:
$ cabal repl --build-depends split
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 9.4.7: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /tmp/cabal-repl.-11713/setcwd.ghci
ghci> :load script.hs
[1 of 2] Compiling Main ( script.hs, interpreted )
Ok, one module loaded.
答:
1赞
Noughtmare
11/12/2023
#1
从 cabal 3.8.1.0 开始,你可以在脚本上调用 cabal repl:
$ cabal repl script.hs
参见阴谋集团repl文档的最后一部分:
https://cabal.readthedocs.io/en/stable/cabal-commands.html#cabal-repl
对于较旧的阴谋集团版本,你可以先打开一个带有手动指定依赖项的 repl,然后加载脚本:
$ cabal repl --build-depends split
...
ghci> :load script.hs
[1 of 2] Compiling Main ( script.hs, interpreted )
Ok, one module loaded.
评论
0赞
peer
11/12/2023
这没有用(见我更新的问题),但它为我指明了正确的方向,谢谢!
0赞
Noughtmare
11/12/2023
@peer你们的阴谋集团版本是在脚本支持被添加到阴谋集团复兴之前发布的。如果您升级到任何更高版本,它将被唤醒。
0赞
peer
11/12/2023
谢谢,ghcup 仍然推荐,所以这就是我安装的,但我刚刚切换到,现在它可以工作了。cabal 3.6.2.0
cabal 3.10.2.0
评论