Relocatability of @__DIR__

I am using the DIR macro in a function like:

function foo()
    return joinpath(@__DIR__, "myfile.ext")
end

If function foo() gets precompiled using PackageCompiler.jl into a sysimage, the @__DIR__ is “hardcoded” into the function. This entails that I can not relocate that sysimage anywhere (which otherwise works as expected).

Is there any other way to lookup that folder, so that it gets evaluated at runtime?

Does this work?

my_dir = Ref{String}()

function __init__()
    my_dir[] = @__DIR__
end

function foo()
    return joinpath(my_dir[], "myfile.ext")
end

But see the caveats in the README.

1 Like