Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import adjacent module when running .hs file as script

I was following the directions here: https://haskell.fpcomplete.com/tutorial/stack-script

I have a cabal/stack project with this file structure:

app/Main.hs
src/Lib.hs

I can run Main.hs with:

stack runghc -- app/Main.hs 

that works if Main.hs does not import Lib.hs, but if it does, I get this error:

app/Main.hs:3:1: error:
    Ambiguous module name ‘Lib’:
      it was found in multiple packages:
      haskell-starter-0.1.0.0 libiserv-8.6.3
  |
3 | import Lib
  | ^^^^^^^^^^

is there anyway to include the src/Lib.hs file in the build? For a package it would look like:

stack runghc --package xyz -- app/Main.hs 

but what about for a module or file? Something like:

stack runghc --module src/Lib.hs -- app/Main.hs 

?

Update: So I tried this:

stack runghc -- -i src/* app/Main.hs

And I got:

src/Lib.hs:0:66: error:
    • Variable not in scope: main :: IO a0
    • Perhaps you meant ‘min’ (imported from Prelude)
like image 641
Alexander Mills Avatar asked Oct 29 '25 02:10

Alexander Mills


1 Answers

You need to pass runghc the -i option to tell it about include paths. The following should work:

stack runghc -- -isrc app/Main.hs
like image 177
Michael Snoyman Avatar answered Oct 31 '25 17:10

Michael Snoyman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!