How can I export a .dot file for dependency graph of Swift package target?

I'm using Swift package manager pretty extensively in my application - for all 3rd party packages, as well as ~150 local targets across ~9 local packages. My application target basically just imports the root of the swift package dependency graph and has the AppDelegate / SceneDelegate. I would like to export the resolved dependency graph including local and remote targets in a .dot graphviz format which I can visualize.

I know that this is possible because this PR was merged a while ago:
https://github.com/apple/swift-package-manager/pull/302

But I'm not sure how to invoke this either through Xcode or from the command line.
I'd love to be able to do something like:

swift build MyRootTarget --show-dependencies > graphviz

And see the graph of all the transitive dependencies of my root target.

2 Likes

Try this:

swift-package show-dependencies --format=dot

(Also try "swift-package show-dependencies --help" to see what formats are supported).

Hmm so that worked (though I had to run it as swift package show-dependencies --format dot) but it only shows me package dependencies and not product dependencies within those packages. Is it possible to build a product dependency graph?

Alternately, should I avoid putting multiple unrelated products in the same package? E.g. I have several module types like "Features" and "Services" which each have a Package.swift file with many (10+) targets / products like "SignInFeature", "UsersService" etc. What I'm trying to accomplish is mapping out the dependency graph for my package products. I could potentially move each product into its own Package.swift file, but that would be a lot of work.

3 Likes

Our service is multi-modular and out of necessity, I have created a shell script to display graphs in svg. Please refer to it.

3 Likes

When I decided to modularize my iOS project with SPM, I thought that visualizing a package with its dependencies in a graph would be very helpful and intuitive. However, every time I needed to see the graph of any package in my local package list, I found it very inconvenient to write the command to create the dot file in the terminal. It was even hard to see. Therefore, I created SwiftDeps(https://swiftdeps.com) to make these tasks faster and easier and to improve work efficiency.

2 Likes