JuliaIO / BSON.jl

Other
158 stars 39 forks source link

BSON fails with import as #108

Open cossio opened 2 years ago

cossio commented 2 years ago
using BSON
import LinearAlgebra as LA
e = LA.eigen(randn(5,5))
BSON.@save "temp.bson" e
BSON.@load "temp.bson" e # ERROR

Stacktrace:

ERROR: UndefVarError: LinearAlgebra not defined
Stacktrace:
  [1] (::BSON.var"#31#32")(m::Module, f::String)
    @ BSON ~/.julia/packages/BSON/N216E/src/extensions.jl:21
  [2] BottomRF
    @ ./reduce.jl:81 [inlined]
  [3] _foldl_impl(op::Base.BottomRF{BSON.var"#31#32"}, init::Module, itr::Vector{Any})
    @ Base ./reduce.jl:58
  [4] foldl_impl
    @ ./reduce.jl:48 [inlined]
  [5] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
  [6] _mapreduce_dim
    @ ./reducedim.jl:327 [inlined]
  [7] #mapreduce#725
    @ ./reducedim.jl:322 [inlined]
  [8] #reduce#727
    @ ./reducedim.jl:371 [inlined]
  [9] resolve(fs::Vector{Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/extensions.jl:21
 [10] (::BSON.var"#35#36")(d::Dict{Symbol, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/extensions.jl:64
 [11] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:80
 [12] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:93
 [13] (::BSON.var"#49#50")(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/extensions.jl:147
 [14] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:92
 [15] (::BSON.var"#19#22"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:86
 [16] applychildren!(f::BSON.var"#19#22"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/N216E/src/BSON.jl:21
 [17] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:86
 [18] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/N216E/src/read.jl:93
 [19] raise_recursive
    @ ~/.julia/packages/BSON/N216E/src/read.jl:103 [inlined]
 [20] load (repeats 2 times)
    @ ~/.julia/packages/BSON/N216E/src/read.jl:108 [inlined]
 [21] top-level scope
    @ ~/.julia/packages/BSON/N216E/src/BSON.jl:52
cossio commented 2 years ago

A workaround is to (also) import the module with its original name.

using BSON
import LinearAlgebra as LA
import LinearAlgebra # we can still refer to LinearAlgebra as LA
e = LA.eigen(randn(5,5))
BSON.@save "temp.bson" e
BSON.@load "temp.bson" e # now works
darsnack commented 2 years ago

In the end, I think we'll want to use LinearAlgebra, etc. so that the BSON file can be loaded by someone else in a totally different environment (unless we figure out a way to resolve both). But maybe we can detect this situation and print out a warning on save?

cossio commented 2 years ago

If I do this:

using BSON
import LinearAlgebra as LA
e = LA.eigen(randn(5,5))
BSON.@save "temp.bson" e

And then, on a fresh Julia session:

using BSON
import LinearAlgebra
BSON.@load "temp.bson" e 

This works fine. So on save BSON is using LinearAlgebra to identify the module which I think is alright, since that's the explicit name.

Now, if I try loading this file from a session which has import LinearAlgebra as LA, I get the error. But can't it see that LA is the same as LinearAlgebra? Is there a Pkg function or something to get the full name of a module / package?

cossio commented 2 years ago
julia> import LinearAlgebra as LA
julia> fullname(LA)
(:LinearAlgebra,)

I think we can use that, right?

cossio commented 2 years ago

If someone can point out where the relevant code is I can take a look and try to put up a PR using fullname as above.