Swift Macros with XcodeProj targets

Hi :wave:,

I'm trying to set up a project with a Swift Macro using Xcode projects and native targets. The targets graph is the following:

  • Framework target (product type: framework)
    • Depends on: Macro target (product type: static framework)
      • Depends on: Macro executable (product type: command line tool)

Because the target dependency between the macro target and the executable doesn't compile the executable transitively when the destination platform doesn't match the host's, macOS, I added a build phase script that copies the executable from $BUILD_DIR/$CONFIGURATION/MacroName to $BUILT_PRODUCTS_DIR/MacroName. Then the framework target is configured (via build settings) to pass the following flag to the Swift compiler:

$(BUILT_PRODUCTS_DIR)/MacroName#MacroName

The above compiles successfully. The Xcode build system is able to compile things in the right order and the Swift compiler is able to use the plugin executable to resolve the macro reference. However, the editor presents an odd behaviour.

If derived data is clean, it's not able to resolve the macro reference, which I expect. Once the project is compiled once, the editor no longer complains. However, this works fine only if the project configurations follow the default naming configuration, Debug and Release. If the configurations have other names, the compilation works but the editor doesn't resolve the references. It seems like Xcode's editor is only able to resolve Debug/Release configurations. Unfortunately I can't further investigate the issue because that's logic is not open source. If I'm wrong in this assumption, I'd appreciate any pointers to continue my investigation.

If you want to reproduce the issue, you can clone this repository, open FrameworkWithSwiftMacro.xcworkspace, and change the configuration of the Framework scheme from Debug to Development. You'll notice that the editor yields different behaviours in Framework.swift depending on the selected configuration.

1 Like

The Xcode workspace I work in has all configurations named 'debug-prod', 'release-prod', 'debug-qa', etc. When macros are used like they are in the linked example, the editor will always show build errors even though it builds successfully. However, they don't go away when a successful build finishes like in this example.

It is a frustrating way to work.