Skip to content

Commit

Permalink
Fail iOS builds on malformed .env files, add troubleshooting instruct…
Browse files Browse the repository at this point in the history
…ions (#516)
  • Loading branch information
ghoshabhi authored Nov 5, 2020
1 parent 3db0022 commit 0c42216
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ If using Dexguard, the shrinking phase will remove resources it thinks are unuse

-keepresources string/build_config_package

### TypeError: _reactNativeConfig.default.getConstants is not a function

This error stems from `.env` file being malformed. Accepted formats are listed here https://regex101.com/r/cbm5Tp/1. Common causes are:
- Missing the .env file entirely
- Rogue space anywhere, example: in front of env variable: ` MY_ENV='foo'`

## Testing

Since `react-native-config` contains native code, it cannot be run in a node.js environment (Jest, Mocha). [react-native-config-node](https://github.com/CureApp/react-native-config-node) provides a way to mock `react-native-config` for use in test runners - exactly as it is used in the actual app.
Expand Down
2 changes: 2 additions & 0 deletions android/dotenv.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def loadDotEnv(flavor = getCurrentFlavor()) {

if (f.exists()) {
f.eachLine { line ->
// https://regex101.com/r/cbm5Tp/1
def matcher = (line =~ /^\s*(?:export\s+|)([\w\d\.\-_]+)\s*=\s*['"]?(.*?)?['"]?\s*$/)
// TODO: Fail Android builds if line doesn't match
if (matcher.getCount() == 1 && matcher[0].size() == 3) {
env.put(matcher[0][1], matcher[0][2].replace('"', '\\"'))
}
Expand Down
3 changes: 2 additions & 1 deletion ios/ReactNativeConfig/ReadDotEnv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def read_dot_env(envs_root)

raw.split("\n").inject({}) do |h, line|
m = line.match(dotenv_pattern)
next h if m.nil?
if m.nil?
abort('Invalid entry in .env file. Please verify your .env file is correctly formatted.')

key = m[:key]
# Ensure string (in case of empty value) and escape any quotes present in the value.
Expand Down

0 comments on commit 0c42216

Please sign in to comment.