# How to add dependency on specific library of a package?

**URL:** <https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197>\
**Category:** Using Swift\
**Tags:** packagemanager\
**Created:** [December 21, 2021, 10:51am UTC](https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197 "2021-12-21T10:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enricoza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/enricoza/32/11135_2.png) [@Enricoza](https://forums.swift.org/u/Enricoza)\
**Post date:** [December 21, 2021, 10:51am UTC](https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197/1 "2021-12-21T10:51:00Z")

</div>

Let's say i have a Package that offers several libraries:

```swift
let package = Package(
    name: "Foo",
    products: [
        .library(
            name: "Bar",
            targets: ["Bar"]),
        .library(
            name: "Baz",
            targets: ["Baz"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "Bar",
        ),
        .target(
            name: "Baz",
        )
    ]
)

```

Is there a way for another package to add a dependency to a specific Foo.Bar or Foo.Baz instead of adding a dependency to the whole Foo package?

Something like Cocoapods' [Subspecs](https://guides.cocoapods.org/syntax/podspec.html#subspec) that can be used indipendently from the full podspec both from Podfile and/or another Podspec.

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [December 21, 2021, 12:17pm UTC](https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197/2 "2021-12-21T12:17:39Z")

</div>

When you add a dependency to a package you need to declare that dependency in two places. The first is the package dependency array - that can only specify the whole package. The second is the dependency array for each target that depends on the package you're pulling in. In that part you need to declare the product that target depends on if a package vends more that one target. E.g. [vapor/Package.swift at main · vapor/vapor · GitHub](https://github.com/vapor/vapor/blob/main/Package.swift#L79)

I can't remember the current state of SwiftPM but IIRC it's clever enough to only download the transitive dependencies it needs and won't build products that aren't needed.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [December 21, 2021, 1:36pm UTC](https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197/3 "2021-12-21T13:36:36Z")

</div>

One particularly annoying caveat: restrictions based on platform, Swift version, etc. are package-wide. I don’t really understand why, but they are.

You may be able to use conditional compilation and/or version-specific package manifests to work around that.

---

<div class="post-metadata">

**Author:** ![bdrelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bdrelling/32/2711_2.png) [@bdrelling](https://forums.swift.org/u/bdrelling)\
**Post date:** [December 21, 2021, 2:07pm UTC](https://forums.swift.org/t/how-to-add-dependency-on-specific-library-of-a-package/54197/4 "2021-12-21T14:07:13Z")

</div>

Because you don’t need to use a mono package for everything. Just make separate packages if you need it.
