Xcode 13.3 beta 3: Configuration of a BuildToolPlugin

Hey!

I am trying to create an R.swift BuildToolPlugin with the help of SE0325, SE0332 and SE0303.
Unfortunately I end up getting the following error:

Failed to run build tool plugins: plugin process ended by an uncaught signal: 4 <command: /usr/bin/sandbox-exec -p '(version 1)

Showing Recent Messages
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow file-write*
(subpath "/private/tmp")
(subpath "/private/var/folders/rn/y9q4wz7n5fg84ymy0rxsxjv80000gn/T")
)
(deny file-write*
(subpath "/Users/user/Developer/myProject-ios/app/myProject/Components")
)

(allow file-write*
(subpath "/Users/user/Library/Developer/Xcode/DerivedData/myProject-apioztmvvdcrptgqqpavjxfgqryl/SourcePackages")
(subpath "/Users/user/Library/Developer/Xcode/DerivedData/myProject-apioztmvvdcrptgqqpavjxfgqryl/SourcePackages/plugins")
)

' /Users/user/Library/Developer/Xcode/DerivedData/myProject-apioztmvvdcrptgqqpavjxfgqryl/SourcePackages/plugins/RSwiftPlugin>, <output:

'Swift/ErrorType.swift:200: Fatal error: Error raised at top level: Swift.DecodingError.typeMismatch(PackagePlugin.WireInput.Target.TargetInfo.BinaryArtifactKind, Swift.DecodingError.Context(codingPath: [PerformActionCodingKeys(stringValue: "input", intValue: nil), CodingKeys(stringValue: "targets", intValue: nil), _JSONKey(stringValue: "Index 14", intValue: 14), CodingKeys(stringValue: "info", intValue: nil), BinaryArtifactInfoCodingKeys(stringValue: "kind", intValue: nil)], debugDescription: "Invalid number of keys found, expected one.", underlyingError: nil))

I think I made a mistake in the config, but I can't pinpoint it. Trying to interpret the error message, I would say I messed up the binary target configuration (I just downloaded the rswift binary and zipped it).

My project structure looks like this:

Components
 ├ Package.swift
 ├ Plugins
    └ RSwiftPlugin
       ├ gen
       └ RSwiftPlugin.swift
 └ Sources
    ├ Rswift
       └ Rswift.zip
    └ Assets

RSwiftPlugin:

import PackagePlugin

@main struct RSwiftPlugin: BuildToolPlugin {

  func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
    let outputPath = context.pluginWorkDirectory.appending("gen")
    let outputFilePath = outputPath.appending("R.generated.swift")

    print("outputFilePath is: \(outputPath)")
    return [
      .prebuildCommand(
        displayName: "running rswift",
        executable: try context.tool(named: "rswift").path,
        arguments: [
          "generate",
          "--accessLevel", "public",
          "\(outputFilePath)"],
        environment: [
          "PROJECT_DIR": "\(context.package.directory)",
          "TARGET_NAME": "\(target.name)"
        ],
          outputFilesDirectory: outputPath
        )
    ]
  }
}

Package.swift:

import PackageDescription

let package = Package(
    name: "Components",
    platforms: [.iOS(.v14), .macOS(.v10_15)],
    products: [
      .library(name: "Assets", targets: ["Assets"]),
      .plugin(name: "RSwiftPlugin", targets: ["RSwiftPlugin"])
    ],
    dependencies: [
      .package(url: "https://github.com/mac-cain13/R.swift.Library", from: "5.2.0")
    ],
    targets: [
      .binaryTarget(name: "rswift", path: "Sources/Rswift/rswift.zip"),
      .plugin(
        name: "RSwiftPlugin",
        capability: .buildTool()
      ),
      .target(
        name: "Assets",
        dependencies: [.product(name: "Rswift", package: "R.swift.Library")],
        resources: [.process("Files")],
        plugins: [.plugin(name: "RSwiftPlugin")])
    ]
)

Does anyone have an idea? Any help would be great. Thank you very much and have a nice day, :wave:t2:

Arsatius

2 Likes

Hello Arsatius, thanks a bunch for the detailed description of the plugin. I don't see anything offhand that is wrong, and in any case I would expect an error message rather than just the signal 4. I'll try this out and see what I can find.

One thing I notice is that the binary target is in the form of a .zip even in the local package, which I think should work but is more unusual. The example at GitHub - abertelrud/swiftpm-buildtool-plugin-examples: Some examples of SwiftPM build tool plugins to go along with SE-0303 + SE-0325 has the SwiftGen binary directory (not zipped) in the package. It shouldn't make a difference though, but is something I noticed.

I'll reply again after taking a closer look.

Hey Anders,

I'll check out your repo, adjust my code and see whats coming up then. :smiley:
Anyways thank you very much for you help!

Hey! I just wanted to report back. Based on your sample projects I dropped R.swift and switched to SwiftGen. (Imo nicer because you just need one dependency).
Thank you and all the best,

Arsatius

1 Like

Thanks for the reply, and glad it's working for you. I do still want figure out whether the original issue you ran into is a bug, but haven't yet had found time to try it out. I'll reply back once I've tried it out. Thanks!