# \[Bug\] Incorrect dependency build order in SPM

**URL:** <https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290>\
**Category:** Using Swift\
**Tags:** packagemanager\
**Created:** [November 26, 2020, 4:25pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290 "2020-11-26T16:25:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apstygo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/apstygo/32/13944_2.png) [@apstygo](https://forums.swift.org/u/apstygo)\
**Post date:** [November 26, 2020, 4:25pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/1 "2020-11-26T16:25:45Z")

</div>

Recently I began porting our frameworks to SPM. We use trunk-based development and have all of our code in a mono-repository, so all of the packages are local.

I've stumbled upon a peculiar problem while building one of the packages. SPM seems to process one of the dependencies of my target _after_ trying to build the target itself. So every build after the first one is successful, but the first one always fails.

> **Here is my package manifest**
>
> ```swift
> // swift-tools-version:5.3
> import PackageDescription
> 
> let package = Package(
> name: "KLStatistics",
> platforms: [
> .macOS(.v10_15), .iOS(.v10)
> ],
> products: [
> .library(name: "KLStatistics_Core", targets: ["KLStatistics_Core"]),
> .library(name: "KLStatistics_Firebase", targets: ["KLStatistics_Firebase"])
> ],
> dependencies: [
> .package(name: "KLFoundation", path: "../KLFoundation"),
> .package(name: "KLFirebase", path: "../KLFirebase")
> ],
> targets: [
> .target(name: "KLStatistics_Core",
> dependencies: ["KLFoundation"],
> path: "KLStatistics_Core/Source"),
> .target(name: "KLStatistics_Firebase",
> dependencies: ["KLFoundation", "KLFirebase", "KLStatistics_Core"],
> path: "KLStatistics_Firebase/Source",
> publicHeadersPath: ".")
> ]
> )
> 
> ```

> **And here is the build log**
>
> ![Снимок экрана 2020-11-26 в 19.22.55](https://global.discourse-cdn.com/swift/original/2X/6/6280a3eca5e23bd26c33ba3ec40f44cbef96b252.jpeg)

It should be noted that both KLFoundation and KLFirebase dependencies are binary frameworks, wrapped into Swift packages

---

<div class="post-metadata">

**Author:** ![rehsals](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rehsals/32/17088_2.png) [@rehsals](https://forums.swift.org/u/rehsals)\
**Post date:** [May 11, 2021, 4:14pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/2 "2021-05-11T16:14:57Z")

</div>

Hi, @apstygo  
Have you been able to work around this issue?  
I've encountered it too while trying to build my SPM module which depends upon couple of binary targets. In my case these binary targets are C-libraries and the error states that headers are not found.

Oddly enough, build only fails when I add this module as SPM dependency to Xcode project.  
When I build it standalone in Xcode (via opening Package.swift) everything works just fine.

I am using Xcode 12.5

---

<div class="post-metadata">

**Author:** ![dougmcbride](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dougmcbride/32/18936_2.png) [@dougmcbride](https://forums.swift.org/u/dougmcbride)\
**Post date:** [August 9, 2021, 6:45pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/3 "2021-08-09T18:45:40Z")

</div>

I'm seeing what seems to be the same issue, and I haven't found a solution yet. Xcode 12.5.1. Using SPM, including package A that depends on package B which uses a `binaryTarget`, and SPM tries to build package A first.

---

<div class="post-metadata">

**Author:** ![DesmanLead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/desmanlead/32/20395_2.png) [@DesmanLead](https://forums.swift.org/u/DesmanLead)\
**Post date:** [November 10, 2021, 3:34pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/4 "2021-11-10T15:34:34Z")

</div>

We're experiencing exactly the same issue with a `binaryTarget`!  
The second build passes as it uses caches. But that's a problem on CI.  
Have you been able to find any workaround?

---

<div class="post-metadata">

**Author:** ![mjablonski-wh](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mjablonski-wh/32/20590_2.png) [@mjablonski-wh](https://forums.swift.org/u/mjablonski-wh)\
**Post date:** [November 23, 2021, 10:11pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/5 "2021-11-23T22:11:28Z")

</div>

I have exactly the same issue. I am not using any binary targets, but am using `.branch` specifier, which I suspect to be problematic.

---

<div class="post-metadata">

**Author:** ![mjablonski-wh](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mjablonski-wh/32/20590_2.png) [@mjablonski-wh](https://forums.swift.org/u/mjablonski-wh)\
**Post date:** [November 29, 2021, 4:49pm UTC](https://forums.swift.org/t/bug-incorrect-dependency-build-order-in-spm/42290/6 "2021-11-29T16:49:52Z")

</div>

So I think I have found a walkaround. i have disabled parallel building for the scheme, and now it is building every time after cleaninig derived data. This is weird, I assumed SPM would be able to determine proper order build for more complex dependencies. But it works for me, so maybe it will work for you.
