React-native-config: Not working for iOS Archive adhoc (often)

Created on 16 Mar 2020  路  9Comments  路  Source: luggit/react-native-config

I notice that if I archive the ios app 10 times
react-native-config does not work 4 times.
All configuration value are empty.

This issue also appears on dev mode, as I can see after press Run button on xCode, then check the file content:

$ cat node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m
Output:
$  #define DOT_ENV @{  };

Expected:

#define DOT_ENV @{  ....THE KEY AND VALUE IN .ENV FILE MUST BE HERE... };

Here my xCode build log of react-native-config pkg:

going to read env file from root folder <MY_SENSITIVE_PROJECT_ROOT>/ios/Pods/../..
**************************
*** Missing .env file ****
**************************
read dotenv {}
Wrote to <MY_SENSITIVE_PROJECT_ROOT>/ios/Pods/../../node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m

Here my react-native info output:

System:
    OS: macOS Mojave 10.14.4
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 81.95 MB / 8.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.6.0 - ~/.nvm/versions/node/v12.6.0/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v12.6.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 25, 26, 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom_64, android-29 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom_64
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5791312
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.5 => 0.61.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1
    react-native-pushdy: 0.0.11
    react-native-swift-cli: 2.3.0

Most helpful comment

I solved this issue.
Follow the docs would not work in my case. I need to do sth different bellow:

FYI, I'm using multiple env file:

  • .env.dev for debug
  • .env.prod for adhoc & build

As I debugged the node_modules/react-native-config/ios/ReactNativeConfig/ReadDotEnv.rb file, I found the problem is RNConfig cannot read the /tmp/envfile content if I use pre-action of Run & Archive scheme. The reason is those pre-action will run after RNConfig build.

To correctly set the /tmp/envfile content, I use Build scheme instead of Run & Archive:

Scheme > Build > Pre-action:

if [ "${CONFIGURATION}" == "Debug" ]; then
  osascript -e "display notification \"RNConfig Generate Preparation: .env.dev ~${CONFIGURATION}~ \""
  echo ".env.dev" > /tmp/envfile
else
  osascript -e "display notification \"RNConfig Generate Preparation: .env.prod ~${CONFIGURATION}~ \""
  echo ".env.prod" > /tmp/envfile
fi

Remember to choose Provide build settings from dropdown so that bash can access CONFIGURATION env:
image

To verify that RNConfig work correctly, read the xCode build log:
image

All 9 comments

@luatnd please use the latest version 1.0.0 and test again

Hi,
I am using this package for env config, android works fine but in iOS it was able to read the file but not the content, because of spacing issues it should be like API_KEY='abc' and not API_KEY = 'abc',anyhow worked :)
Regarding your issue
I think you are running it from xcode, can you run from terminal using package scripts and check?

@rajatrao777 yeah!

You can run passing the current scheme like this

xcodebuild -workspace ios/your_project.xcworkspace -scheme your_scheme -configuration Release or Debug

I am having a unique issue with iOS as well. It seems that it always uses the .env contents of the _previous_ build. So if I build for Dev and then Prod, the Prod build will use the .env.development. Then if I build for Prod again it will correctly use the .env.prod.

@zakton5 do you have added the script for the scheme in the Pre-actions following this steps?

```The basic idea in iOS is to have one scheme per environment file, so you can easily alternate between them.

Start by creating a new scheme:

In the Xcode menu, go to Product > Scheme > Edit Scheme
Click Duplicate Scheme on the bottom
Give it a proper name on the top left. For instance: "Myapp (staging)"
Then edit the newly created scheme to make it use a different env file. From the same "manage scheme" window:

Expand the "Build" settings on left
Click "Pre-actions", and under the plus sign select "New Run Script Action"
Where it says "Type a script or drag a script file", type:
echo ".env.staging" > /tmp/envfile # replace .env.staging for your file```

@luancurti I did follow that step but I put the script under the Run build scripts accidentally. I changed it to be under Build and now it works. Thank you!

@luatnd please use the latest version 1.0.0 and test again

Thanks, I'm using 1.1.0 and it still uses the default .env file instead of .env.dev or .env.prod

I solved this issue.
Follow the docs would not work in my case. I need to do sth different bellow:

FYI, I'm using multiple env file:

  • .env.dev for debug
  • .env.prod for adhoc & build

As I debugged the node_modules/react-native-config/ios/ReactNativeConfig/ReadDotEnv.rb file, I found the problem is RNConfig cannot read the /tmp/envfile content if I use pre-action of Run & Archive scheme. The reason is those pre-action will run after RNConfig build.

To correctly set the /tmp/envfile content, I use Build scheme instead of Run & Archive:

Scheme > Build > Pre-action:

if [ "${CONFIGURATION}" == "Debug" ]; then
  osascript -e "display notification \"RNConfig Generate Preparation: .env.dev ~${CONFIGURATION}~ \""
  echo ".env.dev" > /tmp/envfile
else
  osascript -e "display notification \"RNConfig Generate Preparation: .env.prod ~${CONFIGURATION}~ \""
  echo ".env.prod" > /tmp/envfile
fi

Remember to choose Provide build settings from dropdown so that bash can access CONFIGURATION env:
image

To verify that RNConfig work correctly, read the xCode build log:
image

Thanks @luatnd , it worked like a charm

Was this page helpful?
0 / 5 - 0 ratings