Alexander Mills
Alexander Mills

Reputation: 100416

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)

Upvotes: 4

Views: 569

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31355

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

stack runghc -- -isrc app/Main.hs

Upvotes: 5

Related Questions