Skip to content

Imports inserted by plugin do not influence topographical module sorting

Summary

Sorry, this is a weird one 😄 I'm not sure if it's actually a bug in GHC or just a misunderstanding on my part. I'm looking for clarification from someone who knows more about GHC internals.

I describe the issue in detail here: https://github.com/tfausak/imp/issues/11. But long story short, if you use a plugin to insert an import of a module from the same unit, it won't work because the module you're importing won't be loaded. I believe this happens because while plugins can modify the parsed module, when GHC discovers imports it uses the contents of the actual source file before any plugins have a chance to run.

Steps to reproduce

Define a package like this and try to build it:

-- 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

Expected behavior

In the B module, I expect the identifier A.mainA to work because the Imp plugin inserts import qualified A. Instead I get an error like this:

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

Note that in the A module, the identifier System.IO.print works because Imp inserts import qualified System.IO.

Environment

  • GHC version used: 9.8.2

Optional:

  • Operating System: macOS 14.4
  • System Architecture: ARM64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information