Pluto.jl: [Bug] Loading BSON variable with unit

Created on 17 Aug 2020  ยท  7Comments  ยท  Source: fonsp/Pluto.jl

Here is a MWE Pluto notebook

### A Pluto.jl notebook ###
# v0.11.6

using Markdown
using InteractiveUtils

# โ•”โ•โ•ก 665ea77a-e065-11ea-00f6-4bd4c96dd01e
using Unitful, BSON

# โ•”โ•โ•ก 343a3f00-e067-11ea-0087-578bfebddeb2
ufoo = 3.0u"m/s"

# โ•”โ•โ•ก 4725579e-e067-11ea-0fee-dbf660902683
BSON.@save "test.bson" ufoo

# โ•”โ•โ•ก 4f79588c-e067-11ea-13eb-3bf5069dbf64
function get_ufoo()
    BSON.@load "test.bson" ufoo
    return ufoo
end

# โ•”โ•โ•ก 5a2ccdea-e067-11ea-0169-712307d8649d
ubaz = get_ufoo() # This throws with "Unitful not defined"

# โ•”โ•โ•ก Cell order:
# โ• โ•665ea77a-e065-11ea-00f6-4bd4c96dd01e
# โ• โ•343a3f00-e067-11ea-0087-578bfebddeb2
# โ• โ•4725579e-e067-11ea-0fee-dbf660902683
# โ• โ•4f79588c-e067-11ea-13eb-3bf5069dbf64
# โ• โ•5a2ccdea-e067-11ea-0169-712307d8649d

that throws

UndefVarError: Unitful not defined

    (::BSON.var"#29#30")(::Module, ::String)@extensions.jl:20
    [email protected]:81[inlined]
    _foldl_impl(::Base.BottomRF{BSON.var"#29#30"}, ::Module, ::Array{Any,1})@reduce.jl:58
    [email protected]:48[inlined]
    mapfoldl_impl(::typeof(identity), ::BSON.var"#29#30", ::NamedTuple{(:init,),Tuple{Module}}, ::Array{Any,1})@reduce.jl:44
    #mapfoldl#204(::Base.Iterators.Pairs{Symbol,Module,Tuple{Symbol},NamedTuple{(:init,),Tuple{Module}}}, ::typeof(mapfoldl), ::Function, ::Function, ::Array{Any,1})@reduce.jl:160
    [email protected]:315[inlined]
    #mapreduce#[email protected]:310[inlined]
    #reduce#[email protected]:359[inlined]
    resolve(::Array{Any,1})@extensions.jl:20
    (::BSON.var"#33#34")(::Dict{Symbol,Any})@extensions.jl:53
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#21#22"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:94
    applychildren!(::BSON.var"#21#22"{IdDict{Any,Any}}, ::Array{Any,1})@BSON.jl:28
    [email protected]:94[inlined]
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Array{Any,1})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any})@extensions.jl:138
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:88
    (::BSON.var"#21#22"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:94
    applychildren!(::BSON.var"#21#22"{IdDict{Any,Any}}, ::Array{Any,1})@BSON.jl:28
    [email protected]:94[inlined]
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Array{Any,1})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any})@extensions.jl:138
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:88
    (::BSON.var"#18#20"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:82
    applychildren!(::BSON.var"#18#20"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:82
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    [email protected]:99[inlined]
    [email protected]:104[inlined]
    macro [email protected]:52[inlined]
    get_ufoo()@Other: 2
    top-level scope@Local: 1

I'm not wure why, since this works perfectly outside of Pluto, and it also works when using BSON but not Unitful... What's happening here? (FWIW, 'm trying to use Pluto with some data I have stored in BSON files with units.)

other packages

All 7 comments

Could you try this and see if the error is still there?

### A Pluto.jl notebook ###
# v0.11.6

using Markdown
using InteractiveUtils

# โ•”โ•โ•ก 665ea77a-e065-11ea-00f6-4bd4c96dd01e
begin
    using Unitful, BSON
    Core.eval(Main, :(using Unitful))
end

# โ•”โ•โ•ก 343a3f00-e067-11ea-0087-578bfebddeb2
ufoo = 3.0u"m/s"

# โ•”โ•โ•ก 4725579e-e067-11ea-0fee-dbf660902683
BSON.@save "test.bson" ufoo

# โ•”โ•โ•ก 4f79588c-e067-11ea-13eb-3bf5069dbf64
function get_ufoo()
    BSON.@load "test.bson" ufoo
    return ufoo
end

# โ•”โ•โ•ก 5a2ccdea-e067-11ea-0169-712307d8649d
ubaz = get_ufoo() # This throws with "Unitful not defined"

# โ•”โ•โ•ก Cell order:
# โ• โ•665ea77a-e065-11ea-00f6-4bd4c96dd01e
# โ• โ•343a3f00-e067-11ea-0087-578bfebddeb2
# โ• โ•4725579e-e067-11ea-0fee-dbf660902683
# โ• โ•4f79588c-e067-11ea-13eb-3bf5069dbf64
# โ• โ•5a2ccdea-e067-11ea-0169-712307d8649d

It looks like BSON.jl has the limitation that it needs packages to be defined in the Main scope - it does not work inside modules when using external packages:

https://gist.github.com/fonsp/caef46b11c417014d0d1698b6b70607f#file-bsonunitful-jl

https://gist.github.com/fonsp/caef46b11c417014d0d1698b6b70607f#file-bsonunitful2-jl

๐ŸŸ fonsv/Desktop ยป julia bsonunitful.jl
ubaz = 3.0 m s^-1
๐ŸŸ fonsv/Desktop ยป julia bsonunitful2.jl
ERROR: LoadError: UndefVarError: Unitful not defined
Stacktrace:
 ...

Pluto runs your code inside 'workspace modules' so the line using Unitful, BSON is never executed in Main (only in Main.workspace42, Main.workspace43, etc).

I don't know how BSON works, but maybe it can be fixed by running the extracting code at the place where BSON.@load is called, instead of running it inside Main.

Thanks @fonsp! Maybe it's worth mentioning that this can happen in the FAQ? (And include the Core.eval(Main, :(using MyPackage)) workaround?)

No I think that this issue is a good enough resource on its own.

But maybe I should start making a big list of packages and their workarounds...

Another option is to detect that a goofy package is being used, and show the link to the workaround inside the editor

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dpsanders picture dpsanders  ยท  6Comments

mlg556 picture mlg556  ยท  4Comments

karlwessel picture karlwessel  ยท  4Comments

briochemc picture briochemc  ยท  5Comments

kapple19 picture kapple19  ยท  5Comments