[erlang-questions] Setting the proper source in a module compiled via compile:forms

Richard Carlsson <>
Thu Mar 22 21:20:30 CET 2012


On 03/22/2012 08:43 PM, José Valim wrote:
>     I think you must use `-file("real/path", Line).' (where Line is an
>     integer literal). The version with arity 1 (just the file name) is
>     probably just treated as an unknown user-defined attribute and ignored.
>
>
> Thanks! I have tried the following snippet, still with no success:
>
> 1> Code = [{attribute,1,file,{"real/path",1}},{attribute,1,module,demo}].
> 2> Tuple = compile:forms(Code).
> 3> code:load_binary(demo, "real/path", element(3, Tuple)).
> 4> demo:module_info().
>
> module_info still returns pwd as source. I must add I am using R15B.

Sorry, I missed that point. The -file declarations are for getting the 
correct debugging info and compiler error messages. The source entry in 
module_info is a different thing. Looking in compile.erl, it seems that 
if you compile from forms, the source file is always set to "" (and then 
later, a call to dirname() turns this into ".", which is then passed to 
absname() before it is sent to beam_asm, so that's why you get your 
current directory). Perhaps you could patch compile.erl and submit your 
change to ?

    /Richard



More information about the erlang-questions mailing list