Repository navigation
Open
Description
Describe the bug
shebang script interpreter fails if the script spans over multiple modules
To Reproduce
ghc compiles this program successfully.
$ cat OtherModule.hs
module OtherModule (hello) where
hello :: String
hello = "hello"
$ cat hello.hs
#! /usr/bin/env cabal
{- cabal:
build-depends: base
ghc-options: -Wall
-}
module Main where
import Example.Hello (hello)
main = putStrLn hello
$ ghc hello.hs
[1 of 2] Compiling OtherModule ( OtherModule.hs, OtherModule.o )
Linking hello ...
But cabal run fails:
$ cabal v2-run hello.hs
Resolving dependencies...
Build profile: -w ghc-8.8.3 -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, /var/folders/p4/fdt36vy95f52t_3dnpcx8_340000gn/T/cabal-repl.-24197/dist-newstyle/build/x86_64-osx/ghc-8.8.3/fake-package-0/x/script/build/script/script-tmp/Main.o )
Main.hs:7:1: error:
Could not find module ‘OtherModule’
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
7 | import OtherModule (hello)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expected behavior
cabal script interpreter should be able to compile and run the above program.
System information
- Mac OSX
$ cabal --version
cabal-install version 3.2.0.0
compiled using version 3.2.0.0 of the Cabal library
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.3
Reacted by Julien M., Artem Pelenitsyn and Julio César
Activity
phadej commentedon May 12, 2020
I think this is out of scope of interpreter functionality. At that point you should create a package.
cc @hvr
harendra-kumar commentedon May 13, 2020
That's fine too, it will be nice if we can mention this in the user guide. I was just wondering if this is easy to implement.
ju1m commentedon May 8, 2021
This would enable the usage of TemplateHaskell which can only run a function at compile time if it is imported from another module.
A workaround is to add
hs-source-dirs: /absolute/path/to/directory, but this has poor portability.georgefst commentedon Dec 10, 2021
With 3.6.2.0, that's given me:
And presumably relative paths is yet another thing blocked on #6977.
bacchanalia commentedon Dec 10, 2021
A trick to fake an absolute path as a relative path is to add a while lot of ../../../../ to the front (enough to ensure you get all the way to root no mater where you want to use it from). I've been using this on my work for
replsupport baring a better solution being found so I'd expect it to work here too.bacchanalia commentedon Dec 11, 2021
I have multi module script support working in #7851
Use script in-place for build or run
fgaz commentedon Dec 11, 2021
Hmm, even though that commit makes it possible, I'd still not advertise multi-module scripts as supported. I agree with @phadej that at that point you really should create a package since you already have multiple files anyway
jneira commentedon Dec 11, 2021
Also agree, you aways can create a dummy script to access the package like we did in hls: https://github.com/haskell/haskell-language-server/blob/master/install.hs
Use script in-place for build or run
bacchanalia commentedon Dec 23, 2021
Unfortunately, the final version of #7851 will not allow for multi-module scripts, because I had to balance it against other considerations. I think it's unlikely to be added in the future do to those same considerations.
Use script in-place for build or run
Use script in-place for build or run
Better support for running scripts. (#7851)