[erlang-bugs] code:load_abs/1 fails for packaged modules

Richard Carlsson <>
Sat Feb 7 13:37:28 CET 2009


cyril Romain wrote:
> _StepToReproduce_:
> Let's assume you have /home/me/project/src/test/mymodule.erl and 
> '-module(test.mymodule)' defined in mymodule.erl.
> And the corresponding beam file: /home/me/project/ebin/test/mymodule.beam.
> Run Erlang from the project directory with 'erl -pa ebin'.
> Now in Erlang code:load_abs("/home/me/project/ebin/test/mymodule") fails 
> with the following error report:
> 
> _ErrorReport_:
> =ERROR REPORT==== 28-Dec-2008::22:12:10 ===
> beam/beam_load.c(1035): Error loading module mymodule:
>   module name in object code is test.mymodule
> 
> _Problem_:
> In kernel-2.12.5/src/code_server.erl in load_abs/3 the try_load_module/4 
> call can fail if Mod0 is [] and if the module is a packaged module (i.e. 
> a module within a namespace).
> It is worth noting that code:load_abs/2 succeed though. Indeed 
> code:load_abs("/home/me/project/ebin/test/mymodule", test.mymodule) 
> returns {module,'test.mymodule'}.
> 
> Unfortunately I'm in a case where I only know the file name and not the 
> module name. FYI, I'm trying to fix dialyzer to support packaged module. 
> Dialyzer assumes the module name is the filename:basename, which is 
> wrong for packaged modules. And dialyzer cannot guess the module name 
> without loading the beam file, hence the  load_abs/1 call.

I think that the correct thing in this case is to use beam_lib:info()
to extract the actual module name from the beam file. To avoid reading
the file several times, you could use file:read_file() to fetch the
binary data, then call beam_lib:info() on it, and code:load_binary()
to perform the actual load, using the name found in the beam info.

Arguably, load_abs/1 should do this automatically. After all, if it
is supposed to load a module given only a path, it should be responsible
for getting the actual name right. But I think that the "try prepending
the parent directory name" approach feels wrong.

    /Richard


-- 
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham



More information about the erlang-bugs mailing list