Debugging does not work in vscode for 1 line of code of Zygote

I have this code:

using Zygote

function test_gradient()
    return gradient(x -> 3x^2 + 2x + 1, 5)    
end

and I am trying to debug it by setting a breakpoint in vscode. I get the following error when reaching y, back = pullback(f, args…):

julia> @run test_gradient()

Error while running the debugger (consider adding a breakpoint for uncaught exceptions):
ERROR: MethodError: no method matching getindex(::Nothing, ::Int64)
Stacktrace:
 [1] stack_trace_request(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, state::VSCodeServer.DebugAdapter.DebuggerState, params::VSCodeServer.DebugAdapter.StackTraceArguments)
   @ VSCodeServer.DebugAdapter ~/.vscode/extensions/julialang.language-julia-1.38.2/scripts/packages/DebugAdapter/src/debugger_requests.jl:403
 [2] (::VSCodeServer.DebugAdapter.var"#128#154"{VSCodeServer.DebugAdapter.DebuggerState})(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, params::VSCodeServer.DebugAdapter.StackTraceArguments)
   @ VSCodeServer.DebugAdapter ~/.vscode/extensions/julialang.language-julia-1.38.2/scripts/packages/DebugAdapter/src/packagedef.jl:55
 [3] dispatch_msg(x::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeServer.JSONRPC.MsgDispatcher, msg::Dict{String, Any})
   @ VSCodeServer.JSONRPC ~/.vscode/extensions/julialang.language-julia-1.38.2/scripts/packages/JSONRPC/src/typed.jl:67
 [4] macro expansion
   @ ~/.vscode/extensions/julialang.language-julia-1.38.2/scripts/packages/DebugAdapter/src/packagedef.jl:76 [inlined]
 [5] (::VSCodeServer.DebugAdapter.var"#146#172"{VSCodeServer.var"#186#188", VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, VSCodeServer.JSONRPC.MsgDispatcher})()
   @ VSCodeServer.DebugAdapter ./task.jl:484

Zygote does a bunch of finicky things with the compiler that trip up the VS Code debugging. This means, unfortunately, that print debugging is probably the path of least resistance. If you are feeling up for learning the Debugger.jl CLI however (this is what VS Code uses under the hood), I’ve generally found it to be less error prone on Zygote code than the VS Code UI.

1 Like

Thanks for confirming, I had this suspicion after looking into the code and understanding more about macros.