Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another storing location #30

Closed
Arkoniak opened this issue Feb 23, 2022 · 1 comment
Closed

Another storing location #30

Arkoniak opened this issue Feb 23, 2022 · 1 comment

Comments

@Arkoniak
Copy link

Arkoniak commented Feb 23, 2022

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:

  1. I am using Julia 1.7 and install package Foo
  2. In Julia REPL I run using Foo; set_prefs(foo = "bar") where set_prefs is a function provided by Foo package.
  3. Now, in all of my sessions, Foo knows that foo = "bar".
  4. I am installing Julia 1.8 and install package Foo.
  5. In Julia 1.8 REPL i am running using Foo and I can see that instead of foo = "bar" i get foo = "default". And this is kind of unexpected, because my settings of package Foo 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:

  1. In directory "/tmp/1" I generated package with ] generate Foo
  2. In Foo directory I've added Preferences.jl with julia --project=. and ] add Preferences
  3. Foo package was added in Julia environment with ] dev Foo
  4. In src/Foo.jl added
function set_username(username::String)
    @set_preferences!("username" => username)
end
  1. In "/tmp/1" ran julia --project=. and used using Foo; Foo.set_username("foobar")
  2. LocalPreferences.toml appeared in ~/.julia/environments/v1.7
@staticfloat
Copy link
Member

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants