# Command SwiftEmitModule failed with a nonzero exit code

**URL:** <https://forums.swift.org/t/command-swiftemitmodule-failed-with-a-nonzero-exit-code/64908>\
**Category:** Using Swift\
**Tags:** compiler, recusive-constraint\
**Created:** [May 10, 2023, 5:58am UTC](https://forums.swift.org/t/command-swiftemitmodule-failed-with-a-nonzero-exit-code/64908 "2023-05-10T05:58:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chenyungui](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chenyungui/32/46119_2.png) [@Chenyungui](https://forums.swift.org/u/Chenyungui)\
**Post date:** [May 10, 2023, 5:58am UTC](https://forums.swift.org/t/command-swiftemitmodule-failed-with-a-nonzero-exit-code/64908/1 "2023-05-10T05:58:22Z")

</div>

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:

```swift
// 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：

```swift
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：

```swift
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

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [May 11, 2023, 5:23pm UTC](https://forums.swift.org/t/command-swiftemitmodule-failed-with-a-nonzero-exit-code/64908/2 "2023-05-11T17:23:53Z")

</div>

> [@Chenyungui](#):
>
> Command SwiftEmitModule 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](https://github.com/apple/swift/issues/new?assignees=&labels=bug%2C+triage+needed&projects=&template=bug_report.md&title=) 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.

---

<div class="post-metadata">

**Author:** ![Chenyungui](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chenyungui/32/46119_2.png) [@Chenyungui](https://forums.swift.org/u/Chenyungui)\
**Post date:** [May 12, 2023, 1:41am UTC](https://forums.swift.org/t/command-swiftemitmodule-failed-with-a-nonzero-exit-code/64908/3 "2023-05-12T01:41:08Z")

</div>

Thanks Allan:  
I have file a bug [here](https://github.com/apple/swift/issues/65881).  
I found that just put all the code into a Package, and then the application uses it, and the error can be triggered.
