Another storing location
Arkoniak opened this issue · 1 comments
I am sorry in advance, perhaps I misunderstood some things. But from my experiments with this package (and some explanations given in zulip), it seems that it stores LocalPreferences.toml
roughly in ~/.julia/environments/v1.7/LocalPreferences.toml
(of course instead of v1.7
there can be any other julia version).
This looks somewhat inconvenient since packages version do not correlate with Julia version and the same config can be used between different Julia version. But in current implementation, each time user install new Julia version, he should move/recreate preferences again, despite the fact that packages configuration hasn't change at all.
To illustrate my concern, here is the possible scenario:
- I am using Julia 1.7 and install package Foo
- In Julia REPL I run
using Foo; set_prefs(foo = "bar")
whereset_prefs
is a function provided byFoo
package. - Now, in all of my sessions,
Foo
knows thatfoo = "bar"
. - I am installing Julia 1.8 and install package Foo.
- In Julia 1.8 REPL i am running
using Foo
and I can see that instead offoo = "bar"
i getfoo = "default"
. And this is kind of unexpected, because my settings of packageFoo
was not related to Julia version.
Of course one can recreate all settings, but if many packages uses this mechanism it can be quite a task.
Another option is to manually copy LocalPreferences.toml
between Julia versions, but it looks hackish and low level.
Instead, this file can be saved in ~/.julia/environments
or ~/.julia/prefs
and none of this problems appear.
P.S.: The way how I get this result:
- In directory "/tmp/1" I generated package with
] generate Foo
- In
Foo
directory I've added Preferences.jl withjulia --project=.
and] add Preferences
Foo
package was added in Julia environment with] dev Foo
- In src/Foo.jl added
function set_username(username::String)
@set_preferences!("username" => username)
end
- In "/tmp/1" ran
julia --project=.
and usedusing Foo; Foo.set_username("foobar")
LocalPreferences.toml
appeared in~/.julia/environments/v1.7
Preferences are stored on a per-project basis, so it's actually worse (or better) than you think:
When you run julia
with no --project
argument, you get the "default" project, which is stored in a per-version directory, as you have already discovered. This is intentional, as many packages may have a dependency on a single version, so you may want different versions or even different package sets for each version. However, we strongly encourage users to use a single project per logical chunk of work; e.g. your machine learning project may be one project, whereas your webserver project may be another.
In each of these projects, you can have different preferences applied. This is intentional, as although you may have Foo.jl
installed in both your machine learning project and your webserver project, they may each have very different needs, and may impose different preferences upon Foo.jl
. So we intentionally designed things to be as project-specific (and granular) as possible, for maximum flexibility.
I strongly suggest you embrace setting preferences on a per-project basis, however if you really want a global preference that is used for all Julia versions, you can add an environment to the end of Julia's load path in your startup.jl file, and provide "global defaults" that way:
First, create your "global defaults" directory:
$ mkdir ~/preferences_env
$ julia --project=~/preferences_env
pkg> add Preferences
pkg> add Example
julia> using Preferences, Example
set_preferences!(Example, "foo" => "bar")
This results in a Project.toml
and LocalPreferences.toml
that looks like the following:
$ cat Project.toml
[deps]
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
$ cat LocalPreferences.toml
[Example]
foo = "bar"
We don't strictly speaking need Preferences
added to this environment, I just added it so that I could use set_preferences!()
.
Next, in your startup.jl
, you can add the following:
push!(Base.LOAD_PATH, expanduser("~/preferences_env/Project.toml"))
Et voilà; you can now see that preference in whatever project you start Julia with:
mkdir -p /tmp/other_project
$ julia --project=/tmp/other_project
pkg> add Preferences
pkg> add Example
julia> using Preferences, Example
load_preference(Example, "foo")
"bar"