@everywhere include() does not work with relative file paths

Suppose I have a file located at dir/ModuleA.jl:

module ModuleA
    export f

    function f()
        return("foo")
    end
end

and another file located at dir/main.jl:

addprocs(4)
@everywhere include("ModuleA.jl")

Running main.jl from inside dir with julia main.jl works fine, but fails from anywhere else because worker 2 tries to locate ModuleA.jl inside the current working directory.
As a fix I tried

@everywhere include(joinpath(@__DIR__, "ModuleA.jl"))

but @__DIR__ returns nothing on the workers.

Can someone enlighten me what’s happening here?
The problem I want to solve is making an existing module available on all workers while still being able to call the script from any working directory.

1 Like

I agree this behavior feels broken. The include function treats relative paths differently if called on the main process or on a worker.

on main: “relative” is relative to the file location
on workers: “relative” is relative to the working directory

My current work around is:

function include_everywhere(filepath)
    fullpath = joinpath(@__DIR__, filepath)
    @sync for p in procs()
        @async remotecall_wait(include, p, fullpath)
    end
end

include_everywhere("MyMod.jl")
using MyMod
1 Like