Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use modules from same package #11

Closed
tfausak opened this issue Mar 22, 2024 · 5 comments · Fixed by #15
Closed

Cannot use modules from same package #11

tfausak opened this issue Mar 22, 2024 · 5 comments · Fixed by #15
Labels
bug Something isn't working

Comments

@tfausak
Copy link
Owner

tfausak commented Mar 22, 2024

Consider a simple package like this:

-- example.cabal
cabal-version: 3.8
name: example
version: 0
library
  build-depends: base, imp
  ghc-options: -fplugin=Imp
  exposed-modules: A B
-- A.hs
module A where
mainA = System.IO.print 'a'
-- B.hs
module B where
mainB = A.mainA

This should work since B should just import A, but if you actually try to build it you'll get an error:

B.hs:1:1: error: [GHC-58427]
    attempting to use module ‘example-0:A’ (A.hs) which is not loaded
  |
1 | module B where
  | ^
Error: cabal: Failed to build example-0.

I'm guessing that this happens because GHC figures out the topographic sort of modules before actually compiling them. Since the import isn't actually in the file, GHC is free to compile them in either order. Or, more to the point, it doesn't have to make A available to B.

Relevant documentation for getImports: https://hackage.haskell.org/package/ghc-9.8.2/docs/GHC-Parser-Header.html#v:getImports

@tfausak tfausak added the bug Something isn't working label Mar 22, 2024
@tfausak
Copy link
Owner Author

tfausak commented Mar 22, 2024

I initially thought this could be related to "implicit" imports (#5), but I get the same error either way. This GHC issue is kind of related: https://gitlab.haskell.org/ghc/ghc/-/issues/21730

I could, as that issue suggests, replace the import with an Exact name. However that wouldn't fix this problem.

@tfausak
Copy link
Owner Author

tfausak commented Mar 22, 2024

I think there's nothing that Imp can do about this. The earliest a plugin can do anything is in driverPlugin, and by the time that's called the modules have already been sorted.

Either this can be considered a GHC bug, or it's just a limitation of what Imp can do. I'll open an upstream ticket to see what GHC devs have to say.

@tfausak
Copy link
Owner Author

tfausak commented Mar 22, 2024

I created an upstream issue for this: https://gitlab.haskell.org/ghc/ghc/-/issues/24579

In the meantime, it might suffice to say that Imp can't be used to import modules from the same unit (component).

@tfausak
Copy link
Owner Author

tfausak commented Mar 23, 2024

The title of this issue says "same package" but really it means "same component". You can work around this problem by referring to modules from another component, like a sub-library. For example:

-- example.cabal
cabal-version: 3.8
name: example
version: 0

common library
  build-depends: base, imp
  ghc-options: -fplugin=Imp

library a
  import: library
  hs-source-dirs: a
  exposed-modules: A

library b
  import: library
  hs-source-dirs: b
  build-depends: example:a
  exposed-modules: B
-- a/A.hs
module A where
mainA = System.IO.print 'a'
-- b/B.hs
module B where
mainB = A.mainA

@tfausak
Copy link
Owner Author

tfausak commented Mar 29, 2024

Upstream has confirmed that this is simply a limitation of GHC plugins. Imp cannot support this, so all I need to do is update the documentation to say that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant