Jules
Jules

Reputation: 15199

How to remove module from "target list" in GHCi

In GHCI, you can add modules from your current project using :add module, or you can add modules to the current scope using :module + module. The modules loaded using :add will be automatically reloaded when you use the :reload command, but must be source modules, and cannot come from an external package.

If you accidentally use :add rather than :module + to attempt to load a module, future reloads always fail with an error message that the module is a package module rather than a source module. How can you remove the module so that reload can be used successfully again, without resetting the entire list of source modules via :load?

Upvotes: 2

Views: 696

Answers (1)

Zeta
Zeta

Reputation: 105886

Unfortunately, :load (or :cd) are the only ways to get rid of the target list. The only other function that changes the target list is :add, and as you know that one only adds additional targets.

However, judging by the source, a fix shouldn't be that hard. If you're willing to recompile GHC, something like this should work:

ghciCommands :: [Command]
ghciCommands = map mkCmd [
  ...
  ("rem",       keepGoingPaths removeModule,   completeFilename),
  ...

-- | @:rem@ command
removeModule :: [FilePath] -> InputT GHCi ()
removeModule files = do
  lift revertCAFs
  files'  <- mapM expandPath files
  targets <- mapM (\m -> GHC.guessTarget m Nothing) files'
  mapM_ GHC.removeTarget targets
  _ <- doLoadAndCollectInfo False LoadAllTargets
  return ()

That being said, I haven't tried that yet. Also, this might be worth a ticket on the official tracker.

Upvotes: 2

Related Questions