Julia environments list

here is my function to select and activate a user defined Julia-environment:

function select_Pkg_environment(_environment::String="")
    curr_dir = pwd()
    env_dir = joinpath(Pkg.envdir(), string("v", VERSION)[1:end-2])
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            # println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    if isempty(_environment)
        if isempty(env_list)
            println("No additional environments found!")
        else
            println("----   Number of available environments: ", length(env_list), "  ----")
            for i_ = eachindex(env_list)
                println(i_, ".) ",env_list[i_])
            end
            println("------------------------------------------------")
        end
    else
        if any(occursin.(_environment, string.(env_list)))
            if splitpath(Base.active_project())[end-1] != _environment
                cd(env_dir)
                Pkg.activate(_environment)
                cd(curr_dir)
                println("current dir: ", pwd())
            else
                @info(string("Environment \"", _environment, "\" is already loaded!"))
            end
        else
            @warn(string("Julia Pkg-environment: \"", _environment, "\" not found!"))
            @info("Call function without argument to list available environments.")
        end
    end
    return env_list
end

The strange thing is, that the command:

Pkg.activate("MyEnvironment")

opens a new user defined environment, if it is not yet known in a subfolder of Pkg.envdir().
And establishes the two environment files Project.toml & Manifest.toml in a sub-directory
with the name of the user defined environment as soon as a package is added via: Pkg.add("PackageName").
But the next time I give the identical command: Pkg.activate("MyEnvironment")
Pkg wants to establish a new environment definition directory at the current directory pwd().
Is this a bug or a feature?

1 Like