Skip to content

PrecompileTools doesn't run __init__() so some functionality may not work during package compilation? #32

@NHDaly

Description

@NHDaly

If I have a package's workfile file that runs functionality in the package, from what I understand @compile_workload doesn't run the package's __init__() function, so the package's module will still be uninitialized during the workload?

For example:

module TestPackage

using PrecompileTools

const PORT = Ref(0)

function __init__()
    PORT[] = rand(2222:8888)
end

function test()
    # start up a server and run the tests...
    @assert PORT[] != 0
    # ...
end

@compile_workload begin
    test()
end

end # module TestPackage

Then:

julia> using TestPackage
[ Info: Precompiling TestPackage [13cf7a4f-c364-47a8-9398-f77e91fc5657]
ERROR: LoadError: AssertionError: PORT[] != 0
Stacktrace:
 [1] test
   @ ~/tmp/TestPackage/src/TestPackage.jl:13 [inlined]
 [2] macro expansion
   @ ~/tmp/TestPackage/src/TestPackage.jl:18 [inlined]
 [3] top-level scope
   @ ~/.julia/packages/PrecompileTools/kmH5L/src/workloads.jl:78
 [4] include
   @ ./Base.jl:457 [inlined]
 [5] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing)
   @ Base ./loading.jl:2049
 [6] top-level scope
   @ stdin:3
in expression starting at /Users/nathandaly/tmp/TestPackage/src/TestPackage.jl:1

Does that make sense?
What do other packages usually do here? Are we meant to call __init__() manually? I didn't see anything about this in the docs.
Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions