# Should SwiftPM diagnose missing dependencies?

**URL:** <https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926>\
**Category:** Package Manager\
**Created:** [March 26, 2024, 9:06pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926 "2024-03-26T21:06:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 26, 2024, 9:06pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/1 "2024-03-26T21:06:04Z")

</div>

i wasted a lot of time today trying to debug an issue, which ended up being because SwiftPM _was not actually recompiling my code_ when performing incremental builds.

this happened because a test target description in `Package.swift` did not actually declare a (direct) dependency on the library target being tested. i am not sure how this test target could have possibly passed validation in the first place.

should SwiftPM be diagnosing missing dependencies?

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [March 27, 2024, 4:53am UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/2 "2024-03-27T04:53:12Z")

</div>

There are a number of open issues (and one merged PR) related to this:

- [[SR-1393] [SwiftPM] Enforce Swift module import dependencies · Issue #5297 · apple/swift-package-manager · GitHub](https://github.com/apple/swift-package-manager/issues/5297)
- [[SR-14212] Removing package from Package.swift doesn't result in import error · Issue #4450 · apple/swift-package-manager · GitHub](https://github.com/apple/swift-package-manager/issues/4450)
- [Missing diagnostic about missing dependency · Issue #6607 · apple/swift-package-manager · GitHub](https://github.com/apple/swift-package-manager/issues/6607)
- [Verify that only targets that are manifest dependencies can be imported. by artemcm · Pull Request #3562 · apple/swift-package-manager · GitHub](https://github.com/apple/swift-package-manager/pull/3562)

To me it looks as if Swift compiler and SwiftPM made no checks for whether an `import` has a matching `dependency` declaration in the package file for the current target being built, or even another target that the current one (transitively) depends on!

Instead of checking dependency graph first, the compiler seems to happily continue as long as a previously built module matching that import is found in the build directory.

This lack of discipline in SwiftPM is a constant source of problems for us: things very easily seem to work on my computer, and even a coworker's, until the project's dependency graph is rebuilt in a slightly different order maybe and the missing dependency now causes the import to fail.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [April 8, 2024, 9:16pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/3 "2024-04-08T21:16:56Z")

</div>

so if anyone is stumbling on this, the `--explicit-target-dependency-import-check=error` option can be used to raise an error when this situation arises.

i now wonder if there is a way to add this setting to the `Package.swift`, so that it is always enabled. is this possible?

---

<div class="post-metadata">

**Author:** ![markturnip](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/markturnip/32/23088_2.png) [@markturnip](https://forums.swift.org/u/markturnip)\
**Post date:** [September 12, 2024, 4:30am UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/4 "2024-09-12T04:30:16Z")

</div>

Have you tried?

```swift
swiftSettings: [
   .unsafeFlags(["-Xfrontend", "-explicit-target-dependency-import-check"])
]

```

---

<div class="post-metadata">

**Author:** ![cprince-foreflight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cprince-foreflight/32/34130_2.png) [@cprince-foreflight](https://forums.swift.org/u/cprince-foreflight)\
**Post date:** [October 24, 2024, 5:05pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/5 "2024-10-24T17:05:01Z")

</div>

This fails for me. I get:

```swift
<unknown>:0: error: unknown argument: '-explicit-target-dependency-import-check'
Command SwiftEmitModule failed with a nonzero exit code

```

In my Package.swift:

```swift
// swift-tools-version: 5.10

```

---

<div class="post-metadata">

**Author:** ![Nikoloutsos](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikoloutsos/32/27022_2.png) [@Nikoloutsos](https://forums.swift.org/u/Nikoloutsos)\
**Post date:** [October 30, 2024, 11:27pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/6 "2024-10-30T23:27:54Z")

</div>

I faced the same issue and I decided to create my own swift build plugin that let me know if my source code is using any transitive dependency.

> **[GitHub - Nikoloutsos/explicitDependencyImportCheck: The missing...](https://github.com/Nikoloutsos/explicitDependencyImportCheck)**
>
> The missing --explicit-target-dependency-import-check flag for SPM ⭐️

---

<div class="post-metadata">

**Author:** ![michaelversus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michaelversus/32/29900_2.png) [@michaelversus](https://forums.swift.org/u/michaelversus)\
**Post date:** [May 19, 2025, 9:59pm UTC](https://forums.swift.org/t/should-swiftpm-diagnose-missing-dependencies/70926/7 "2025-05-19T21:59:53Z")

</div>

Still waiting apple to fix this one. You can also try my tool [here](https://github.com/michaelversus/SwiftImportChecks) if you prefer git-hooks over swift build plugins.
