Is there a command for forcing a fresh precompilation of a module?

Hey, I am wondering if one can force a module to be precompiled. So I am looking for a command like

precompilemodule( "pathto/MyModule.jl")

It’s easiest to create a package and dev it.

1 Like

Thanks for your answer @tim.holy

But it does not look as if this was the best solution for my problem, maybe I should have described it a little more. So let me do it now:
What I have is a module MyModule that collects all constants for a project that are used in different other modules of the project. However the problem is that some constants are loaded in MyModule from some datafile.jld2 files using the JLD2 package and those data might be updated from time to time.
It took me a long time to figure that a precompalation of MyModule is required, when the data in datafile.jld2 is updated. But the problem is, a precompalation will not automatically take place the next time the package is used because MyModule.jl actually never needs to be modified and (as far as I know) only modifications in MyModule.jl lead to new automatic precompalation.
My module looks as follows:

module MyModule

using JLD2, FileIO

export somedata


@load "../jld2s/datafile.jld2" somedata

end

What I do right now is making some unecessary alibi modifcations in MyModule.jl to force a new precompalation when the data in datafile.jld2 is updated. However from time to time I forget to do so and get into trouble.
Hence what I would really love to do is forcing a precompalation immeditately after the data in datafile.jld2 is updated.

You can do this but MyModule will have to be a little more complicated. First, define an __init__ function and use watch_file from the FileWatching standard library so that you’re notified of changes to any files that you draw constants from. Second, your “constants” probably deserve a little bit of thought. If they are const then you will likely encounter trouble because they may get compiled into some of your methods that use them, and those won’t update. It would be better to have them be non-constant, and it might be worth considering storing them in Dict or something. You could then have your file-watching utility just reload the relevant items in the Dict when a file changes.

1 Like

Thanks for you answer, @tim.holy.
I tried to use watch_file, but unfortuantely it did not work out as expected. I seemed as if MyModule was kind of waiting for changes in datafile.jld2 and would not continue until the file was changed, but all changes take place before MyModule is used and hence it should check if datafile.jld2 has changed instead of waiting for a change.
However I’m quite greateful for your input and your idea anyway since it brought me on the right track. A solution that works as desired can be implemented by a single line of code, using the Base.include_dependency function.
One would simply need to write

module MyModule

using JLD2, FileIO

export somedata

include_dependency( "../jld2s/datafile.jld2")
@load "../jld2s/datafile.jld2" somedata

end

This has the effect that MyModule will check if datafile.jld2 has changed since the last precompaliation and recompile again if that’s the case, exactly as I want it to be.

I will now consider to store my data in a Dict as you suggested, too, because I think it makes sense.
Thanks for your help!

1 Like

Glad you found a good solution!

That’s how watch_file works. Typically you use it in conjunction with an @async somewhere, using a separate Task to perform the updates. But as warned, this solution is more complicated, and if you don’t need the values to update mid-session then indeed include_dependency will be a much simpler solution.