Met a weird error; UndefVarError

The debugger handles scope slightly wrong when you execute julia code in it, so if you defined obj_path_tmp in a more inner scope it is possible the debugger is showing it still being defined while it is not. Here is a MWE:

1|debug> st
In f() at REPL[11]:1
 1  function f()
 2      for i in 1:5
 3          a = 3
 4      end
>5      print(a)
 6  end

About to run: (print)(Main.a)
1|julia> a
3

1|debug> n
ERROR: UndefVarError: a not defined

One of the solutions is to put local obj_path_tmp in the same scope as you want to use it:

julia> function f()
           local a
           for i in 1:5
               a = 3
           end
           print(a)
       end
f (generic function with 1 method)

julia> f()
3
2 Likes