A bug in Xcode lldb debugger for swift

In some particular situation, Xcode debugger or lldb cli cannot correctly extract some value that returned by a async throw function and handled by guard let try?.

Here is the minimum example:

import Dispatch

func test() async throws -> [Int] {
  return [369]
}

let group = DispatchGroup()

group.enter()
let task = Task {
  guard let res = try? await test() else { return }
  print(res)
  group.leave()
}

group.wait()

If added a break point at print(res), the debugger cannot show the value of res.

Due to forum limitation, I cannot paste a screenshot here...

if use p res or po res at lldb cli, it shows:

(lldb) p res
error: expression failed to parse:
error: <EXPR>:3:1: error: cannot find 'res' in scope
res
^~~

(lldb) po res
error: expression failed to parse:
error: <EXPR>:3:1: error: cannot find 'res' in scope
res
^~~

If test() returns a dict, or a costom struct, the issue retains. But if returned a trivial value like Int, it acts normally.

Also, if remove the guard statement, make res a optional value(use let res = try? await test()), debugger can extract the value.

Above results are compiled and run in this environment:

  • Swift 5.6.1
  • Xcode 13.4.1 (13F100)
  • lldb-1316.0.9.46
  • macOS 12.4
  • x86_64 arch
Post not yet marked as solved Up vote post of yh0x13f Down vote post of yh0x13f
3.7k views

Replies

I also encounter this issue when debugging async code. In 99% cases I cannot print values using the po and I'm getting such message

po result.queue.count

error: expression failed to parse:

error: <EXPR>:8:1: error: cannot find 'result' in scope

result.queue.count

^~~~~~

This doesn't happen in synchronous code

XCode 14.1

I'm also facing this issue in Xcode 14.1

If I put a breakpoint inside any async function and try to po any value in the local scope of the function, I get the cannot find in scope message.

The workaround for now is to use the good old print() and print the values at run time.

I hope there's a solution for this.

You could try fr v yourVariable to print a frame variable description – at least that is mostly working in these cases.

But I only came here to note down this is still an issue in Xcode 14.2, and it becomes more frustrating than printing when I f.e. want an override like thread return myVariable to perform an early return with a different value, getting the same error message instead. 😕

Are you by any chance setting breakpoints inside of code that's in a framework? I just read somewhere that this is a known issue.

For me the code is in the app.

(lldb) po rv
error: expression failed to parse:
error: <EXPR>:3:1: error: cannot find 'rv' in scope
rv
^~
(lldb) fr v rv
(WhatSize.SideBarItemReducer.State) rv = <variable not available>

// the code I'm trying to debug
    let rv = SideBarItemReducer.State.init(item: item)
        .preserveUIState(sideBarItemState)

    return rv

What's not so funny is that rv has the proper data in the Variables View, but I love command line short cuts.

Not a show stopper but a huge annoyance. Xcode 14.3.1 (14E300c) Thanks.