Weird UndefVarError in Julia 1.3 using HDF5.jl

Hello, consider the following MWE:

using HDF5

u = rand(2, 3)
h5open("foo.h5", "w") do f # write some data in `foo`
    du = similar(u)
    d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))
end

A = Vector{Int}(1:10) # write some data in `bar`
h5write("bar.h5", "bar", A)
# will error
h5writeattr("bar.h5", "bar", Dict("c"=>"value for metadata parameter c", "d"=>"metadata d"))
@show h5readattr("bar.h5", "bar")

rm("bar.h5"; force=true)
rm("foo.h5"; force=true)

This will show error on attempting h5writeattr:

ERROR: UndefVarError: S not defined
Stacktrace:
 [1] datatype(::S<:String) at /home/pshi/Documents/julia-1.3.0-mkl/packages/HDF5/Y9Znv/src/HDF5.jl:1178

However, in the original code the S is well-defined:
https://github.com/JuliaIO/HDF5.jl/blob/9fe2e927ba1e22b27546d1392ac3e81a77719d51/src/HDF5.jl#L1177-L1182

On the contrary, if you put the data creation in foo after calling h5writeattr, everything works perfectly (please restart a new Julia repl):

using HDF5

u = rand(2, 3)

A = Vector{Int}(1:10) # write some data in `bar`
h5write("bar.h5", "bar", A)
h5writeattr("bar.h5", "bar", Dict("c"=>"value for metadata parameter c", "d"=>"metadata d"))

h5open("foo.h5", "w") do f # write some data in `foo`
    du = similar(u)
    d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))
end

h5writeattr("bar.h5", "bar", Dict("e" => "more metadata"))
# will show all saved meta data
@show h5readattr("bar.h5", "bar")

rm("bar.h5"; force=true)
rm("foo.h5"; force=true)

Moreover, if I make either of the two changes, hard-coding data type or not using similar array, the problem will disappear:

d_create(f, "u", datatype(Float64), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))

or

du = rand(2, 3)
d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))

I am confused about this emitted error which does not make sense. Also it only happens on Julia 1.3 or later, not before. I encounter this problem when wrapping h5writeattr in my own function.

Thanks in advance!

Feels like a bug to me. Please open an issue.

1 Like

Thanks for the reply, here is the issue.