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)
@goatandsheep What I want to add (wiki could be OK) is a warning about sentry.
I am setting
SENTRY_DSNin GitHub Action environment variables, but this happened to me.My
.envlooks like this (simplified example)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.localThis works without any issues for my android builds. But for my ios builds, I was getting
Version: gitin my production app.What was happening was this:
sentry-cli react-native xcode ../node_modules/react-native/scripts/react-native-xcode.sh.envbut not.env.localand sets the environment up forreact-native-xcode.shThis means when react-native-dotenv runs you have
process.env.APP_ENVset togit. Most importantly, Sentry ignores.env.local.This affects anyone who
.env.env.localThe
env.localoverride 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
.envfiles, and you must read the documentation thoroughly and deeply to discover it.So perhaps we can add a one-liner to the readme like
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:
It works if Metro is started alone
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.envfile.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.
This link https://shockoe.com/ideas/development/how-to-setup-configurations-and-schemes-in-xcode/ is not working mentioned here in docs https://github.com/goatandsheep/react-native-dotenv/wiki/Multi-env-troubleshooting#xcode
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’m also using sentry-cli, so I’ll try to set
SENTRY_LOAD_DOTENV=0and 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=productionuser defined setting.Also gave Full Disk Access for XCode
Nothing helps, unfortunately 😦