Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iOS): #591 track env file changes on each build #801

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

valerii-link
Copy link

Why:

This is a fix for #591 , #698 and #788.

What:

Updated the podspec's input_files to include default .env and custom (passed with "ENVFILE") locations. This fix ensures that Xcode recognizes changes in .env files and re-runs the script phase accordingly.

I suggest that touching BuildDotenvConfig.rb is not the best solution, as it will re-run the script phase on every build, even if .env is unchanged.

execution_position: :before_compile,
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb'],
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb', '$SRCROOT/../../.env', '$ENVFILE'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe need to add '$SRCROOT/../../$ENVFILE' for relative file name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit tricky 😁
$ENVFILE is used for the case when the env file is set via build_settings, like so (in your Podfile):

ENVFILES = {
  'Debug' => '$(PODS_ROOT)/../../.env.debug',
  'Release' => '$(PODS_ROOT)/../../.env.production',
}
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if target.name == 'react-native-config'
        config.build_settings['ENVFILE'] = ENVFILES[config.name]
      end
    end
  end
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I don't use the "alternative" path as described here: https://github.com/lugg/react-native-config?tab=readme-ov-file#ios-1

But instead define ENVFILE as environment variable before running the react native scripts from the command line:

In the scripts section of package.json:

"sim.prod": "cross-env ENVFILE=.env.production  react-native run-ios --no-packager --simulator 'iPhone 15 Pro (17.2)'",

Since the Ruby files in this package are reading the environment variables.

I guess I could switch that to use a scheme arg on the CLI instead, but others may also make the above assumption that setting the ENVFILE env var works.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I think the following is better:

"sim.prod": "cross-env ENVFILE=$(pwd)/.env.production react-native run-ios --no-packager --simulator 'iPhone 15 Pro (17.2)'"

Why:
To avoid mixing concerns. The Xcode project should not define where the ENVFILE is located (except for the default location like '$SRCROOT/../../.env').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants