Default project activation via Pkg.jl or DrWatson.jl

Right now my, I have several directories, which have Project.toml files because of 2 situations:
(1) I have cloned them from Github repos, or
(2) I have created them myself as DrWatson.jl “scientific” projects.

In order to have the corresponding environments automatically loaded when I launch Julia, I created a startup.jl file reading:

using OhMyREPL
using DrWatson
if isfile("Project.toml") && isfile("Manifest.toml")
    quickactivate(".")
end

It works ok for the case (2) above, but I would like to contemplate case (1) as well, where the command quickactivate is not relevant (despite not issuing any errors…); in this case (1) I guess I would like to run something like

using Pkg
activate(".")

Which conditional should I test to contemplate this case (1)? It seems the Project.toml file does not have a variable name set instead of what happens in case (2) with the DrWatson projects…

How should I check this and change my startup.jl file accordingly? I hope I have been clear, but am not sure…
Thanks in advance.

Is this a good idea? it seems to me that it can lead to a lot of accidentally created environments. Anyway,

I would think of using DrWatson’s findproject. Something like:

using DrWatson
import Pkg
project_found = findproject(".") #Will look recursively for an existing project, similar to quickactivate
project_dir = isnothing(project_found) ? "." : project_found
Pkg.activate(project_dir)

Another option is to run julia as julia --project=@., which you can do with an alias.

DrWatson.quickactivate or DrWatson.@quickactivate should work irrespectively whether it’s a github repo or a DrWatson project or a local package. It will always activate the environment as long as a parent directory contains at least a Project.toml.
Maybe you are not in the correct directory "." that you need to activate ?

I would totally avoid having something like this in my startup.jl:

using Pkg
activate(".")

because imagine starting up julia from $HOME/../myproject/src/ you will end up activating src, which is not desirable.

As a reminder, running julia --project most of the times is what you want as it will also search the parent directories and activate the first Project.toml found.

@filchristou

I sure was in the directory with the .toml file and my current startup.jl file did correctly activate its project, via DrWatson’ s quickactivate command there, but my problem is that, in case (1) I really do not have a bona fide DrWatson “scientific” project and some commands, such as projectname() does not work as expected, because it returns Nothing instead of the name of the directory…

As concerns the command block:

using Pkg
activate(".")

I would use it, if at all, only inside an if or elseif block, with the conditional to check for the existence of the .toml file…

@aramirezreyes

I was not thinking of using the commands

using Pkg
activate(".")

outside of a conditional if block. Then I guess it would not “lead to a lot of accidentally created environments”, right?

Your piece of code seems to be a good suggestion for both cases (1) and (2). I wonder however what it would miss by not using DrWatson’s quickactivate command; for instance, the call projectname() returns Nothing instead of the expected (?) directory name containing the .toml file. The findproject() on its turn does output the full path of this directory… After all, a DrWatson project is somewhat different from a pure Pkg project, right?

I don’t think you will lose anything. The code for quickactivate does not do anything fancy at all.

function quickactivate(path, name = nothing)
    projectpath = findproject(path)
    if projectpath === nothing || projectpath == dirname(Base.active_project())
        return nothing
    end
    Pkg.activate(projectpath)
    if !(name === nothing) && projectname() != name
        error(
        "The activated project did not match asserted name. Current project "*
        "name is $(projectname()) while the asserted name is $name."
        )
    end
    return nothing
end

In the end it uses Pkg.activate if it finds a project. The only difference with what you want is that it will activate a project in the current working directory. In this sense I don’t think a DrWatons Project is any different than a regular one.

I think I agree with @filchristou that what you want is just a use quickactivate, quickactivate will work for any project, regardless if it was created using DrWatson or not.

correct!

So to sum up, you are suggesting/recommending that my startup.jl might stay as is:

using OhMyREPL
using DrWatson
if isfile("Project.toml") && isfile("Manifest.toml")
    quickactivate(".")
end

so that, there would be a default project activation if and only if I launch julia from a directory where there are both Project.toml and Manifest.toml files, no matter how they were created. Is that it?
Thanks again!

1 Like

You do not need DrWatson for this block… Since you are anyways explicitly checking for the existence of a project file you can replace quickactivate(".") with just Pkg.activate(".").

Edit: I am assuming you DO NOT want the extra functionality quickactivate has of searching on parent folders of current folder.