Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the solution for "error: Couldn't IRGen expression, no additional error" on Xcode 10.1?

We have a large project with a lot of dependencies brought in through Carthage. Whenever we try to look at a variable in the lldb debugger (p variablename), it gives us an error: error: Couldn't IRGen expression, no additional error

None of the workarounds are very good. We can run carthage with --no-use-binaries to get around it, but it makes builds take a prohibitively long time. We can use fr v on some variables, but not all. People have fixed this in previous versions of Xcode by changing permissions on some Swift directories, but I can't find the corresponding directories in Xcode 10.1. And I saw someone say that changing the build system back and forth helped him, but that hasn't worked for us.

So I'm starting this search for a solution on Xcode 10.1 specifically. Has anyone else found out what's causing this error, and/or a good solution to it?

like image 830
Brian Kendig Avatar asked Feb 19 '19 23:02

Brian Kendig


3 Answers

Someone on my team shared a solution to this that actually works (I don't know if he discovered it or found it elsewhere):

Set a breakpoint on the first line of the AppDelegate didFinishLaunchingWithOptions method. Set the action for this breakpoint to: po application

Now, when your run your application, the debugger will pause at that breakpoint and will display this text in the lldb debugger pane (with your app name instead of Foo):

note: Swift compiler options for Foo conflict with options found in other modules; Switching to a new expression evaluator for Foo, old $R variables are lost.

And then the lldb debugger will work properly, able to p and po variables and expr expressions.

I don't know why it works, but it works, and reliably too!

like image 190
Brian Kendig Avatar answered Oct 17 '22 22:10

Brian Kendig


A colleague and me found a solution:

Create an Objective-C file in your Swift project. Hit yes when it asks for the Bridging Header.

Test.h

#import <Foundation/Foundation.h>

@interface Test : NSObject

- (id)init;

@end

Test.m

#include "Test.h"

@implementation Test

- (id)init
{
    return self;
}

@end

MyProject-Bridging-Header.h

#include "Test.h"

And now: 🎉 no more error: Couldn't IRGen expression, no additional error and you can debug again.

So I think just adding a bridging header works as a workaround...

But If that doesn't work:

Add a breakpoint in AppDelegate.swift in application: UIApplication, didFinishLaunching like so and hope that this helps:

Breakpoint in Xcode

like image 33
Nico S. Avatar answered Oct 17 '22 20:10

Nico S.


There is currently a hard requirement that the version of the swift compiler you use to build your source and the version of lldb you use to debug it must come from the same toolchain. Currently the swift debug information for types is just a serialization of internal swift compiler data structures. It also depends on local path information, which makes it hard to move around.

There is a longer term effort to change that design, but for now you have to rebuild all your binaries every time you update your tools, and you can't use pre-built binaries.

I'm a little surprised this causes a day-to-day problem, however. This full rebuild only needs to happen when you pull new sources from Carthage or update your tools, which shouldn't be that often. If you are triggering rebuilds more frequently than that, maybe dependencies aren't getting tracked properly, so that more files are getting rebuilt than need to be?

like image 32
Jim Ingham Avatar answered Oct 17 '22 20:10

Jim Ingham