Command SwiftEmitModule failed with a nonzero exit code

After a refactoring, my project doesn't run anymore, but if I transfer the code from the library to the project, the project can compile normally. Spent a lot of time tracking down the problem and finally found the problem.

For this, I created a test project to reproduce the issue. This project has two Packages, DataMoodule and ProtocolModule, and two Apps, TestCrossModule and TestWholeModule. TestCrossModule is used to reproduce compilation errors. TestWholeModule is a project that can be compiled (same code as TestCrossModule, but there are no modules)

DataModule contains the following code:

// Protocol.swift
public protocol Anchor: CALayer {
    associatedtype P: AnchorPath where P.A == Self
    
    var path: P { get }
}

public protocol AnchorPath: CALayer {
    associatedtype A: Anchor where A.P == Self
    
    var anchors: [A] { get set }
}
// Implementation.swift
import Foundation
import UIKit

public class AnchorLayer<P: AnchorPath>: CALayer {
    public unowned let path: P
    
    public var isActive: Bool = false
    public var x: Double {
        get { position.x }
        set { position.x = newValue }
    }
    public var y: Double {
        get { position.y }
        set { position.y = newValue }
    }
    
    public init(path: P) {
        self.path = path
        super.init()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

public class AnchorPathLayer<A: Anchor>: CALayer {
    public var anchors: [A] = []
}

public final class CVAnchor: AnchorLayer<CVPath>, Anchor {
    
}

public final class CVPath: AnchorPathLayer<CVAnchor>, AnchorPath {
    
}

ProtocolModule contains the following code:

import Foundation

public protocol TableProtocol: AnyObject {
    associatedtype Row: TableRowProtocol
    associatedtype Column: TableColummProtocol where Column.RowContent == Self.Row.Value
    var name: String? { get }
    var rows: [Row] { get }
    var columns: [Column] { get }
}

public protocol TableRowProtocol: AnyObject {
    associatedtype Value
    var name: String { get }
    var data: Value { get set }
}

public protocol TableColummProtocol {
    associatedtype RowContent
    var name: String { get }
    var keyPath: WritableKeyPath<RowContent, Double> { get }
}

public class TableRow<Value, Column: TableColummProtocol>: TableRowProtocol {
    public var name: String
    public var data: Column.RowContent
    
    public init(data: Column.RowContent, name: String) {
        self.data = data
        self.name = name
    }
}

public class TableColumn<Value>: TableColummProtocol {
    public var name: String
    public var keyPath: WritableKeyPath<Value, Double>
    
    public init(name: String, keyPath: WritableKeyPath<Value, Double>) {
        self.name = name
        self.keyPath = keyPath
    }
}

App contains the following code:

import Foundation
import DataModule
import ProtocolModule

extension CVPath: TableProtocol {
    public typealias Row = TableRow<CVPath, TableColumn<CVAnchor>>
    public typealias Column = TableColumn<CVAnchor>
    
    public var rows: [Row] {
        let hasSelected = self.anchors.contains{ $0.isActive }
        return self.anchors.enumerated().compactMap { index, knot in
            if !hasSelected || (hasSelected && knot.isActive) {
                return Row(data: knot, name: "knot[\(index)]")
            } else {
                return nil
            }
        }
    }
    
    public var columns: [Column] {
        return [
            Column(name: "x", keyPath: \.x),
            Column(name: "y", keyPath: \.y)
        ]
    }
}

the compile error:

Command SwiftEmitModule failed with a nonzero exit code

Command SwiftCompile failed with a nonzero exit code

When you see Command _ failed with a nonzero exit code it usually means that the tool run by build system crashed. I recommend looking for swift-frontend crash logs on your system (on macOS you can find them in the Crash Reports section of Console.app) and then file a GitHub issue including the crash log. If you can also attach a project/package that reproduces the issue that will greatly increase the chances that the issue can be diagnosed quickly.

Thanks Allan:
I have file a bug here.
I found that just put all the code into a Package, and then the application uses it, and the error can be triggered.