Xcode Cloud has no write permission when running Swift Package Plugin

I have this Plugin

struct Main: BuildToolPlugin {
  func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
    let inputFolder = target.directory.appending("Image")
    let output = context.pluginWorkDirectory.appending("GeneratedImageAssets.swift")
    return [
      .buildCommand(
        displayName: "Running ImageAssets parser",
        executable: try context.tool(named: "ImageAssetsParser").path,
        arguments: [inputFolder.string, output.string],
        environment: [:],
        inputFiles: [inputFolder],
        outputFiles: [output]
      )
    ]
  }
}

ImageAssetsParser is an executableTarget in swift code that scan folders and write them in the output folder sent by the plugin.

Although locally it works everything, on Xcode Cloud I got permission error:

The weird things is that I have also a prebuildCommand plugin that uses swiftgen, as an artifactbundle and it works properly, it writes in same folder.

Am I missing something?

5 Likes

After months, by chance I found what was the issue!

In my plugin implementation I write the file through

"""
// auto generated swift file

// ... the content
""".write(to: output, atomically: true, encoding: .utf8)

Using atomically: true is the issue: this means that swift writes the entire content into a temporary file placed somewhere else and Xcode Cloud doesn't have permission in this moment!

Using atomically: false the file is written directly into our output file, which is the context.pluginWorkDirectory.appending("GeneratedImageAssets.swift") and permissions here are granted!!

2 Likes