I currently have a macro which works if I test it directly via assertMacroExpansion(), but if I try to actually use it simply gives the error "error: failed to receive result from plugin (from macro 'Foo ')" and no further information other than pointing at the line of code being expanded. Is there some way to debug this and attempt to determine what is going wrong, or do I just need to change things at random and hope it starts working?
error: failed to receive result from plugin (from macro 'Foo ')
I think it failed because your macro's
expansion() method crashed somehow. You could try copying the compiler command line invocation from the build log in Xcode, and execute it in the Terminal to see if something is emitted to the stderr from the plugin.
It's unfortunate that compiler diagnostics doesn't show much information, though. Please feel free to file an issue to Issues · apple/swift · GitHub
Ah, invoking the compiler directly from the command line does result in it printing the errors which occurred (which were some failed force unwraps in this case).
if you use the package layout of macro (
swift package init --type macro) you can debug the invocation of the macro in the tests; which makes tracking those types of things down considerably easier. Plus writing those gives you some neat unit tests for your macro .
This specific bug was in a conformance macro which currently isn't supported by macro tests (see [Macros] Implement expansion of conformance macros by DougGregor · Pull Request #1773 · apple/swift-syntax · GitHub).
assertMacroExpansion() does not seem like a viable way to write or test macros. It simply doesn't do the same thing as the compiler does, and a test passing is no guarantee that the macro will do the same thing when invoked on the exact same code by the compiler.
I have the same issue. Could you please tell how to invoke the compiler from the command line when using the Xcode Macro Package?