rules_xcodeproj icon indicating copy to clipboard operation
rules_xcodeproj copied to clipboard

Feature Request: Allow setting additional environment variables in `Test` action

Open thii opened this issue 2 years ago • 6 comments

Currently the environment variables in the scheme's Test action are already inherited from the _test target's env attribute. But there are use cases where environment variables don't have fixed values, so it's not possible to define them in the target definition. This would be possible if we can define additional Xcode build settings in the generated Xcode project, then expand them in the target:

ios_ui_test(
    ...
    env = {"MY_VAR": "$(MY_VAR)"},
    ...
)

thii avatar Nov 29 '23 15:11 thii

The test you have defined there, wouldn't be runnable outside of Xcode then, right? I'm hesitant to add capabilities that make it possible to have a target only work in Xcode, but not with bazel test. How would you do this outside of Xcode?

brentleyjones avatar Nov 29 '23 15:11 brentleyjones

The variable is optional, so it can run outside of Xcode, but we always run this with Xcode or xcodebuild.

thii avatar Nov 29 '23 15:11 thii

I'm thinking through how the API would work for this. At a basic level we would have to specify a [Label: [String: String] association, similar to associated_extra_files.

With the new incremental generation mode I've improved the custom scheme API to allow adding additional specifiers, to ensure you are talking about the exact right target (decedent of a certain top-level target and possibly target environment). I don't think that's the right method here. Instead, how about we allow setting .xcconfig files at the project and/or target level? Then you can use the [sdk=] or [config=] conditionals in the .xcconfig file to adjust based on Xcode configuration or platform. In that case I think just a label would be good enough for the association. What do you think?

brentleyjones avatar Nov 29 '23 15:11 brentleyjones

That would work. But I don't really like to manage yet another git-ignored file. Is there a way to take certain environment variables and make them available in test? I thought this worked in Bazel but when I tried, it didn't:

$ export MY_VAR=value
# .bazelrc
test --test_env=MY_VAR

thii avatar Nov 29 '23 15:11 thii

That should work, but you'll need to adjust xcodeproj.bazel_env to include MY_VAR.

brentleyjones avatar Nov 29 '23 15:11 brentleyjones

I'll need to look a bit more but this was the only way that I saw it generated correctly in the Xcode project.

# .bazelrc
build --test_env=MY_VAR=value

thii avatar Nov 29 '23 16:11 thii

Seems like the desire for this went away? Ping if that's not the case.

brentleyjones avatar Mar 20 '24 16:03 brentleyjones