3

I have a Package.swift file with the following structure

var package = Package(
  name: "MyProject",
  targets: [
    Target(name: "MyProject")
  ],
  dependencies: [],
  exclude: ["Exclude"]
)

#if DEBUG
  package.dependencies.append(Package.Dependency.Package(url: "Dependency-One.git", majorVersion: 0, minor: 0))
#else
  package.dependencies.append(Package.Dependency.Package(url: "Dependency-Two.git", majorVersion: 0, minor: 0))
#endif

When I build this with any of the following:

swift build
swift build -c release
swift build -c debug
swift build -c RELEASE
swift build -c DEBUG

It still always downloads Dependency-Two.git. This remains true if I prepend all of the above with

rm -rf .build/ && rm -rf Packages/

So I don't think it's because it's reusing some cache. Is it possible to do what I'm intending here?

Ethan Kay
  • 657
  • 6
  • 24
  • What about `if getenv("SOME_KEY") != nil { /* add some dependency */ }`? And invoke the build command as `SOME_KEY=true swift build -c debug`. Whether `Package.swift` is conditionally compiled or conditionally executed has the same effect. – Meow Cat 2012 May 12 '23 at 10:53

2 Answers2

3

Still not available. My workaround has been to create multiple Package.swift files, named like Package-DEBUG.swift and have a script copy the appropriate one to Package.swift at build time.

This is a really mindless workaround and the SPM developers really need to implement command-line flags.

Reid Ellis
  • 3,996
  • 1
  • 21
  • 17
1

This is by design. The manifest is not intended to be used to declare conditional behaviors in this manner, but unfortunately for what you are trying to do, the mechanism by which this would be supported (e.g., including additional APIs from PackageDescription to declare what you want in each configuration) is not designed yet.

I suggest you file an enhancement request for this feature on https://bugs.swift.org.

Daniel Dunbar
  • 3,465
  • 1
  • 21
  • 13