How to set SWIFTPM_ENABLE_PLUGINS=1 in Xcode

When I'd like to test the plugin API in my Package.swift file. I got the error saying

plugin target 'Generate' cannot be used because the feature isn't enabled (set SWIFTPM_ENABLE_PLUGINS=1 in environment)

I can easily export SWIFTPM_ENABLE_PLUGINS=1 and run swift build successfully in the terminal.

But how can I add this env value so that Xcode can read.

Tried to add the export in my .zshrc file not working

Xcode scheme editor can add env value easily but scheme is not available before properly "Resolve Package" is done

I tried ProcessInfo().environment, but it's a get-only property

Found a workaround:
export SWIFTPM_ENABLE_PLUGINS=1 && open /Applications/Xcode.app

Xcode never sources this file.

There is a way to set environment variables at run-time:

setenv("SWIFTPM_ENABLE_PLUGINS", "1", 1)

Also this post does not belong in the "development" category.

1 Like

setenv seems work for normal swift files, but not work in Package.swift. Maybe due to it is running in the sandbox.

After I workaround to run export SWIFTPM_ENABLE_PLUGINS=1 && open /Applications/Xcode.app.

A new problem came out. I can't import PackagePlugin to get Plugin API, because it's missing. :joy:

Fixed, moving it to "Using Swift" and tagged with PackageManager

This makes no sense. setenv sets environment variables at run time. I don't know what it would mean for this to work on a per-file basis.

Presumably, this environment variable needs to be present at build-time. This is the same reason why setting environment variables in the scheme doesn't work.

Maybe I should just using the swift 5.6-dev snapshot toolchain.

I change it in Xcode setting and set the env to pass it to Xcode. export TOOLCHAIN=swift && open /Applications/Xcode.app

I can confirm this change by echo $(swift --version) > ~/tmp.txt in a normal iOS project's pre-build script

But when opening the package project, Xcode still gives me an error

Showing All Messages
package at 'XX' is using Swift tools version 5.6.0 but the installed version is 5.5.0

How can I change this behavior?

If you received this error:

Internal error: missingPackageDescriptionModule - Resolving Package Graph Failed

Then see Xcode 13.2 - Internal error : Miss… | Apple Developer Forums

Make sure you've updated to Xcode 13.2.1.

Yes, maybe the SPM need the env/flag to be set before compile time.

But "scheme doesn't work" is not because of it. It's due to before you have a valid resolution of Package.swift file, Xcode won't show the scheme editor.

I know this issue, but it seems irrelevant to my issue. (I am using Xcode 13.2 RC in the developer website and does not have this issue)

Below is my issue if I use // swift-tools-version:5.5 and run Xcode through export SWIFTPM_ENABLE_PLUGINS=1 && open /Applications/Xcode.app

If the environment variable needs to be set before package resolution, then it definitely can't be set at run-time, which is when environment variables defined in the scheme are set. Package resolution occurs before run-time. Surely you know this.

Also, is english not your first language?

Oh, I got what you mean. Thanks

Also, is english not your first language?

Yes, it is my second language. Sorry if there were any grammar mistakes in my statements which makes you confused.