Macro usage experience

Hello,

While I have enjoyed writing macros for my library, I think that the onboarding experience for my users leaves a bit to be desired. Currently my users need to both mess with their dependencies to take a dependency on the macro package, and then use a separate import directive to use the macros.

I have three components: SwiftGodot (my library), SwiftGodotMacros (the library that contains the macro definitions) and SwiftGodotMacroLibrary (the .macro) target that implements the macros.

My users had to change code that looked like this:

        .target(
            name: "MyLib",
	    dependencies: ["SwiftGodot"])

to:

        .target(
            name: "MyLib",
            dependencies: ["SwiftGodot", .product (name: "SwiftGodotMacros", package: "SwiftGodot")],

Which is a little bit more sausage making than I would like to expose.

The other problem is that my users need to add another import in addition to "SwiftGodot", they need to manually add "import SwiftGodotMacros".

There has to be a better way. In an ideal world, I would like "SwiftGodot" to be the sole dependency that users are exposed to, and the sole import that they need to take.

I did try one stunt to attempt that, but it exposed a build anomaly.

I figured "Let me put the Macro definitions into my main library and kill entirely the SwiftGodotMacros library", so I moved my macro definitions into SwiftGodot and killed the SwiftGodotMacros definition library. No complains from Swift build, everything is fine.

But to reduce the burden on my users on Package.swift, I also did this: I made my "SwiftGodot" target depend on the "SwiftGodotMacrosLibrary" target, and for my local tests this worked fine.

This is where the anomaly comes up.

If I reference my SwiftGodot dependency with this:

.package(url: "https://github.com/migueldeicaza/SwiftGodot", branch: "wip")

The build fails with a "unknown attribute 'Godot'" error.

but if I replace it with:

.package (path: "/Users/miguel/cvs/SwiftGodot")

It works.

At the time of this test both the local copy and the remote copy were identical.

Would love some guidance on how to improve the experience for the end users.

If you want to follow at home, you can get this sample with the cumbersome approach, and then replace the target as outlined above and make it point to a local check-out using the "wip" branch:

https://tirania.org/tmp/sample-godot.tar.gz

8 Likes