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

JLD2 doesn't recognise types during Documenter builds #328

Closed
AayushSabharwal opened this issue Jun 15, 2021 · 6 comments
Closed

JLD2 doesn't recognise types during Documenter builds #328

AayushSabharwal opened this issue Jun 15, 2021 · 6 comments

Comments

@AayushSabharwal
Copy link

Hi! I'm trying to generate documentation, some of which uses JLD2. If I define a struct, and @save it, it works. However, when loading back in, JLD2 doesn't recognise the struct type even though it's in the same module. This only occurs during Documenter builds. Consider the following mockup:

mockup.jl file
using Agents
using JLD2

struct FooAgent <: AbstractAgent
    id::Int
    pos::Tuple{Int,Int}
    foo::String
end

# This is some markdown stuff

agents = [FooAgent(i, (i, i), "$i") for i in 1:10]

# This is some more markdown stuff

@save "test.jld2" agents

# Now that something is saved

agents = nothing
@load "test.jld2" agents

# Did it load properly?

println(agents)
println(typeof(agents))
println(typeof(agents[1]))

# Hmmm

Using Literate.markdown("mockup.jl")

mockup.md
 ```@meta
EditURL = "<unknown>/src/mockup.jl"
```   

```@example mockup
using Agents
using JLD2

struct FooAgent <: AbstractAgent
    id::Int
    pos::Tuple{Int,Int}
    foo::String
end
```

This is some markdown stuff

```@example mockup
agents = [FooAgent(i, (i, i), "$i") for i in 1:10]
```

This is some more markdown stuff

```@example mockup
@save "test.jld2" agents
```

Now that something is saved

```@example mockup
agents = nothing
@load "test.jld2" agents
```

Did it load properly?

```@example mockup
println(agents)
println(typeof(agents))
println(typeof(agents[1]))
```

Hmmm

---

*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*

After building as a Documenter page:
image

The type is reconstructed even though it was defined right there.

@JonasIsensee
Copy link
Collaborator

Hi @AayushSabharwal

JLD2 usually throws a warning when it cannot reconstruct a struct. Can you please check the stderr (e.g. CI log ?)
for warnings?
Do you get the same behaviour, when you use FileIO.load ?

@AayushSabharwal
Copy link
Author

There aren't any warnings in the CI log about this. The only time it complains is when I try to use the data, since it's not compatible with other functions and data structures.

as for FileIO.load, there isn't any change:
image

@JonasIsensee
Copy link
Collaborator

image

This is odd and is not a problem with JLD2. There is something funky going on with the modules the code is being evaluated into

@Libbum
Copy link

Libbum commented Jun 15, 2021

Hmm, indeed. Looks like that module is coming from documenter.

@JonasIsensee
Copy link
Collaborator

JonasIsensee commented Jun 15, 2021

It should be possible to fool JLD2 , though.
Defining

julia> module Foo
       struct Bar
       x
       end
       @eval Main Baz = $(@__MODULE__)
       end
Main.Foo

julia> Baz
Main.Foo

julia> Baz.Bar
Main.Foo.Bar

So in this case

@eval Main __atexample__named__mockup = $(@__MODULE__) #hide

@AayushSabharwal
Copy link
Author

This fixes the docs build. Thanks!

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

3 participants