Can swift package generate-xcodeproj preserve project environment variables?

Hello!

I think it would be helpful to check for any existing environment variables for the "Run" scheme.

Each time I include a new package into my project and run swift package generate-xcodeproj I have to manually go in and add my env vars again.

The exact command I run is actually:
swift package -Xlinker -L/usr/local/lib generate-xcodeproj

This is so I can use MySQL, and I have my database connection details in env vars.

Thanks for reading. I look forward to hearing back!
Jared Anderton

1 Like

Yes, this sucks, but as a workaround you can write scripts that call swift package generate-xcodeproj and then run some other code that sets the environment variables, e.g. with Python

import xml.etree.ElementTree as ETree

file = "MyProject.xcodeproj/xcshareddata/xcschemes/MyTarget.xcscheme"

tree = ETree.parse(file)

env_var_section = ETree.SubElement(tree.getroot().find("LaunchAction"), "EnvironmentVariables")
my_env_var_setting_1 = ETree.SubElement(env_var_section, "EnvironmentVariable")
my_env_var_setting_1.attrib["key"] = "MY_ENV_VAR_1"
my_env_var_setting_1.attrib["value"] = "true"
my_env_var_setting_1.attrib["isEnabled"] = "YES"
my_env_var_setting_2 = ETree.SubElement(env_var_section, "EnvironmentVariable")
my_env_var_setting_2.attrib["key"] = "MY_ENV_VAR_2"
my_env_var_setting_2.attrib["value"] = "500"
my_env_var_setting_2.attrib["isEnabled"] = "YES"

tree.write(file)

Alternatively, you might instead store your configuration in files (those don't need to be committed, if they contain sensitive data), and use something like Configuration or DotEnv. This is probably a cleaner long-term solution (though I agree that it would be nice if the script didn't overwrite existing environment variables...).

1 Like

Hi Jared,

I ran into the same issue a few months ago, some good alternative workaround suggestions can be found on this thread:

Terms of Service

Privacy Policy

Cookie Policy