Project Specific Startup.jl

Hello,

I am using DrWatson to manage some simulations for an upcoming conference. I am using some locally developed packages that do not include DrWatson in their dependencies.

They create some complicated data structures that I have my own methods defined for saving to file using HDF5.

I want to take advantage of the DrWatson saving tools so I need to overload _wsave per the docs. That’s fine - however I’d prefer not to have to do this in every script I create nor do I want to add DrWatson as a dependency to my packages.

I know that I can have methods be created on startup via ./julia/config/startup.jl however I think(?) that is global. I’d like to confine the definition of these methods to a specific project/environment.

Does that make sense? What is the best method to achieve something like a local startup.jl that gets run any time I run Julia in a particular environment?

Your startup.jl could look like this:

const startup_project = Base.active_project()

if startup_project == "/home/mkitti/Project.toml"
    hello_world() = println("Hello World")
elseif startup_project == "/home/mkitti/secret_project/Project.toml"
   # something secret
end

This requires you to use julia --project="folder/containing/a/project/toml" for this to work.

3 Likes

Alternatively, if you use the VS Code extension, that’s done automatically when you start a session in a folder that contains a Project.toml.

Expanding on @mkitti’s answer, if you do not want to list all projects in your global startup.jl file, you can also make it load per-project startup.jl files if they are defined in the project root directory.

E.g put this in ~/.julia/config/startup.jl

let startup_project = Base.active_project()
    startup_file = joinpath(dirname(startup_project), "startup.jl")
    if isfile(startup_file)
        @info "Loading Project-specific startup file" path=startup_file
        include(startup_file)
    end
end

and add a startup.jl file in the root directory of your project (alongside the Project.toml file)

8 Likes