Determine ModuleName of loaded target

130 Views Asked by At

I was wondering how do you determine the ModuleName of the file you've just loaded in ghc using the API.

If you look at the Documentation of the API they always assume you know the module name before you load the file in.

I have tried top sorting the dependency graph and picking the last module there, but that doesn't seem to always return the file that was loaded in.

The way I have been able to do it previously was to modify the GHC source code to store this information in the HscEnv.

It would be nice if the call to load would return it, But there surely must be a way to do it already since GHCi knows this when it loads a file.

1

There are 1 best solutions below

1
On

The haskell-src-exts package has a parseFile function with type parseFile :: FilePath -> IO (ParseResult Module). The Module type has a field for the module's name.