react-native-dotenv: iOS release build: .env.production.local file does not overwrite variables defined in base .env file

  • Asked question in discussions
  • Tried the troubleshooting Wiki
  • Followed the migration Wiki

In iOS release build, variables defined in base .env file aren’t overwritten by variables in .env.production.local Reproduces only for iOS release builds. On Android there is no any issue.

To Reproduce

.env file:

SOME_VAR1=var1

.env.production file:

SOME_VAR2=var2

.env.production.local file:

SOME_VAR1=overwrite-var1
SOME_VAR2=overwrite-var2

Expected behavior console.log(SOME_VAR1); // overwrite-var1 console.log(SOME_VAR2); // overwrite-var2

Actual behavior Android (release) console.log(SOME_VAR1); // overwrite-var1 console.log(SOME_VAR2); // overwrite-var2

Actual behavior iOS (release) console.log(SOME_VAR1); // var1 - but it should be overwritten by value in .env.production.local console.log(SOME_VAR2); // overwrite-var2

RN: 0.70.6 react-native-dotenv: 3.4.2, 3.4.6

babel.config.js:

plugins:[
//...
  [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env',
        safe: false,
        allowUndefined: true,
      },
   ],
]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 27 (8 by maintainers)

Most upvoted comments

@goatandsheep What I want to add (wiki could be OK) is a warning about sentry.

I am setting SENTRY_DSN in GitHub Action environment variables, but this happened to me.

My .env looks like this (simplified example)

APP_VERSION="git"

It’s committed to git and will be loaded in on developer machines. In the burger menu Version: git is displayed as the version of the app.

As part of the CI process, I grab the git tag being processed and write the following to .env.local

APP_VERSION="1.4.1"

This works without any issues for my android builds. But for my ios builds, I was getting Version: git in my production app.

What was happening was this:

  • Sentry overrides the code build process in Xcode to build the JS bundle and upload the source files
  • It runs sentry-cli react-native xcode ../node_modules/react-native/scripts/react-native-xcode.sh
  • As part of this process, it has its own internal dotenv (using the go library, I believe)
  • This loads .env but not .env.local and sets the environment up for react-native-xcode.sh

This means when react-native-dotenv runs you have process.env.APP_ENV set to git. Most importantly, Sentry ignores .env.local.

This affects anyone who

  • Uses sentry in their react-native app
  • Has a variable in .env
  • Then changes it in .env.local

The env.local override will be ignored by react-native-dotenv because Sentry already loaded it into the environment.

From my perspective, this is completely unexpected behaviour from Sentry. I would never have expected that it supports .env files, and you must read the documentation thoroughly and deeply to discover it.

So perhaps we can add a one-liner to the readme like

NOTE: If environment variables are not loading correctly and use are using Sentry refer to WIKIPAGE

The wiki page could then dive into something like my explanation above and how to use SENTRY_DOTENV_LOAD=0

It doesn’t work if Metro is started with the build and runs on another shell:

APP_ENV=staging react-native run-ios
APP_ENV=staging react-native run-android

It works if Metro is started alone

APP_ENV=staging react-native start

I know you’re actively on this but I’ll have to think longer on this. and also will need to improve some of the docs on caching and switching environments.

@goatandsheep I do use XCode schemes + build configurations like described in https://shockoe.com/ideas/development/how-to-setup-configurations-and-schemes-in-xcode/, but I don’t set NODE_ENV anywhere - just didn’t find where should I do that. But anyway, after some experiments I realized that .env.production.* files are used (so NODE_ENV=production in release configurations) like in issue description, but they aren’t overwrite vars defined in base .env file.

Also I didn’t give Full Disk Access for XCode, like described in https://github.com/goatandsheep/react-native-dotenv/wiki/Multi-env-troubleshooting#xcode, but I don’t think it matters for this case.

Yeah it happens where ever you’re running sentry-cli to upload the artifacts.

I’ll throw some text together and send it across for you to add a little later

I’ve worked this out for my particular use case.

I’m using sentry. sentry-cli gets called as part of the xCode build process. Sentry supports .env files to load in things like SENTRY_DSN.

You can disable it by setting SENTRY_LOAD_DOTENV=0

I’m also using sentry-cli, so I’ll try to set SENTRY_LOAD_DOTENV=0 and inform here

@goatandsheep Do you think it’s worth adding an entry to the README specifically about sentry? I’m happy to create a PR

Just tried to change set up for all release build configurations in XCode: for every Release build configuration added NODE_ENV=production user defined setting.

Also gave Full Disk Access for XCode

Nothing helps, unfortunately 😦