I’m developing a Julia-based simulation model that runs from the terminal (no UI). Each time I execute the script, it precompiles all dependencies, which takes quite a long time (around 8 minutes).
Is there a way to keep installed dependencies (e.g., JuMP, Agents) precompiled and cached so that subsequent runs only compile my own modules, rather than recompiling all packages each time? Ideally, I’d like behavior similar to running code from VS Code, where even between sessions it does not recompile everything only the custom modules.
Edit:
I tried the solution from this thread but it did not work for me.
Here are the dependencies I have (Revise only for development)
Odd, precompile caching is the default behavior and it is usually only overwhelmed by frequent-enough environment switching. Just to check our terminology here, are you seeing a Precompiling printout each session?
In my experience, frequent precompilation has often to do with code loading from shared environments before activating the actual environment (for example when you load Revise in your startup and activate the env within your script). See also
How are the script’s dependencies set up? If they are not in an environment and you create a temp environment then they cannot be cached.
Please post how exactly you run the script and the beginning of the script where you load the libraries you want to use.
Yes I updated the post with more details, as shown each time I run my code it precompiles everything even when I don’t modify anything. I tried a solution from an old thread but it did not work for me.
Yes I do use Revise for development and my script activate the environment and check for missing packages because it will be used by other people in the future so I have to make sure they have all the dependencies. I will try to remove Revise and see what happens. Thank you
I updated the post with the Project.toml content, the program uses a combination of local modules and installed dependencies and because the program will be used by other people each time I run the code Main.jl it executes this part to check for packages:
using Pkg
Pkg.activate(".")
Pkg.instantiate()
Besides this part nothing fancy happens it executes the main() which is just an encapsulation for some function calls from the project.
Note: I do use Revise.jl for development which I will try to remove and check if it resolves the issue.
I think the actual solution is to always execute scripts with
julia --project=@. script.jl
(possibly replace @. with whatever path your environment lives at) and basically never Pkg.activate during a session. This will activate the correct environment beforestartup.jl is executed, therefore statements like using Revise are evaluated/resolved in the correct environment and the state of your main env manifest does not leak into your actual environment.
This is a very common footgun with cached precompilation + stackend environments. Personally, I think activate should warn each time you activate an environment whose manifest is partly ignored because different versions of packages are already loaded (warnings like this sometimes appear during precompilation).