Command plugin dependencies wrong when macro target present

Building a package using a SPM command plugin (for embedded), and I'm trying to add macro support to this build process.

When I add a macro target as a dependency of the product's target, the command plugin package description only gives the macro targets dependencies, and no longer lists the products dependencies (not even the macro target)

I want to manually build the macro compiler plugin and pass it to swiftc while compiling the product. But I can't build the product at all because the dependencies are no longer correct.

How can I get the product targets real dependencies when a macro is also a dependency? This is unexpected and doesn't make sense, could this be a bug?

@Max_Desiatov

I don't think this is currently (or ever would be) supported by SwiftPM.

Couldn't I just compile the macro target to an executable with swiftc? I couldn't find a makefile example of using macros but that's essentially what I wanna do in the plugin

Sure, but swiftc invocations are implementation details of SwiftPM's build process that can change without any warning and you shouldn't rely on them going in particular sequence, writing to specific paths, or running with certain flags and options.

You either

  1. describe your build with Package.swift and call swift build from CLI directly, or the predefined packageManager.build API that is essentially equivalent to swift build under the hood, but doesn't interfere with the ongoing build process that drives a plugin, calling swiftc manually in this case is an undefined behavior;
  2. or drive the build process yourself with swiftc invocations and possibly your favourite build system (shell scripts, CMake, GNU Make, BSD Make, Bazel, Buck etc), but don't invoke SwiftPM simultaneously so that it doesn't interfere with your manual build process.

Oh, yes. We're doing both. We're letting SPM validate the build as a regular package and using a command package plugin to compile the package for an embedded device. It works great so far.

The macro works as expected with the package and swift build, but for the plugin we're using the package description provided to the plugin to build for the device.

But when I add a macro target to the build product the package description no longer provides the correct dependencies when using sourceModule.dependecies or sourceModule.recursiveTargetDependencies. It provides the macro target's dependencies (SwiftSyntax) and none of the build products dependencies.

My plan was to grab the macro target by its type, build it, then pass it to swiftc with -load-plugin-executable while building the build product. But I can't get the build products dependencies at all when the macro target is a dependency.

Without the macro target:
dependencies: ["CPlaydate", "SwiftUnicodeDataTables"],

Modules for PlaydateKitTemplate: ["CPlaydate", "SwiftUnicodeDataTables", "PlaydateKit"]

With the macro target:
dependencies: ["CPlaydate", "SwiftUnicodeDataTables", "PlaydateKitMacros"],

Modules for PlaydateKitTemplate: ["_SwiftSyntaxCShims", "SwiftSyntax509", "SwiftSyntax510", "SwiftSyntax600", "SwiftSyntax", "SwiftDiagnostics", "SwiftParser", "SwiftBasicFormat", "SwiftParserDiagnostics", "SwiftSyntaxBuilder", "SwiftSyntaxMacros"]