Automate the expansion of Swift macros

I'm troubleshooting indexing issues in a Swift project I'm working on: Xcode 16.1: Swift macro killing in… | Apple Developer Forums

tl;dr the project has several dozen targets depending on a macro, and Xcode spends a lot of time indexing SwiftParser, SwiftDiagnostics, SwiftOperators. It does this for every target which adds up to hours.

I suspect the issue is caused by macros, so I'd like to try to expand all macros, replace them with the generated code, remove the dependency, and see if this fixes the issue. Doing this manually will probably take days (there are around 250 instances in the project), so I was wondering if there's a way to automate this. I know there's -Xfrontend -dump-macro-expansions but afaik I will still need to copy and paste the expanded code manually.

I also thought about checking out a version of the project prior to macro adoption, but it will require Xcode 15 which may not have this issue.

Any other suggestions?

1 Like

I had this same problem when working with macros and VS Code. I don't know of a way to automate expansion of macros, but I do have a suggestion:

Move the macro to its own repo. This solution fixed all IDE problems I was having, mainly indexing, compilation and auto-completion.

If it already is in its own repo, than I got nothing.

It's already in a separate repo, so it must be something else. Thanks for the suggestion anyway!

Can you share any code or the project where this happens (or the macro in question)?

I'm afraid I can't, it's all proprietary code.

That is unexpected. Since this sounds like an Xcode issue, could you file a feedback at https://feedbackassistant.apple.com and attach /tmp/indexbuild.txt after running xcindex-test as follows:

env DEVELOPER_DIR=/path/to/Xcode.app xcrun xcindex-test -project <project.xcodeproj> -continue-after-errors -derivedDataPath /tmp/indexdd -- create-build-description -- prepare -target <target> -destination 'generic/platform=iOS' -scheme <scheme> -- index-files -target <target> -- quit 2>&1 | tee /tmp/indexbuild.txt

Feel free to share the Feedback ID here and we’ll follow up with you there to figure out what’s causing this issue.

Thanks for the suggestion! I filed a feedback, here's the ID: FB16035141

I saw a bunch of errors in the log even though the target builds successfully in Xcode. Also, the command finished in ~3 minutes (which seems normal), while Xcode took ~3 hours to index the project, so I'm not sure I did everything correctly.

Thanks for the prompt feedback and including the index log in it. We’ll get back to you if we have any follow-up questions.

Running the command with -all-targets instead of a specific target takes ~3 hours, so that's probably what Xcode is doing.

I haven't figured out how to automate the expansion of macros but replacing the swift-syntax dependency in the macro package with GitHub - sjavora/swift-syntax-xcframeworks worked like a charm. Indexing time is down to just a couple of minutes, and my Xcode is no longer crippled, search, refactoring, etc. work again.