Skip to content

Multi module script support #6787

Open
Open

Description

@harendra-kumar

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

Activity

  1. phadej commented on May 12, 2020

    @phadej
    Collaborator

    I think this is out of scope of interpreter functionality. At that point you should create a package.

    cc @hvr

  2. harendra-kumar commented on May 13, 2020

    @harendra-kumar
    Author

    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.

  3. ju1m commented on May 8, 2021

    @ju1m

    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.

  4. added
    can-workaround
    on Aug 18, 2021
  5. georgefst commented on Dec 10, 2021

    @georgefst

    A workaround is to add hs-source-dirs: /absolute/path/to/directory

    With 3.6.2.0, that's given me:

    Warning: script block:5:57:
    unexpected absolute FilePath
    cabal: Failed parsing "script block".
    

    And presumably relative paths is yet another thing blocked on #6977.

  6. removed
    can-workaround
    on Dec 10, 2021
  7. bacchanalia commented on Dec 10, 2021

    @bacchanalia
    Collaborator

    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 repl support baring a better solution being found so I'd expect it to work here too.

  8. bacchanalia commented on Dec 11, 2021

    @bacchanalia
    Collaborator

    I have multi module script support working in #7851

  9. fgaz commented on Dec 11, 2021

    @fgaz
    Member

    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

  10. jneira commented on Dec 11, 2021

    @jneira
    Member
  11. bacchanalia commented on Dec 23, 2021

    @bacchanalia
    Collaborator

    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.

  12. added a commit that references this issue on Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions