Using libSwiftPM to modify Package.swift manifest file

I'm looking for an advice on how one would use libSwiftPM to load, modify and output Package.swift file.

Loading the manifest file into an instance of Manifest is simple with ManifestLoader. Now, Manifest is immutable, so modifying it is tricky, but not impossible. However, I can't find a way to generate Package.swift from an instance of Manifest. Is there anything in libSwiftPM or in some other tool that I missed what can product Swift package file (not JSON)?

Thank you

There is no way to turn a loaded Manifest back into its original Package.swift, because that is a lossy operation. (#if, if ProcessInfo.processInfo.environment, etc.)

But you can make targeted changes, or construct a manifest from scratch. That sort of thing is found under Sources/SPMPackageEditor, but it isn’t exposed as a product and isn’t even defined in the manifest. If I remember correctly, that is because it involves a circular dependency on SwiftSyntax that hasn’t been sorted out yet. The circularity is only when constructing the Swift toolchain though, so it is probably possible to use if you stitch it into a manifest yourself. You could see if it works to pull in SwiftPM as a git submodule and then put an entry in your own manifest pointing at that subdirectory of the submodule. The risk of clashing Swift modules is minimal, since there is no “legitimate” way for it to also end up elsewhere in your dependency graph.

1 Like

Thanks @SDGGiesbrecht. SPMPackageEditor looks interesting. In my case it seems that it would be much simpler to write the Manifest exporter to Package.swift than to add various features I need to the package editor. The limitation would be, of course, that it only works with manifest files that don't have any conditions or computations.

Terms of Service

Privacy Policy

Cookie Policy