0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swift macrosのテストターゲットで"No such module ..."となったときの対処法

0
Posted at

Swift macrosのテストコードを書こうとしたのですが、No such module 'CocoonMacros'というエラーが発生時、ビルドできないという事象に当たりました。
実際に発生した時のテストコードは以下の通りです。

import CocoonMacros // ⇦ No such module 'CocoonMacros'
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest

final class ObjectMacroTest: XCTestCase {

    func testExample() throws {
        ...
    }
}

こういう場合は大体、テストターゲットの依存関係にCocoonMacrosを追加できていなかったりするので、Package.swift を確認してみましたが、特に問題はなさそうです。

// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/swiftlang/swift-syntax.git", from: "601.0.0"),
    ],
    targets: [
        .target(
            name: "CocoonMacro",
            dependencies: [
                "CocoonMacros"
            ]
        ),
        .macro(
            name: "CocoonMacros",
            dependencies: [
                .product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
                .product(name: "SwiftCompilerPlugin", package: "swift-syntax")
            ]
        ),
        .testTarget(
            name: "CocoonMacroTests",
            dependencies: [
                .product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
                .product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
                "CocoonMacros"
            ]
        )
    ]
)

SPM のキャッシュが変に残っていたりすることもあるので、Reset Package Cacheを実行してみたりしましたが効果なしでした。

結論

destination を"My Mac"にするとビルドが通るようになりました。

もともとテスト実行先には"iPhone 16(iOS 18.4)"を指定していました。
image.png

以下の様に"My Mac"を指定すると、ビルドが通り、単体テストが実行できる様になりました。
image.png

macroターゲットはiOSには対応しないのでしょうかね。
以下記事では、Swift macrosはコンパイル時に実行されるため、テストでも実行時と同様にmacOSで実行されるべきと議論されていました。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?