JuliaInterop / RCall.jl

Call R from Julia
Other
319 stars 59 forks source link

ERROR: Cannot set preferences of an unknown package that is not loaded! #551

Open schlichtanders opened 3 weeks ago

schlichtanders commented 3 weeks ago

Trying to define the preferences for RCall, I am running into an error. How to actually do this?

julia> import Pkg; Pkg.add(["UUIDs", "Preferences", "CondaPkg"])

julia> using UUIDs, Preferences, CondaPkg

julia> envdir = CondaPkg.envdir()

julia> RCALL_UUID = UUID("6f49c342-dc21-5d91-9882-a32aef131414")
UUID("6f49c342-dc21-5d91-9882-a32aef131414")

julia> set_preferences!(RCALL_UUID, "Rhome" => "$envdir/lib/R", "libR" => "$envdir/lib/R/lib/libR.so")
ERROR: Cannot set preferences of an unknown package that is not loaded!
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] set_preferences!(::UUID, ::Pair{String, String}, ::Vararg{Pair{String, String}}; export_prefs::Bool, active_project_only::Bool, kwargs::@Kwargs{})
   @ Preferences ~/.julia/packages/Preferences/enLcN/src/Preferences.jl:281
 [3] set_preferences!(::UUID, ::Pair{String, String}, ::Pair{String, String})
   @ Preferences ~/.julia/packages/Preferences/enLcN/src/Preferences.jl:223
 [4] top-level scope
   @ REPL[8]:1
schlichtanders commented 3 weeks ago

It seems the only way to actually fix this is to first add the RCall package (which will trigger precompilation of RCall) and then add the preferences like above.

I tried writing the LocalPreferences.toml directly, but it won't actually work, has_preferences will simply show false as long as RCall is not a top-level dependency, even if the preferences are set inside LocalPreferences.toml

palday commented 3 weeks ago

I think this setup is also desirable behavior -- I think it makes sense to require direct dependency for something you care enough about the details of to set a preference on.

schlichtanders commented 2 weeks ago

The confusing part is on reading the preferences. Despite having LocalPreferences.toml filled with an entry for RCall, you will get the following divergent behaviour between UUID and package-name, if RCall is not added as a toplevel dependency.

julia> using Preferences, UUIDs

julia> RCALL_UUID = UUID("6f49c342-dc21-5d91-9882-a32aef131414")

julia> has_preference(RCALL_UUID, "Rhome")
false

julia> has_preference("RCall", "Rhome")
ERROR: ArgumentError: Cannot resolve package 'RCall' in load path; have you added the package as a top-level dependency?
Stacktrace:
 [1] package_lookup_error(name::String)
   @ Preferences ~/.julia/packages/Preferences/enLcN/src/utils.jl:88
 [2] has_preference(name::String, key::String)
   @ Preferences ~/.julia/packages/Preferences/enLcN/src/Preferences.jl:76
 [3] top-level scope
   @ REPL[8]:1
frankier commented 2 weeks ago

This might be relevant/helpful: https://github.com/JuliaPackaging/Preferences.jl/issues/53